一个用c++编写的简单游戏循环程序
C++ simple game using a Loop program
这个程序将与用户玩一个叫做几率和偶数的游戏。计算机将玩偶数,而人类用户将玩奇数。对于一轮游戏,每个玩家在[1,10]范围内选择一个整数。玩家独立选择自己的号码:在选择自己的号码之前,任何玩家都不知道对方的号码。如果数字的和是偶数,那么偶数(计算机)赢了这一轮;如果数字的总和是奇数,那么赔率(人类)赢了这一轮。玩家想玩多少回合就玩多少回合;用户通过输入非#或[1,10]之外的数字来结束游戏。在游戏结束时,程序总结比分。
我在正确地循环这个问题时有困难。随机分配pc选择的数字是行不通的,因为pc在游戏的每一轮中都会选择相同的数字。我也不知道我该如何让程序总结分数。帮助将是非常感激的,因为我有另一个问题的家庭作业,是类似的!下面是我的代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
bool die(const string & msg);
int main(){
srand(static_cast<unsigned>(time(0)));
unsigned num1 = 0, num = 0, sum = 0;
bool userTurn = true;
cout << "Welcome to the Odds and Evens game!";
num = rand() % 10 + 1;
while (num){
if (userTurn){
cout << " Your # in [1,10] is ";
cin >> num1;
}
else {
cout << "My number is " << num;
sum = num1 + num;
if (sum % 2 == 0){
cout << " I win!";
}
else {
cout << " You win!";
}
}
userTurn = !userTurn;
}
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
随机选择pc选择的数字是行不通的,因为pc在游戏的每一轮中都选择相同的数字。
当轮到计算机时,您没有代码来重新设置num
的值。
在
行之后userTurn = !userTurn;
添加if ( !userTurn )
{
num = rand() % 10 + 1;
}
我也不知道如何让程序总结分数。
保留两个计数器,分别表示人类赢了多少次和计算机赢了多少次。
int computerWinCount = 0;
int humanWinCount = 0;
,然后更新循环使用:
if (sum % 2 == 0){
cout << " I win!";
++computerWinCount;
}
else {
cout << " You win!";
++humanWinCount;
}
while
循环的条件使得您的程序永远不会终止。将其更新为如下所示:
while (true) {
if (userTurn){
cout << " Your # in [1,10] is ";
cin >> num1;
// If the user entered a number that is not
// within range or the user did not input a number,
// then break out of the loop.
if ( !cin || num1 < 1 || num1 > 10 )
{
break;
}
}
else {
cout << "My number is " << num;
sum = num1 + num;
if (sum % 2 == 0){
cout << " I win!" << endl;
++computerWinCount;
}
else {
cout << " You win!" << endl;
++humanWinCount;
}
}
userTurn = !userTurn;
if ( !userTurn )
{
num = rand() % 10 + 1;
}
}
要报告摘要,在main
结束前添加以下行。
cout << "Number of times I won: " << computerWinCount << endl;
cout << "Number of times you won: " << humanWinCount << endl;
此处:
num = rand() % 10 + 1;
while (num){
... // never change num
}
你看到问题了吗?电脑玩家随机选择num
,但只能选择一次。在主循环中再放一个num = rand() % 10 + 1;
。
(另外,你似乎没有办法让用户终止游戏。)
所以你需要一个简单的循环来做以下事情:
- 获取用户输入。
- 获取计算机输入
- 查看当前回合谁赢了
- 更新分数。
直到用户选择的选项不是1到10
在此之后,您想要显示分数。
下面是一个完整的例子。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int mynum, compNum, myScore(0), compScore(0);
srand(time(NULL));
cout << "Welcome to the Odds and Evens game!" << endl;
cout << "Your # in [1,10] is ";
while ((cin >> mynum) && mynum > 0 && mynum <= 10){
compNum = rand()%10 + 1;
if ((mynum + compNum)%2){
cout << "You win" << endl;
++myScore;
} else {
cout << "Computer Wins" << endl;
++compScore;
}
cout << "Your # in [1,10] is ";
}
cout << "You won " << myScore << " games" << endl;
cout << "The computer won " << compScore << " games" << endl;
return 0;
}
你的计算机数字没有改变的问题是由于你没有在循环中更新它的值。
如果你想跟踪分数,你可以简单地保留两个整数,记录用户赢了多少次,计算机赢了多少次。然后在最后(while循环之后)计算他们的每个分数。
总的来说,你的代码非常接近。你只需要确保你在while循环中更新了计算机的猜测,当你决定谁赢了这一轮时,增加那个人的分数。
原始代码中的整个循环条件将始终求值为true。因为num总是1到10的整数。您需要在while循环条件中使用用户的输入。
我的代码中的while条件将执行以下操作:获取用户的输入。如果Cin读取数字失败,则Cin>> mynum将求值为false。如果它读了一个数字,条件将检查这个数字是否在1到10之间。
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 简单的游戏数据库问题
- X 变量在最后一次尝试简单猜谜游戏时发生变化,C++
- 简单的比赛游戏与 C++ 中的动态数组
- 简单的游戏,同时循环和检查字符串
- 生活的游戏邻居检查(更简单的方法可能吗?)
- 如何简单地管理我的游戏状态?C++
- 循环麻烦.C 简单游戏
- 简单的单词猜测游戏
- 我简单的 c++ 文本游戏不起作用。我正在使用随机数生成,我很困惑
- OpenGL - 简单3D游戏的低FPS
- 简单的战舰游戏建议
- 我需要哪种语言/课程来制作简单的纸牌游戏
- 如何创建一个简单的侧滚动游戏
- 为什么c++游戏与unity3d相比如此简单
- 变量的值在使用C++的简单文本 RPG 游戏中不会更改
- 我的简单游戏的计时器非常关闭...C++
- 组织X11和Win32简单游戏代码的最佳方式是什么
- 如何得到每个子弹的起点?Libgdx简单游戏
- 一个用c++编写的简单游戏循环程序