【问题标题】:Dice game pig in C++C++ 中的骰子游戏猪
【发布时间】:2013-09-26 02:43:36
【问题描述】:

第一次发帖,请多多包涵。我对 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; 
            }                              
        }
    }
}

【问题讨论】:

  • 嗨,安德里亚,欢迎来到 SO。您的问题要求提供很多建议,而人们可能不想提供太多建议。当有一个明确的问题可以有一个明确的答案时,我们的工作效果最好,帮助人们自己学习很重要,这样我们就不仅仅是提供答案。这里有一些提示,看看你的代码,也许你可以从那时起更新你的代码。 1)为什么在魔鬼滚动后退出?你有一个循环给玩家一个回合,一个给魔鬼。你需要一个永远有效的外循环——然后如果用户退出或获胜,你就会返回。
  • 2) 确定获胜的最简单方法是将整个掷骰子放入函数中。函数非常适合删除重复代码,掷骰子和检查分数都非常适合。您可以制作处理掷骰子的函数和添加分数的函数。在添加分数的函数中,您可以检查分数是否为 100,如果是,则给出获胜消息并退出。祝你好运!

标签: c++ dice


【解决方案1】:

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

while(hasSomeoneWonYet==false){ 代码 }

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

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

如果 (winConditionsMet){ hasSomeoneWonYet=true;}

将符合条件的获胜条件替换为实际情况,应该会更好。

【讨论】:

    【解决方案2】:

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

    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

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      问题 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;
            }
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-29
        • 2015-08-25
        • 1970-01-01
        • 2020-08-02
        • 1970-01-01
        • 2015-02-03
        • 2013-10-14
        • 2012-11-27
        相关资源
        最近更新 更多