2016-05-05 7 views
0

Hey Folksこのプログラムの動作に問題があります。私は今まで何をしてきたのかをプログラムで編集しましたが、オプション2を渡すのをやめようとすると、ゲームが失われてしまい、ダイスが表示されません。それを動作させる方法上の任意の提案や、よりきれいに私のプログラムの表示を行う上の任意のヒントは、コードは、いくつかの構文エラーといくつかの論理エラーがありクラップス・プログラムの作成

/************* 
Program: 
Author: 
Description: 
Last Modified: 
**************/ 

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <cmath> 
#include <ctime> 
#include <stdlib.h> 


using namespace std; 

void getBet(int pot, int& b); 
char menu(); 
bool playGame(); 
int diceRoll(); 
void displayDice(int dice); 
void rules(); 
bool winner(int pass, int dontPass); 


int main() 
{ 
// Declare variables below here 
char choice; 
char pick = ' '; 
int d1 = 0, d2 = 0, total, bet, pass, dontPass; 
int pot = 100; 

bool winner; 

srand(time(NULL)); 

cout << setiosflags(ios::fixed | ios::showpoint); 

choice = menu(); 

while (choice != 'q' || choice != 'Q') 
{ 
    if (choice == 'r' || choice == 'R') 
    { 
     rules(); 
     choice = menu(); 
    } 

    if (choice == 'q' || choice == 'Q') 
    { 
     return 0; 
    } 
    if (choice == 'p' || choice == 'P') 
    { 
     // Begin your algorithm here 

     getBet(pot, bet); 

     while (bet > 0) 
     { 

      do 
      { 

       { 
        playGame(); 
       } 
      } while (choice == 'q' || choice == 'Q'); 

      if (winner = false) 
      { 
       cout << "You win!\n\n"; 
       pot += bet; 
      } 
      else 
      { 
       cout << "You lose!\n\n"; 
       pot -= bet; 
      } 

      getBet(pot, bet); 


     } 



    } 
} 
} 

char menu() 
{ 
char choice = ' '; 

cout << "Welcome to Craps!!" << endl << endl; 

cout << "Enter P to Play Craps!" << endl << endl; 

cout << "Enter R to Read The Rules!" << endl << endl; 

cout << "Enter Q to Quit the Program" << endl << endl; 

cout << "Enter The Letter Of Your Choice: "; 

cin >> choice; 

return choice; 
} 

int diceRoll() 
{ 

int d1 = 0, d2 = 0; 

int total = 0; 

d1 = 1 + rand() % 6; 

displayDice(d1); 

d2 = 1 + rand() % 6; 

displayDice(d2); 

total = d1 + d2; 

cout << "Player rolls " << d1 << " and " << d2 << " for a total of " << total << endl; 


return total; 


} 

void displayDice(int dice) 
{ 
if (dice == 1) 
{ 
    cout << "-------------" << endl; 
    cout << "-   -" << endl; 
    cout << "-  *  -" << endl; 
    cout << "-   -" << endl; 
    cout << "-------------" << endl; 
} 
if (dice == 2) 
{ 
    cout << "-------------" << endl; 
    cout << "- *  -" << endl; 
    cout << "-   -" << endl; 
    cout << "-  * -" << endl; 
    cout << "-------------" << endl; 
} 
if (dice == 3) 
{ 
    cout << "-------------" << endl; 
    cout << "- *  -" << endl; 
    cout << "-  *  -" << endl; 
    cout << "-  * -" << endl; 
    cout << "-------------" << endl; 
} 
if (dice == 4) 
{ 
    cout << "-------------" << endl; 
    cout << "- *  * -" << endl; 
    cout << "-   -" << endl; 
    cout << "- *  * -" << endl; 
    cout << "-------------" << endl; 
} 
if (dice == 5) 
{ 
    cout << "-------------" << endl; 
    cout << "- *  * -" << endl; 
    cout << "-  *  -" << endl; 
    cout << "- *  * -" << endl; 
    cout << "-------------" << endl; 
} 
if (dice == 6) 
{ 
    cout << "-------------" << endl; 
    cout << "- *  * -" << endl; 
    cout << "- *  * -" << endl; 
    cout << "- *  * -" << endl; 
    cout << "-------------" << endl; 
} 

} 

void getBet(int p, int& b) 
{ 
cout << "You have $" << p << " Enter your bet: (0 to stop) $"; 
cin >> b; 
while (b > p || b < 0) 
{ 
    cout << "Error: must enter a bet between 0 and " << p << " Enter your bet: $"; 
    cin >> b; 
} 
} 

