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

コード全体がループせず、ゲームの反復が 1 回しか発生しないため、コードのループの問題が発生しているようです。プレイヤーのターンから悪魔のターンの終わりまでのコードを、このような条件でループに入れる必要があります

while(hasSomeoneWonYet==false){ コード }

これにより、someoneWonYet が true になるまで順番が交互に繰り返されます。

勝利条件の確認方法については、各プレイヤーのターン終了時に勝利条件を満たしているかどうかを確認するステートメントを入れる方法です。次のようなもの:

if (winConditionsMet){ hasSomeoneWonYet=true;}

勝利条件を実際の条件に置き換えると、よりうまく機能するはずです。

于 2013-09-25T18:28:25.003 に答える
0

問題 1: 各プレイヤーのターンを制御する 2 つのループがありますが、条件が満たされる (勝利条件) までターンを繰り返す外側のループに含まれていません。

問題 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 に答える
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 は 2 つの値しかないため、 などのブール値に変更できますis_computer_turn

それが役立つことを願っています。

于 2013-09-25T18:29:10.893 に答える