My game of nim is creating an infinite loop of saying "the mode is set to dumb." and "It is your turn." I need help in changing these loops to make them repeat but, I wouldn't know how to do it without the do-while loop. I know the while(pilesize!=1); is the problem but, I don't know how to fix it.
// 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 turn(int, int);
usingnamespace 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") {
dumbcomputer(marbletake, pilesize);
}
else {
smartcomputer(pilesize);
}
}
}
else {
turn(marbletake, pilesize);
}
} while (pilesize != 1);
return 0;
}
int turn(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");
return 0;
}
}
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;
system("pause");
return 0;
}
}
}
int smartcomputer(int pilesize){
cout << "The bot is removing marbles from the pile." << endl;
if (pilesize > 63) {
pilesize = 63;
}
elseif (pilesize > 31 && pilesize < 63) {
pilesize = 31;
}
elseif (pilesize > 15 && pilesize < 31) {
pilesize = 15;
}
elseif (pilesize > 7 && pilesize < 15) {
pilesize = 7;
}
elseif (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;
system("pause");
return 0;
}
}
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;
system("pause");
return 0;
}
}