一个用c++编写的简单游戏循环程序

C++ simple game using a Loop program

本文关键字:简单 游戏 程序 循环 c++ 一个      更新时间:2023-10-16

这个程序将与用户玩一个叫做几率和偶数的游戏。计算机将玩偶数,而人类用户将玩奇数。对于一轮游戏,每个玩家在[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. 获取用户输入。
  2. 获取计算机输入
  3. 查看当前回合谁赢了
  4. 更新分数。

直到用户选择的选项不是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之间。