c++猜谜游戏
C++ Guessing Game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main();
void Menu();
void PlayGame();
void Again();
int main()
{
// Seed the random number generator
srand(time(0));
Menu();
return 0;
}
// Menu so can keep playing or close the program
void Menu()
{
int selection = 0;
bool playing = true;
while (playing)
{
cout << "Guess Number Gamenn";
cout << "Menunn";
cout << "1) Play Gamen";
cout << "2) Exitnn";
cout << "Enter your selection: ";
cin >> selection;
cin.ignore();
cout << endl;
if (selection == 1)
PlayGame();
else if (selection == 2)
playing = false;
else if (selection != 1 || 2) //lets the user know why they are going back to the menu
cout << "Try again, choose 1 or 2" << endl;
}
}
void PlayGame()
{
int RandomNumber = rand() % 50 + 1; // to set the random number between 1-50
int attempts = 15; // sets attempts at guessing the number to 15
int guess;
const int HighestNum = 50;
const int LowestNum = 1;
cout << "Guess the random number between 1 and 50!n";
while (true)
{
cout << "You have " << attempts << " attempts remainingnn"; // so the user can keep track of attempts
cout << "Enter a number: ";
cin >> guess;
cin.ignore();
cout << 'n';
if (guess == RandomNumber)
{
cout << "Congratulations, you won: " << RandomNumber << "!";
cout << "nn" << endl;
cout << "Would you like to play again y or n?" << endl;
cin.get ();
void Again();
}
else if (guess < RandomNumber) // to satisfy requirements of showing user whether the guess is too low
{
cout << "That guess is too low!n" << endl;
}
else if (guess > RandomNumber)// to satisfy requirements of showing user whether the guess is too high
{
cout << "That guess is too high!n" << endl;
}
else if (guess > HighestNum || guess < LowestNum)
{
cout << "Guess must be lower than 50 and higher than 1, Try again" << endl; //setup so guesses not within the range of 1-50 do not count against the 15 guesses
cin.get();//remove this to count guesses that are outside the 1-50
}
attempts--;
if (attempts == 0)
{
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!";//so the user can see the random number at the end of their attempts
cout << "n";
cin.get();
void Again();
}
}
void Again();
{
int decision = 0;
bool TryAgain = true;
char y;
char Y;
char n;
char N;
while (TryAgain)
{
cout << "Y) Play Againn";
cout << "N) Exitnn";
cout << "Enter your selection: ";
cin >> decision;
cin.ignore();
cout << endl;
if (decision == y || decision == Y)
{
PlayGame();
}
else if (decision == n || decision == N)
{
TryAgain = false;
}
else if (decision != y, Y || n, N) //lets the user know why they are going back to the menu
{
cout << "Try again, choose y or n" << endl;
}
}
return
试图让void Again ();
从void PlayGame();
读取。使用void Again ();询问用户是否想再玩一次。请帮助!
也可以使用别人的帮助来改变函数,使其具有int reviewGuess(int, int)的原型,其中函数将计算机生成的随机数作为第一个参数,将用户猜测的数字作为第二个参数。
- 如果数字匹配,函数将返回一个零值。
- 如果数值太大,函数返回值为1。
- 如果数值过低,函数返回值为-1。
相当困惑的ATM。
谢谢你,
首先你应该解决Alde提到的问题。接下来,while (true)
声明是怎么回事?您可能应该将其替换为while (attempts != 0)
并设置:
if (attempts == 0) {
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber
<< "!";//so the user can see the random number at the end of their attempts
cout << "n";
cin.get();
void Again();
}
超出while作用域。
关于你的int reviewGuess(int, int)
函数,你是在寻找这样的东西吗:
int reviewGuess(int randomNumber,int userChoice)
{
if(randomNumber == userChoice)
return 0;
if(userChoice > 50)
return 1;
if(userChoice < 1)
return -1;
}
我看到了多个问题:
- 在PlayGame和Again函数的末尾缺少结尾'}'
- 的返回值是可选的,在 的末尾应该有一个';'
- 您正在将'decision'与初始化变量y, y, n和n进行比较…我猜你是想把它的值和字符'y' 'y' 'n' 'n'比较
- 'else if (decision != y, y || n, n)'不是有效的语法,您已经在这种情况下使用'else'
- 你有一个额外的';'在'void Again()'的末尾
- 你正在尝试调用一个函数'void PlayGame();'
- 程序所取的路径不干净,并且你正在做不必要的递归,最好返回一个值而不是
我会这样做:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void PlayGame()
{
const int HighestNum = 50;
const int LowestNum = 1;
int RandomNumber = LowestNum + rand() % HighestNum;
cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!nn";
int attempts = 15;
while (attempts > 0)
{
int guess;
cout << "You have " << attempts << " attempt" << (attempts > 1 ? "s" : "") << " remainingn";
cout << "Enter a number: ";
cin >> guess;
cout << endl;
if (guess < LowestNum || guess > HighestNum)
{
cout << "Guess must be higher than " << LowestNum << " and be lower than " << HighestNum << ". Try again!n" << endl;
continue;
}
if (guess == RandomNumber)
{
cout << "Congratulations, you won!nn" << endl;
return;
}
else if (guess < RandomNumber)
{
cout << "That guess is too low!n" << endl;
}
else if (guess > RandomNumber)
{
cout << "That guess is too high!n" << endl;
}
--attempts;
}
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!nn";
}
bool Menu()
{
cout << "Guess Number Gamenn";
cout << "Menunn";
cout << "1) Play Gamen";
cout << "2) Exitnn";
cout << "Enter your selection: ";
int selection = 0;
while (true)
{
cin >> selection;
cout << endl;
if (selection == 1)
return true;
else if (selection == 2)
return false;
else
cout << "Try again, choose 1 or 2: ";
}
}
bool Again()
{
cout << "1) Play Againn";
cout << "2) Exitnn";
cout << "Enter your selection: ";
int selection = 0;
while (true)
{
cin >> selection;
cout << endl;
if (selection == 1)
{
return true;
}
else if (selection == 2)
{
return false;
}
else
{
cout << "Try again, choose 1 or 2: ";
}
}
}
int main()
{
srand(time(0));
bool play = Menu();
if (play)
while (true)
{
PlayGame();
if (!Again())
break;
}
return 0;
}
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- 文本冒险游戏 - 如何区分一种项目类型与另一种项目类型以及如何构建项目类/子类