gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:26 27 4
gpt4 key购买 nike

我不知道如何在没有 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com