[英]C++ simple game using a Loop program
該程序將與用戶玩一個稱為“賠率和賠率”的游戲。 電腦將播放“偶數”,而人類用戶將播放“奇數”。 對於游戲的每一回合,每個玩家都選擇[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);
}
由於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之間(含1和10)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.