0

第一次在这里发海报,所以请多多包涵。我对 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; 
            }                              
        }
    }
}
4

3 回答 3

2

代码中的循环问题似乎发生了,因为整个代码没有循环,导致游戏只发生一次迭代。你应该把从玩家回合到魔鬼回合结束的代码放到一个循环中,条件是这样的

而(hasSomeoneWonYet==false){ 代码 }

这将保持轮流交替,直到有人WonYet 为真

至于如何检查获胜条件,我的做法是在每个玩家回合结束时写一个语句检查是否满足获胜条件。类似于以下内容:

if (winConditionsMet){ hasSomeoneWonYet=true;}

替换符合实际情况的获胜条件,它应该会更好地工作。

于 2013-09-25T18:28:25.003 回答
0

这里有一些伪代码可以帮助你:

turn = player
winner = false
rolled_1 = false
pass = false

while !winner

    if turn == player

        show the player options (roll, pass, or quit)

        respond to player's choice

    else if turn == computer
        logic for what computer does for its roll

        respond to computer's choice

    check for winner

    if winner
        announce winner
    else
        if rolled 1 or pass was chosen
            if turn == computer
                turn = player
            else
                turn = computer

(winner became true, so the while loop is exited, and the program finishes)

使用缩进知道大括号的放置位置。许多 IDE 都有某种“格式”或“缩进”助手。

此外,由于 turn 只有两个值,您可以将其更改为布尔值,例如is_computer_turn.

希望有帮助。

于 2013-09-25T18:29:10.893 回答
0

问题 1:您有两个循环控制每个玩家的回合,但它们没有包含在一个将重复回合直到满足条件(获胜条件)的外部循环中。

问题2:当掷出的数字+回合分数+总分数>=100时,您应该检查中奖情况。这种情况下,您可以简单地打印中奖语句并返回。

一个可能的解决方案是(我正在使用无限循环和中断,不是很优雅,但应该这样做):

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
  int die;
  int myScore = 0;
  int devilScore = 0;
  int turnScore = 0;
  char choice;
  bool change = false;

  bool weHaveAWinner = false;
  cout << "Welcome to Devil's Dice! Please select from the following menu: ";

    //Enclosing infinite loop, only can exit on return
    while (true) {
    //Player turn loop, can exit with breaks to main loop
    while (true){
      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;
          if (turnScore + myScore >=100) {
            cout << "You win!" << endl;
            //Winning condition met. Game over and return.
            return 0;
          }
          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;
          turnScore = 0;
          //End turn. Breaks secondary loop.
          break;
        }
      }

      if(choice == 'p')   {
        myScore += turnScore;
        cout << "Your score is now " << myScore << "." << endl;
        turnScore = 0;
        change = true;
        //End turn. Breaks secondary loop.
        break;
      }  //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 (true){
       //Devil's turn loop, can exit with breaks to main loop
      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;
            if (turnScore + devilScore >=100) {
              //Winning condition met. Game over and return.
              cout << "The Devil wins!" << endl;
              return 0;
            }
          }else{
            cout << "The Devil rolls a 1. He loses his points and now it's your turn!" << endl;
            turnScore = 0;
            change = false;
            //End turn. Breaks secondary loop.
            break;
          }

        }
      }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;
        //End turn. Breaks secondary loop.
        break;
      }
    }
  }
}
于 2013-09-25T18:37:52.103 回答