使用for循环来跟踪播放的轮次并将用户选择与计算机进行比较 [英] Using a for loop to keep track of rounds played and comparing user choice to computer
问题描述
背景:计算机角色在类中定义。所有输入/输出必须在主功能内部进行。另外,我为标记c ++ 98和14道歉。我不知道这是哪个版本。
< pre lang = c ++> #include< string>
#include< iostream>
using namespace std;
int ROUNDS;
int USER_CHOICE;
int SCORE;
class ComputersChoice {//包含AI播放器随机数函数的类
private:
int randNum = rand()%5 + 1;
public:
void SetNum(int value){//由于某些原因,程序在没有此参数的情况下运行缓慢,并将其设置为等于randNum
randNum = value;
}
int GetNum(){
return randNum;
}
void printResult(){
if(randNum == 1){
cout<<计算机选择摇滚。;
}
else if(randNum == 2){
cout<<计算机选择了纸张。;
}
否则如果(randNum == 3){
cout<<计算机选择了剪刀。;
}
else if(randNum == 4){
cout<<计算机选择了蜥蜴。;
}
else {
cout<<计算机选择了spock。;
}
}
};
int main(){
//用户想玩多少轮?
cout<<你想玩3轮,5轮还是7轮? << ENDL;
cin>> ROUNDS;
开关(ROUNDS){
案例3:
cout<<你选择最好的3轮。<< endl ;
休息;
案例5:
cout<<你选择了最好的5轮。<< endl;
休息;
案例7:
cout<<你选择了最好的7轮。<< endl;
休息;
默认值:
cout<<输入无效。输入3,5或7。<< endl;
休息;
}
//用户输入
cout<<输入1表示摇滚,2表示纸张,3表示剪刀,4表示蜥蜴或者5表示spock。<< endl;
cin>> USER_CHOICE;
开关(USER_CHOICE){
案例1:
cout<<你选择了摇滚乐。 << ENDL ;;
休息;
案例2:
cout<<你选择了纸张。 << ENDL;
休息;
案例3:
cout<<你选择了剪刀。 << ENDL;
休息;
案例4:
cout<<你选择了蜥蜴。 << ENDL;
休息;
案例5:
cout<<你选择了spock。 << ENDL;
休息;
默认值:
cout<<无效输入。输入1表示摇滚,2表示纸张,3表示剪刀,4表示蜥蜴或5表示spock。 << ENDL;
休息;
}
srand(time(NULL));
//对象创建
ComputersChoice randomNumber;
//获取随机数并相应地打印结果
randomNumber.GetNum();
randomNumber.printResult();
}
我尝试了什么:
通过循环,我尝试将整个switch语句包装在循环,在switch语句的外部,在case的上方或下方和内部放置一个循环。我完全迷失了如何将计算机的随机数与用户选择进行比较。随机数是在类中生成的,我不确定如何访问随机数。
首先,你需要先在第一个循环上开始用户选择:如果他们输入无效输入,那么它会抱怨,但让他们无论如何都要玩 - 这很糟糕。添加一个有效条目变量,并循环直到你得到一个。
其次,你需要第二个循环来实际游戏的用户输入。这一次,你循环用户选择的轮数:3,5或7.
我这样做的方法是抽象代码函数:从GetRounds函数开始:
int GetRounds()
{
bool isValid = false ;
int numberRounds = 0 ;
while (!isValid)
{
cout<< 你想玩3轮,5轮还是7轮?<< ENDL;
cin>> numberRounds;
switch (numberRounds)
{
case 3 :
case 5 :
case 7 :
isValid = true < /跨度>;
break ;
默认:
cout<< 输入无效。输入3,5或7。<< ENDL;
break ;
}
}
cout<< 你选择了最好的<< numberRounds<< rounds。<< endl;
return numberRounds;
}
然后从你的Main函数调用它。
添加第二个PlayRound函数 - 你可以自己编写一个 - 并使用for
循环来调用用户选择的次数。
让PlayRounds函数返回1以获得玩家获胜, - 1表示计算机获胜,0表示平局,然后将每个结果加起来。最后,正面总数是玩家赢得会议,负面是计算机会议的胜利。
首先尝试,然后看看你得到多远。
Background: The computers role is defined in the class. All input/output must take place inside the main function. Also, I apologize for tagging c++98 and 14. I don't know which version this is.
<pre lang="c++">#include <string> #include <iostream> using namespace std; int ROUNDS; int USER_CHOICE; int SCORE; class ComputersChoice { //Class that contains the random number function for the AI player private: int randNum = rand() % 5 + 1; public: void SetNum(int value) { //Program runs slow for some reason without this parameter and setting it equal to randNum randNum = value; } int GetNum() { return randNum; } void printResult() { if (randNum == 1) { cout <<"The computer chose rock."; } else if (randNum == 2) { cout<<"The computer chose paper."; } else if (randNum == 3) { cout<<"The computer chose scissors."; } else if (randNum == 4) { cout<<"The computer chose lizard."; } else { cout<<"The computer chose spock."; } } }; int main() { //How many rounds does the user want to play? cout<<"Do you want to play 3, 5 or 7 rounds?" << endl; cin>>ROUNDS; switch(ROUNDS) { case 3: cout<<"You picked best of 3 rounds."<<endl; break; case 5: cout<<"You picked best of 5 rounds."<<endl; break; case 7: cout<<"You picked best of 7 rounds."<<endl; break; default: cout<<"Invalid input. Enter 3, 5 or 7."<<endl; break; } //User Input cout<<"Enter 1 for rock, 2 for paper, 3 for scissors, 4 for lizard or 5 for spock."<<endl; cin>>USER_CHOICE; switch(USER_CHOICE) { case 1: cout<<"You chose rock." << endl;; break; case 2: cout<<"You chose paper." << endl; break; case 3: cout<<"You chose scissors." << endl; break; case 4: cout<<"You chose lizard." << endl; break; case 5: cout<<"You chose spock." << endl; break; default: cout<<"Invalid input. Enter 1 for rock, 2 for paper, 3 for scissors, 4 for lizard or 5 for spock." << endl; break; } srand(time(NULL)); //Object creation ComputersChoice randomNumber; //Fetches the random number and prints results accordingly randomNumber.GetNum(); randomNumber.printResult(); }
What I have tried:
With the loop I have tried wrapping the entire switch statement in a loop, putting a loop on the outside of the switch statement above or below and inside the cases. I am completely lost on how to compare the random number of the computer to the user choice. The fact the random number is generated in a class, I am unsure how to access the random number.
First off, you need to start by looping on the first user choice: if they enter an "Invalid input" then it complains, but lets them play anyway - that's bad. Add a "valid entry" variable, and loop until you get one.
Second, you need a second loop for the user input of the actual game. This time, you loop for the number of rounds the user selected: 3, 5, or 7.
The way I'd do it, is to abstract the code into functions: start with a GetRounds function:
int GetRounds() { bool isValid = false; int numberRounds = 0; while (!isValid) { cout << "Do you want to play 3, 5 or 7 rounds?" << endl; cin >> numberRounds; switch(numberRounds) { case 3: case 5: case 7: isValid = true; break; default: cout << "Invalid input. Enter 3, 5 or 7." << endl; break; } } cout<<"You picked best of " << numberRounds << " rounds."<<endl; return numberRounds; }
Then call that from your Main function.
Add a second "PlayRound" function - you can write that one yourself - and use afor
loop to call it the number of times that the user selected.
Make the PlayRounds function return 1 for player win, -1 for computer win, and 0 for a draw, then add up each result. At the end, a positive total is a player win for the session, negative is a computer session victory.
Start by trying that, and see how far you get.
这篇关于使用for循环来跟踪播放的轮次并将用户选择与计算机进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!