📄 game.cpp
字号:
// game.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "game.h"
#include <iostream>
#include <string>
using namespace std;
//定义
void getNextElem(int cur_seq_index);
void DisplayguessRec(GuessRecord guessRec);
//下面是主函数
int _tmain(int argc, _TCHAR* argv[])
{ // int _tmain
string user_name;
cout <<"请输入您的姓名: ";
cin >> user_name;
system("cls");
cout <<"==================================="<<endl;
cout <<" "<<user_name<<",欢迎您光临本游戏!"<<endl;
cout <<"==================================="<<endl;
GuessRecord guessRec = {0, 0}; //初始化猜测记录结构体
GuessRecord *Rec;
Rec = &guessRec;
strcpy(guessRec[0].FuctionName,"Fibonacci ");
strcpy(guessRec[1].FuctionName,"Lucas ");
strcpy(guessRec[2].FuctionName,"Pell ");
strcpy(guessRec[3].FuctionName,"Triangular");
strcpy(guessRec[4].FuctionName,"Square ");
strcpy(guessRec[5].FuctionName,"Pentagonal");
bool next_seq = true; //
while ( next_seq == true )
{ //while1
int usr_guess; //存放用户的答案
char usr_rsp = 'N'; //答错了再试?
bool keep_on_it = true; //用户继续猜错后继续?
bool got_it = false; //用户猜对了?
int cur_seq_index = rand() % seq_cnt; //随机生成序列索引号
cout <<"##################"<<endl;
while (( got_it == false ) && ( keep_on_it == true ))
{//while2
elemstart_index = rand() % elem_cnt; //随机生成元素索引号
getNextElem(cur_seq_index); //调用序列选择函数
cout <<"函数索引号:"<< cur_seq_index << endl
<<"元素索引号:"<< elemstart_index << endl;
//显示随机元素序列
cout << "数列中的三个连续元素是: "
<< elem1 <<" "<< elem2 <<" "<< elem3 << endl
<< "下一个数是什么? ";
cin >> usr_guess;
guessRec[cur_seq_index].num_tries++; //猜测次数
if ( usr_guess == next_elem ) //用户答对了的操作
{ //if1
got_it = true;
cout << "----------------"<< endl
<< "恭喜,您答对了!"<< endl
<< "----------------"<< endl;
guessRec[cur_seq_index].num_cor++; //猜测正确次数
DisplayguessRec(guessRec);
}
else //用户答错了的操作
{
cout << "----------------"<< endl
<< "很遗憾,您答错了!"<< endl
<< "----------------"<< endl;
DisplayguessRec(guessRec);
cout << "想要再试试其他元素吗?(Y/N): " ;
cin >> usr_rsp;
if ( usr_rsp == 'N' || usr_rsp == 'n' )
keep_on_it = false;
} // end of if1
} // end of while2
cout << "想继续玩游戏吗?(Y/N): ";
char try_again;
cin >> try_again;
if ( try_again == 'N' || try_again == 'n' )
next_seq = false;
} // end of while1
} // end of int _tmain
//下面的函数用于调用相应序列函数来生成序列的三个连续元素和它们下一个元素
void getNextElem(int cur_seq_index)
{
switch(cur_seq_index)
{
case 0:
Fibonacci(); break;
case 1:
Lucas(); break;
case 2:
Pell(); break;
case 3:
Triangular(); break;
case 4:
Square(); break;
case 5:
Pentagonal(); break;
default: break;
}
}
//下面显示猜测数据统计信息
void DisplayguessRec(GuessRecord guessRec)
{
cout <<"统计:"<< endl
<<"函数名字"<<'\t'<<"猜测次数"<<" "<<"正确次数" << endl;
for(int i=0;i<6;i++)
{
cout << guessRec[i].FuctionName
<< '\t'<<guessRec[i].num_tries
<< '\t'<<" "<<guessRec[i].num_cor << endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -