C++ nim游戏:循环和程序的一般问题
我不知道如何在没有do-while循环的情况下使程序重复。循环使我的程序重复说“模式设置为哑”和“轮到你了”。我真的需要一些帮助来完成这个程序,因为它是家庭作业,今晚9:30到期。游戏是从一堆弹珠中取出弹珠,拿最后一颗弹珠的人输了C++ nim游戏:循环和程序的一般问题,c++,C++,我不知道如何在没有do-while循环的情况下使程序重复。循环使我的程序重复说“模式设置为哑”和“轮到你了”。我真的需要一些帮助来完成这个程序,因为它是家庭作业,今晚9:30到期。游戏是从一堆弹珠中取出弹珠,拿最后一颗弹珠的人输了 // new project 1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <ctime> #include &
// 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;
}
//新项目1.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括
智能计算机(int);
int计算机(int,int);
int playerturn(int,int);
使用名称空间std;
int main()
{
布尔标志=真;
int-marbletake=0;
弦乐哑铃;
srand(时间(0));
int pilesize=rand()%(100-10)+10;
cout从循环中取出打印模式的cout
行:
cout << "The mode is set to " << dumborsmart << "\n";
do {
...
} while (pilesize != 1);
cout X&&pilesize
测试时,您跳过了pilesize==Y
的情况。您应该删除&&pilesize
,因为之前的如果失败,那么这些边界情况就不会被跳过,这已经得到了保证。您是否在问if(turn=false)中的内容为什么{
不运行?使用=
进行比较,=
用于赋值。是的,我在问为什么程序重复“模式设置为哑”和“轮到你了”一次又一次。我想这是在底部,但是,我不知道如何让它重复。好的。让我试试。好的。谢谢。我会改变你说的所有东西,看看它是如何进行的。