bool winner(int pass, int dontPass) 
{ 
if (pass == 1) 
    return true; 
else if (dontPass == 1) 
    return false; 

} 

bool playGame() 
{ 
int firstRoll = 0, roll = 0, dontPass = 0, pass = 0, point = 0; 

bool win = false; 

cout << "Welcome to Craps Would You like to Pass or Don't Pass?" << endl; 
cout << "Enter 1 to Pass" << endl; 
cout << "Enter 2 to Don't Pass" << endl; 
cout << "What is Your choice?: "; 
cin >> pass; 



system("CLS"); 

if (pass == 1) 
{ 
    firstRoll = diceRoll(); 

    if (firstRoll == 7 || firstRoll == 11) 
    { 
     cout << "Pass Wins You Rolled A:" << firstRoll << endl; 
     pass = true; 
     return pass; 
    } 
    else if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12) 
    { 
     cout << "Pass Loses You Rolled A:" << firstRoll << endl; 
     dontPass = false; 
     return false; 
    } 


    while (win == false) 
    { 
     system("PAUSE"); 

     system("CLS"); 

     point = firstRoll; 
     cout << "Point Is : " << point << endl; 
     roll = diceRoll(); 



     if (roll == point) 
     { 
      cout << "Pass Wins!! You Rolled A:" << point << endl; 
      pass = true; 
      return pass; 
     } 

     else if (roll == 7) 
     { 
      cout << "Pass Loses!! You Rolled A:" << point << endl; 
      dontPass = false; 
      return false; 
     } 

    } 
    if (pass == 2) 
    { 
     firstRoll = diceRoll(); 

     if (firstRoll == 7 || firstRoll == 11) 
     { 
      cout << "DontPass Loses You Rolled A:" << firstRoll << endl; 
      dontPass = false; 
      return false; 
     } 
     else if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12) 
     { 
      cout << "DontPass Wins You Rolled A:" << firstRoll << endl; 
      pass = true; 
      return pass; 
     } 


     while (win == false) 
     { 
      system("PAUSE"); 

      system("CLS"); 
      point = firstRoll; 
      cout << "Point Is : " << point << endl; 
      roll = diceRoll(); 

      if (roll == point) 
      { 
       cout << "DontPass Loses!! You Rolled A:" << point << endl; 
       dontPass = false; 
       return false; 
      } 

      else if (roll == 7) 
      { 
       cout << "DontPass Wins!! You Rolled A:" << point << endl; 
       pass = true; 
       return pass; 
      } 
     } 

    } 


} 
} 


void rules() 
{ 
system("CLS"); 

cout << "Simplified Rules of Craps Craps is played with a set of 2 dice." << endl; 
cout << "\nThere are 2 outcomes(or 'lines') to a round : pass or don't pass." << endl; 
cout << "\nThe first roll of a round is called the 'Come Out' roll." << endl; 
cout << "\nOn the come out roll, if the roll is a 7 or 11, the pass line wins." << endl; 
cout << "\nIf the come out roll is a 2, 3, or 12, the don't pass line wins (this is called 'Craps')." << endl; 
cout << "\nIf none of the numbers above are rolled, then whatever was rolled is now called the 'point', and the round enters the point phase." << endl; 
cout << "\nRolls continue until either the point is rolled(pass), or a 7 is rolled(don't pass)." << endl; 

system("PAUSE"); 

system("CLS"); 


} 

答えて

0

いただければ幸いです。

これを修正するには、以下を実行してください。

  1. セミコロン(;)を削除最初しばらく、

    しばらく(選択= 'Q' ||選択= 'Q'!!)の後に

  2. をメイン関数を閉じます:追加'}' before char menu()関数。

  3. charオプションを宣言します。メニュー()関数で。

  4. すべてのwhileループをdisplayDice(int dice)関数のif文に変換します。

ありがとう!!!

+0

THanks私はそれを編集しました。これでルールが表示されますが、どのオプションを選んでも問題ありません。 –

+1

プログラムをデバッグし、原因を調べてみてください。デバッグがなければ、障害を発見するのは難しいです。 –

+0

私は多くの変更を加えましたが、プログラムはちょうど私がそれを望む方法ではなく実行されます。申し訳ありませんが、デバッグの解釈にはあまり良くありません。私はプログラムを編集し、修正しようとしているものを変更しました。 –

関連する問題