首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nim游戏:循环和程序的一般问题

nim游戏:循环和程序的一般问题
EN

Stack Overflow用户
提问于 2018-03-27 00:23:56
回答 1查看 425关注 0票数 2

我不知道如何使我的程序在没有do-while循环的情况下重复。循环让我的程序重复说“模式设置为哑”和“该轮到你了”。我真的需要一些帮助来完成这个程序,因为它是家庭作业,今晚9:30到期。游戏是把弹珠从一堆里取出来,而把最后一块大理石取下来的人就输了。

代码语言:javascript
复制
 // new project 1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <ctime>
#include <string>
#include <iostream>
#include <random>
int smartcomputer(int);
int dumbcomputer(int, int);
int playerturn(int, int);
using namespace std;


int main()
{
    bool flag = true;
    int marbletake = 0;
    string dumborsmart;
    srand(time(0));
    int pilesize = rand() % (100 - 10) + 10;
    cout << "Hello, Welcome to the game of Nim. Two players alternatively take marbles from a pile." << endl;
    cout << "Each move a player must take at least one marble but at most half of the marbles." << endl;
    cout << "Enter dumb for dumb mode and smart for smart mode." << endl;
    getline(cin, dumborsmart);
    do {
        if (dumborsmart == "dumb") {
            cout << "The mode is set to dumb." << endl;//set to dumb

            bool turn = rand() % 2;
            if (turn = true) {
                cout << "It is your turn" << endl;
            }
            else {
                cout << "It is the bots turn" << endl;
            }
            if (turn = false) {      //not player's turn=false
                if (dumborsmart == "dumb") {
                    pilesize = dumbcomputer(marbletake, pilesize);
                    bool turn = true;
                }
                else {
                    pilesize = smartcomputer(pilesize);
                    bool turn = true;
                }
            }
        }
        else {

            pilesize = playerturn(marbletake, pilesize);
            bool turn = false;
        }

    } while (pilesize != 1);
    return 0;
}

int playerturn(int marbletake, int pilesize){
    cout << "It is your turn. Marbles remaining in pile: " << pilesize << endl;
    cout << "Enter a number of marbles to remove from the pile" << endl;
    cin >> marbletake;
    if (marbletake > 1 && marbletake < (pilesize / 2)) {
        pilesize = pilesize - marbletake;
        cout << "Number of marbles in the pile: " << pilesize << endl;
        if (pilesize ==1) {
            cout << "You win. The bot is forced to take the last marble." << endl;
            system("pause");

        }
    }
    else {
        cout << "Error, please remove a number of marbles greater than 0 or less than half the pile." << endl;
        cin >> marbletake;
        pilesize = pilesize - marbletake;
        cout << "Number of marbles in the pile: " << pilesize << endl;
        if (pilesize ==1) {
            cout << "You win. The bot is forced to take the last marble." << endl;

        }
    }

    return pilesize;

}
int smartcomputer(int pilesize){
    cout << "The bot is removing marbles from the pile." << endl;
    if (pilesize > 63) {
        pilesize = 63;
    }
    else if (pilesize > 31 && pilesize < 63) {
        pilesize = 31;
    }
    else if (pilesize > 15 && pilesize < 31) {
        pilesize = 15;
    }
    else if (pilesize > 7 && pilesize < 15) {
        pilesize = 7;
    }
    else if (pilesize > 3 && pilesize < 7) {
        pilesize = 3;
    }
    else {
        pilesize = pilesize - rand() % (pilesize / 2) + 1;
    }
    cout << "Number of marbles in the pile: " << pilesize << endl;
    if (pilesize ==1) {
        cout << "You lose. You are forced to take the last marble." << endl;

    }

    return pilesize;
}
int dumbcomputer(int marbletake, int pilesize){
    cout << "The bot is removing marbles from the pile." << endl;
    pilesize = (pilesize - rand() % (pilesize / 2) + 1); // bot takes marbles from pile(half pile size-1)
    cout << "Number of marbles in the pile: " << pilesize << endl;
    if (pilesize ==1) {
        cout << "You lose. You are forced to take the last marble." << endl;

    }

    return pilesize;

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-27 00:33:37

将输出模式的cout行从循环中取出:

代码语言:javascript
复制
cout << "The mode is set to " << dumborsmart << "\n";
do {
    ...
} while (pilesize != 1);

另外,if (turn = true)if (turn = false)不能工作,您需要在那里使用==。但是写if (turn)if (!turn)更好。

在您的else if (pilesize > X && pilesize < Y)测试中,您跳过了pilesize == Y的情况。您应该只删除&& pilesize < Y,因为以前的if失败已经保证了这一点,因此不会跳过这些边界情况。

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

https://stackoverflow.com/questions/49502602

复制
相关文章

相似问题

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