繁体   English   中英

骰子游戏猪用C ++编写

[英]Dice game pig in C++

第一次在这里发布海报,请耐心等待。 我也是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; 
            }                              
        }
    }
}

代码中的循环问题似乎发生是因为整个代码没有循环,导致只发生一次游戏迭代。 你应该把玩家的代码转到魔鬼的结尾,进入一个像这样的条件的循环

while(hasSomeoneWonYet == false){code}

这将保持转弯交替,直到有人有一个是真的

至于如何检查胜利条件,我这样做的方法是在每个玩家回合结束时发出声明,检查是否已经满足胜利条件。 类似以下内容:

if(winConditionsMet){hasSomeoneWonYet = true;}

取代满足实际条件的胜利条件,它应该更好。

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

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都有某种“格式”或“缩进”帮助器。

此外,因为转弯只有两个值,您可以将其更改为布尔值,例如is_computer_turn

希望有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM