c++ - nim 游戏 : Issues with loops and program in general

标签 c++

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

 // 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;

}

最佳答案

乘坐cout在循环外打印模式的行:

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 已经保证了这一点失败,则不会跳过这些边界情况。

关于c++ - nim 游戏 : Issues with loops and program in general,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49502602/

相关文章:

C++ 排序类数组

c++ - Alpha channel 被渲染为黑色。怎么让它透明呢?

python - 用于在opencv3上进行视差映射的cv2.StereoSGBM_create()和cv2.StereoBM_create()函数之间有什么区别?

c++ - 是否有办法使用Booth算法捕获二进制乘法期间的上溢/下溢?

c++ - C++按下键

c++ - gazebo ros插件报错: Undefined symbols for architecture x86_64

c++ - 为什么 C++ char 数据类型 3d array 当传递参数第一个括号为空时

c++ - 删除最后一行时,QTableWidget 的单元格小部件无法正确显示

C++ 变量类型条件

用于检查有序数据是否在集合中的 C++ 容器