基本C ++骰子游戏

时间:2013-10-07 12:25:38

标签: c++ c++11 dice

我在这里有一个问题,如果有人可以帮助我在这里会很好。这是我第一次使用这个程序,所以不要去评判。

#include <cstdlib>
#include <iostream>

using namespace std;
int throw1, throw2, throw3, throw4;
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3);
int deposit;
int account;

int main(){
    int count = 0;

    while(count < 3){

        cin>>deposit;

        while(deposit>5000 || deposit<0){                //Makes sure so that my deposit is between 0-5000
            cout<<"deposit failed"<<endl;
            cin>>deposit;

        }
        account = deposit;
        cout<<"You have deposited" <<deposit<<"Swedish Krona"<<endl;   
        cout<<"and you have this much cash on your account"<<account<<"Swedish Krona"<<endl;

        if (konto>499){                 //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more 
            cout<<"please place your bet"<<endl;
            cout<<"bet1=100, bet2=300, bet3=500"<<endl;
            cin>>bet1;
            cin>>bet2;
            cin>>bet3;
            account = (deposit - bet);
            cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
        }
        else if(account>299){
            cout<<"please place your bet"<<endl;
            cout<<"bet1=100, bet=300"<<endl;     
            cin>>bet1;
            cin>>bet2;
            account =(deposit - bet);
            cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
        }
        else if(account>99){
            cout<<"please place your bet"<<endl;
            cout<<"bet1=100"<<endl;
            cin>>bet1;
            cout<<"you have placed your bet"<<bet<<"Swedish Krona"<<endl;
        }

        while (account<100 || deposit>5000){
            cout<<"insufficient funds"<<endl;
            cin>>deposit;
            account=deposit;
        }

        {
            cout<<"Throw dice"<<endl;
            srand(time(0)); 
            Throw1 = rand() % 6 + 1;
            Throw2 = rand() % 6 + 1;
            Throw3 = rand() % 6 + 1;
            Throw4 = rand() % 6 + 1;
            cout<<"You rolled"<<Throw1<<endl;
            cout<<"You rolled"<<Throw2<<endl;
            cout<<"Computer rolled"<<Throw3<<endl;
            cout<<"Computer rolled"<<Throw4<<endl;
        }
    }
    count++;

    system ("pause");
}

所以这里的事情是,由于某种原因,我总是下注500,即使输入bet1或bet2,我也不知道如何解决这个问题。然后我的循环函数(int count 0; while(count < 3)count++)它开始循环而没有我按任何东西,即使我在简单的编码中使用相同的循环函数,只需键入一些cout&lt;&lt;它运作良好的东西,但当我在这个代码中使用它,它会流失,有人知道为什么会发生这种情况,如果有人能够回答,将会很感激,谢谢先进。

4 个答案:

答案 0 :(得分:9)

int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3)

最后一行将按如下方式进行评估:100,300,500。逗号分隔表达式列表的结果将是最后一个值,即500.因此,您的投注变量将始终设置为500.

答案 1 :(得分:5)

您在代码下方的评论中说明的内容(int count 0; while(count < 3)count++)看起来像forwhile循环的奇怪混合。请再次检查您的C ++教科书/在线教程,了解如何编写正确的循环。

在您显示的代码中,在while循环中,您不会修改count变量 - 因此如果count为&lt;它将永远循环。循环前3。您的代码缩进确实具有误导性。我冒昧地重新格式化您的代码 - 现在您应该看到count++语句实际上超出了您的主while循环!

当你想要做一些固定次数的事情时,建议使用for循环,这会让忘记增量更难!

答案 2 :(得分:3)

你在之外增加count 循环,因此总是为零。要么在循环中移动它(正确的缩进是关键!)或者可能使用for循环代替:

for (count = 0; count < 3; ++count) { ... }

答案 3 :(得分:0)

一些建议,

  • 将您的存款提示(insattning)放入函数
  • 将您的提示下注到功能
  • 在提示下注之前检查是否有足够的钱
  • 输入一个字符串,然后验证输入(尚未在下面完成)
  • 检查下注是否有效(= 100,= 300,= 500,下注&lt; = konto)

以下是这些便利功能,

#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;
int kast1, kast2, kast3, kast4;
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=0; //assignment didn't make sense
int insattning=0;
int konto=0;

//deposit
int get_insattning()
{
    int good = 0;
    while( !good )
    {
        cout<<"deposit"<<endl; //prompt for deposit
        cin>>insattning;
        if(insattning>5000 || insattning<0)//Makes sure so that my deposit is between 0-5000
        {
            cout<<"insattning fel, var vänlig och gör rätt denna gången"<<endl;
        }
        else good = 1;
    }
    cout<<"du har nu satt in" <<insattning<<"kr"<<endl;
    return insattning;
}

我不清楚你是想要下注100,300,或500或3个投注。这是第一个,

//bet
int get_bet()
{
    int good = 0;
    int bet;
    std::string validbets = "";
    if(konto<100){ cout<<"you need more money"; return 0; }
    while( !good )
    {
        cout<<"var vänlig och placera ditt bet"<<endl;
        if(konto>=100){ validbets = "bet1=100"; }
        if(konto>=300){ validbets += ", bet=300"; }
        if(konto>=500){ validbets += ", bet=500"; }
        cout<<validbets<<endl;
        cin>>bet;
        if( bet >= konto ) {
            cout<<"you don't have enough money"<<endl;
            continue;
        }
        if (bet==500){                 //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more 
            cout<<"du har så här mycket på kontot nu "<<konto<<" kr"<<endl;
            good = 1;
        }
        else if(bet==300){
            cout<<"du har så mycket på kontot nu "<<konto<<" kr"<<endl;
            good = 1;
        }
        else if(bet==100){
            cout<<"du har nu bettat "<<bet<<" kr"<<endl;
            good = 1;
        }
        else {
            cout<<"you must place valid bet"<<endl;
            continue;
        }
    }
    return bet;
}

现在您的主要游戏玩法更清晰/更容易阅读。我不知道胜利条件是什么或支付,并且由于你的提示不是英语,我不能阅读它们来告诉下一步该怎么做,

int main()
{
    int count = 0;
    int bet;
    srand(time(0));
    for( count=0; (count < 3); count++)
    {
        konto = get_insattning();
        if (konto<100)
        {
            cout<<"du har inte nog med pengar, vänligen sätt in pengar"<<endl;
            continue;
        }
        cout<<"och du har så här mycket i ditt konto "<<konto<<" kr"<<endl;
        bet = get_bet();
        //when you bet, reduce konto by bet
        konto = (konto - bet);
        {
            cout<<"slå tärningar"<<endl;
            kast1 = rand() % 6 + 1;
            kast2 = rand() % 6 + 1;
            kast3 = rand() % 6 + 1;
            kast4 = rand() % 6 + 1;
            cout<<"Du fick"<<kast1<<endl;
            cout<<"du fick"<<kast2<<endl;
            cout<<"datorn fick"<<kast3<<endl;
            cout<<"datorn fick"<<kast4<<endl;
        }

您需要编写代码以确定您是赢还是输,然后在获胜时添加到konto,

        //did you win or lose?
        //win?  add money to konto
        //lose?  you have already deducted from konto
    }
    system ("pause");
}

这些建议可以帮助您修复程序。