第一次在这里发海报,所以请多多包涵。我对 C++ 也很陌生。我正在根据名为 Pig 的骰子游戏编写这个游戏。它接近正常工作,但我遇到了一些问题。游戏的重点是达到 100 分,但我不知道如何编写代码。我尝试了几种方法,例如 while 循环或 if 语句,但它们都没有奏效。我想让它说,只要掷出获胜的骰子,谁就会获胜。最好的方法是什么?
我也遇到了正确循环的问题。它应该在计算机通过后回到玩家的循环,但它只想退出。任何帮助表示赞赏!
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int die;
int myScore = 95;
int devilScore = 95;
int turnScore = 0;
int myScoretotal = myScore += turnScore;
int devilScoretotal = devilScore += turnScore;
char choice;
bool change = false;
cout << "Welcome to Devil's Dice! Please select from the following menu: ";
while(change == false){ //&& devilScoretotal < 100 && myScoretotal < 100){
cout << "\nRoll [r], Pass [p], or Quit [q].";
cin >> choice;
if(choice == 'r'){
die=(rand() % 6 + 1); //Number generator for die
if(die > 1){
cout << "You rolled a " << die << "." << endl;
turnScore += die;
cout << "You will add " << turnScore << " points if you pass now. ";}
else{
cout << "You rolled a 1. You lose your points and your turn ends." << endl;
change = true;
turnScore = 0;
}
}
if(choice == 'p')
{myScore += turnScore;
cout << "Your score is now " << myScore << "." << endl;
turnScore = 0;
change = true; } //needs to go to Devil now
if(choice == 'q')
{cout << "\n\tThanks for playing! ";
return 0; }
else if(choice > 'r' || choice < 'p')
{cout << "Please select from the choices listed."; }
}
while(change == true){// && devilScoretotal < 100 && myScoretotal < 100){ //The Devil's actions
if(myScore > devilScore && turnScore < 17 || devilScore > myScore && turnScore < 12 || devilScore > 84){
choice='r';
cout << "The Devil rolls! " << endl;
if(choice == 'r'){
die=(rand() % 6 + 1); //Number generator for die
if(die > 1){
cout << "The Devil rolls a " << die << "." << endl;
turnScore += die;
}else{
cout << "The Devil rolls a 1. He loses his points and now it's your turn!" << endl;
turnScore = 0;
change = false;
}
}else{
cout << "The Devil chooses to pass. ";
devilScore += turnScore;
choice='p';
turnScore = 0;
cout << "He has " << devilScore << " points. " << endl;
cout << "The Devil now has " << devilScore << " points." << endl;
change = false;
}
}
}
}