C++完成了随机猜测我的数字游戏Lookover
C++ Finished Random Guess my Number Game Lookover
我想知道是否有人可以为这个"随机猜测我的数字游戏"查看我的代码。
以下是所有代码:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int PlayerNumber, x;
bool win = false;
long foo;
char milliongame;
int main(){
srand (time(NULL));
cout << "Guess your computers number." << endl << endl;
for (int level = 1, foo = 10; level<=10; level++, foo = foo*2){
switch ( foo ){
case 20:
foo = 25;
break;
case 200:
foo = 250;
break;
case 2000:
foo = 2500;
break;
}
win = false;
x = (rand()%foo + 1);
refresh:
cout << "level " << level << endl;
cout <<"Computers number is between 1 and "<< foo << endl;
cout << "Your number: "; cin >> PlayerNumber;
if (!cin || PlayerNumber > foo || PlayerNumber <= 0){
cout << "That's not a number between 1 and " << foo << " !" << endl;
PlayerNumber = 0;
system("pause");
system("cls");
goto refresh;
}
if (x > PlayerNumber)
cout << "Your number was lower than the computers." << endl;
if (x < PlayerNumber)
cout << "Your number was higher than the computers" << endl;
if (x == PlayerNumber ){
cout << "You Won! nnThe computer's number was "<< x << endl; win = true;
}
cout << endl; system("pause");
system("cls");
if (win != true){
goto refresh;
}
}
cout << "You Beat the Game! nnDo You Dare Play for a Million? n[y/n]: "; cin >> milliongame;
if (milliongame != 'y')
return 0;
mill:
cout << "million level!" << endl;
x = (rand()%1000000000);
cout <<"Computers number is between 1 and 1,000,000,000" << endl;
cout << "Your number: "; cin >> PlayerNumber;
if (!cin || PlayerNumber > 1000000000 || PlayerNumber <= 0){
cout << "That's not a number between 1 and 1,000,000,000!" << endl;
PlayerNumber = 0;
system("pause");
system("cls");
goto mill;
}
if (x > PlayerNumber)
cout << "Your number was lower than the computers." << endl;
if (x < PlayerNumber)
cout << "Your number was higher than the computers" << endl;
if (x == PlayerNumber )
{cout << "You Won! nnThe computer's number was "<< x << endl; win = true;
}
}
任何能给我任何提示的人都将不胜感激。
这个想法是玩家geuss的一个数字,程序会告诉他们这个数字是高于还是低于电脑的数字。
还要注意,它是一个使用Visual C++2012 编译的控制台应用程序
谢谢!
这段代码:
if (milliongame == 'y')
{goto mill;}
else
return 0;
mill:
cout << "million level!" << endl;
你可以用更简单的替代:
if (milliongame != 'y')
return 0;
cout << "million level!" << endl;
此:
if (foo == 20)
foo = 25;
if (foo == 200)
foo = 250;
if (foo == 2000)
foo = 2500;
这个:
switch ( foo )
{
case 20:
foo = 25;
break;
case 200:
foo = 250;
break;
case 2000:
foo = 2500;
break;
}
这个:
if (x > PlayerNumber)
cout << "Your number was lower than the computers." << endl;
if (x < PlayerNumber)
cout << "Your number was higher than the computers" << endl;
if (x == PlayerNumber )
{cout << "You Won! nnThe computer's number was "<< x << endl; win=true;}
带有:
if (x > PlayerNumber)
cout << "Your number was lower than the computers." << endl;
else if (x < PlayerNumber)
cout << "Your number was higher than the computers" << endl;
else
{
cout << "You Won! nnThe computer's number was "<< x << endl;
win=true;
}
此外,最后一段代码在您的程序中出现两次,因此您可以创建一个函数
void result(int PlayerNumber, int x)
{
if (x > PlayerNumber)
cout << "Your number was lower than the computers." << endl;
else if (x < PlayerNumber)
cout << "Your number was higher than the computers" << endl;
else
{
cout << "You Won! nnThe computer's number was "<< x << endl;
win=true;
}
}
并称之为
result(PlayerNumber,x);
这个代码应该在哪里。
相关文章:
- 在我的小行星游戏中,我无法在x坐标上移动我的飞船
- 我需要静态转换我的数字模板值吗
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 在C++中,我的主机游戏角色在游戏场数组内移动时不会停止
- 为什么 rand() 给我的数字超出了我定义的间隔?(昨天工作正常)
- 为什么我的 UWP 游戏在发布时比在调试模式下慢
- 如何与他人共享我的 SFML 游戏而不会出错?
- 为什么我的 nim 游戏总是选择桩 A?
- 我的COCOS2D游戏开始,而不是其他音乐应用程序停止
- 如何使用运行时检查错误来修复我的C 格斗游戏
- 为什么我的TicTacToe游戏在生成随机移动时会卡住
- 我的简单游戏的计时器非常关闭...C++
- 为什么我的代码只检查我的刽子手游戏(C ++)中单词的第一个字母
- 为什么这段代码会给我的数字加一个额外的零
- 为什么我的 SFML 游戏只能在终端中运行时使用
- 对计算我的刽子手游戏分数背后的逻辑有问题
- C++完成了随机猜测我的数字游戏Lookover
- 为什么D3D10SDKLayers.dll在我的DX11游戏中加载
- 我应该用什么语言编写我的2D游戏?
- WPF世界编辑器为我的DirectX游戏引擎