我不知道如何在没有do-while循环的情况下重复我的程序。循环使我的程序重复说"模式设置为哑"和#34;轮到你了。"我可以真正使用一些帮助来完成这个程序,因为它是家庭作业,并将于今晚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;
}
答案 0 :(得分:0)
将打印模式的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
,因为前一个{{1}}失败已经保证了这一点,因此不会跳过这些边界情况。