[使用Loop程序的C ++简单游戏

问题描述 投票:-1回答:4

[此程序将与用户玩一个称为“赔率和赔率”的游戏。电脑将播放“偶数”,而人类用户将播放“奇数”。对于游戏的每一回合,每个玩家都选择[1,10]范围内的整数。玩家独立选择号码:两个玩家在选择自己的号码之前都不知道对方的号码。如果数字的总和为偶数,则Evens(计算机)赢得该回合;否则,该数字为0。如果数字的总和是奇数,则赔率(人类)赢得该回合。游戏可以持续进行多达用户想要玩的回合。用户通过在输入[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);
}
c++ loops for-loop
4个回答
1
投票

随机分配个人计算机选择的号码不起作用,因为在游戏中每一轮个人计算机选择相同的号码。

轮到计算机时,您没有代码可以重置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;

0
投票

这里:

num = rand() % 10 + 1;

while (num){
  ...  // never change num
}

您看到问题了吗?电脑播放器随机选择num,但只能选择一次。只需在主循环中放入另一个num = rand() % 10 + 1;

((而且,您似乎没有办法让用户终止游戏。)


0
投票

因此,您需要一个简单的循环来执行以下操作。

  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之间(包括1和10)。


0
投票

[玩游戏时如何将结果放入计算机?还有每个游戏的结果,例如,当人类选择奇数为5,计算机选择偶数为8时,我想要一个结果,并且该游戏将发布随机结果。.你们可以如何输入?

© www.soinside.com 2019 - 2024. All rights reserved.