首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果骰子落在某个数字上,掷骰子程序不会将我的分数重置为0

如果骰子落在某个数字上,掷骰子程序不会将我的分数重置为0
EN

Stack Overflow用户
提问于 2019-02-11 06:51:44
回答 1查看 53关注 0票数 0

我几乎已经完成了我的程序,它运行得很好,但是当我同时为玩家和AI滚动,并滚动1(半身数)时,它没有设置我的pot = 0。它只会保持运行编号并继续运行。这里有没有我不明白的逻辑?PS -它不让我发布我的全部代码,所以我只发布了部分代码

代码语言:javascript
复制
//AI
int aiTurn(int AI) {
    //Variables
    int pot = 0;
    int count = 0;
    char choice = ' ';
    cout << "AI turn" << endl;
    //While loop until AI turn reaches 20 or score >=50
    while (count < 20 && AI < 50) {
        int rollValue = diceRoll();
        //Incrementing turn
        count++;
        //Checking for bust
        if (rollValue == 1) {
            cout << "Die Roll " << rollValue << " : BUST" << endl << endl;
            pot = 0;
            return AI;
        }
        //Else increment 
        else {
            pot += rollValue;
            cout << "Die Roll : " << rollValue << "   Pot : " << pot << endl;
            AI += rollValue;
        }
    }
    return AI;
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-11 07:43:41

我的(count < 20 && AI < 50)是错误的,对于你所指定的规格,它应该是((count < 20) || (AI < 50))。请参考下面的代码进行进一步的修改。

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <cstdlib>

using std::cout;
using std::endl;

int diceRoll() { return rand() % 6 + 1; }

int aiTurn(int AI) {
    int pot = 0;
    int AI_value_to_be_returned = 0;
    //char choice;
    cout << "AI turn\n";
    //While loop until AI turn reaches 20 or score >=50

    int count = 0;
    while ((count < 20) || (AI < 50)) {
        int rollValue = diceRoll();
    printf("dice roll {%d}\n", rollValue);
        //Incrementing turn

        //Checking for bust
        if (rollValue == 1) {
            cout << "Die Roll " << rollValue << " : BUST" << endl << endl;
            pot = 0;
        AI_value_to_be_returned = AI;
        cout << "Die Roll " << rollValue << " : BUST Pot Value = " << pot << endl;
        break;
        } else {
            pot += rollValue;
            cout << "Die Roll : " << rollValue << "   Pot : " << pot << endl;
            AI += rollValue;
        }
    count++;
    }
    return AI_value_to_be_returned;
}

int main() { printf("aiTurn {%d}\n", aiTurn(50)); return 0; }

输出:

代码语言:javascript
复制
AI turn
dice roll {2}
Die Roll : 2   Pot : 2
dice roll {5}
Die Roll : 5   Pot : 7
dice roll {4}
Die Roll : 4   Pot : 11
dice roll {2}
Die Roll : 2   Pot : 13
dice roll {6}
Die Roll : 6   Pot : 19
dice roll {2}
Die Roll : 2   Pot : 21
dice roll {5}
Die Roll : 5   Pot : 26
dice roll {1}
Die Roll 1 : BUST

Die Roll 1 : BUST Pot Value = 0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54621940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档