2016-05-05 8 views
0

Hey Leute, ich habe Probleme damit, dieses Programm funktionieren zu lassen. Ich habe das Programm auf das geändert, was ich bisher getan habe, aber die meisten Sachen funktionieren, aber wenn ich mich entscheide, nicht zu gehen, was Option 2 ist, verliere ich die Spiele und es zeigt nicht die Würfel an. Alle Vorschläge, wie es funktionieren würde und alle Tipps, um mehr zu machen mein Programmanzeige schönEin Craps-Programm schreiben

/************* 
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"); 


} 

Antwort

0

Der Code hat einige Syntaxfehler und einige logische Fehler erkannt werden.

Bitte tun Sie Folgendes, um es zu beheben.

  1. das Semikolon entfernen (;) nach der ersten Zeit,

    while (! Wahl = 'q' || Wahl = 'Q')

  2. die Hauptfunktion schließen: add a '}' vor char menu() -Funktion.

  3. Char-Auswahl deklarieren; in Menü() Funktion.

  4. Konvertieren Sie alle while-Schleifen in if-Anweisungen in der Funktion displayDice (int dice).

Vielen Dank !!!

+0

THanks Ich habe es bearbeitet und es zeigt jetzt die Regeln, aber das ist es egal, welche Option ich wähle. –

+1

Bitte debuggen Sie das Programm und versuchen Sie die Ursachen herauszufinden. Ohne Debugging ist es schwierig, die Fehler zu entdecken. –

+0

Ich habe viele Änderungen vorgenommen und das Programm läuft einfach nicht so wie ich es will. Tut mir leid, ich bin nicht sehr gut darin, das Debug zu interpretieren. Ich habe das Programm bearbeitet und geändert, was ich suche, um zu beheben. –