写一个掷骰子计划

时间:2016-05-05 02:54:57

标签: visual-c++

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


}

1 个答案:

答案 0 :(得分:0)

代码有一些语法错误和一些逻辑错误。

请执行以下操作以解决问题。

  1. 首先删除分号(;),

    while(选择!=&#39; q&#39; ||选择!=&#39; Q&#39;)

  2. 关闭主要功能:添加&#39;}&#39;在char menu()函数之前。

  3. 声明字符选择;在menu()函数中。

  4. 将所有while循环转换为displayDice(int dice)函数中的if语句。

  5. 谢谢!!!