// Boma Braide
// December 12, 2018
// Game of NIM
#include <iostream>
#include <ctime>
#include <cmath>
usingnamespace std;
int main(){
bool winner = false;
int num, sticks = 22, comp_choice;
char whoseTurn, yourTurn = 'Y', compTurn = 'N';
srand(time(NULL));
cout << "welcome to the game of N I M. would you like to go first? Y/N" << endl;
cin >> whoseTurn;
if (whoseTurn == yourTurn){
cout << "you go first!" << endl;
}
elseif (whoseTurn == compTurn){
cout << "the computer goes first!" << endl;
}
cout << endl;
while (sticks >= 0){
// if the player chooses to go first...
if (whoseTurn == yourTurn){
cout << "there are " << sticks << " sticks in the pile.\n"
<< "\n enter the amount (between 1 and 4) of sticks that you will remove from this pile." << endl;
cin >> num;
// if number is 1-4, amount of sticks taken/left will be displayed
if ((num >= 1) && (num <= 4)){
cout << "you have removed " << num << " sticks from the pile." << endl;
sticks -= num;
cout << "there are now " << sticks << " sticks left in the pile." << endl;
}
// if number is less than 1 or greater than 4, user will be repeatedly prompted until done so
if ((num < 1) || (num > 4)){
cout << "unacceptable answer >:( enter an amount between *1* and *4*" << endl;
// cin >> num;
}
// computer enters value
else {
comp_choice = 1 + rand() % (4 - 1) + 1;
sticks -= comp_choice;
cout << "\n the computer has taken away " << comp_choice << " sticks from the pile.\n"
<< "there are now " << sticks << " sticks left in the pile.\n";
}
}
// ----------------------------------------------------------------------------------------------------------- //
// if COMPUTER goes first
elseif (whoseTurn == compTurn){
comp_choice = 1 + rand() % (4 - 1) + 1;
sticks -= comp_choice;
cout << "\n the computer has taken away " << comp_choice << " sticks from the pile.\n"
<< "there are now " << sticks << " sticks left in the pile.\n";
}
else {
do {
cout << "there are " << sticks << " sticks in the pile.\n"
<< "\n enter the amount (between 1 and 4) of sticks that you will remove from this pile." << endl;
cin >> num;
} while ((num >= 1) && (num <= 4));
cout << "you have removed " << num << " sticks from the pile." << endl;
sticks -= num;
cout << "there are now " << sticks << " sticks left in the pile." << endl;
if ((num < 1) || (num > 4)){
cout << "unacceptable answer >:( enter an amount between *1* and *4*" << endl;
}
whoseTurn = whoseTurn == compTurn ? yourTurn : compTurn;
}
}
system("pause");
return 0;
}
There were several small errors in your code, especially at the range of your random numbers and how you detect if the poile get empty. Here the fixed code. I have used the random facilities introduced at c++11.
#include <iostream>
#include <random>
usingnamespace std;
int main(){
bool playerWon;
int num, sticks = 22;
char whoseTurn;
constchar yourTurn = 'Y', compTurn = 'N';
default_random_engine r( random_device{}());
uniform_int_distribution<> d(1,4);
cout << "welcome to the game of N I M. would you like to go first? Y/N" << endl;
cin >> whoseTurn;
if (whoseTurn == yourTurn) cout << "you go first!" << endl;
elseif (whoseTurn == compTurn) cout << "the computer goes first!" << endl;
cout << endl;
while (sticks > 0){
// if the player chooses to go first...
if (whoseTurn == yourTurn){
cout << "There are " << sticks << " sticks in the pile.\n"
<< "\n enter the amount (between 1 and 4) of sticks that you will remove from this pile." << endl;
cin >> num;
// if number is 1-4, amount of sticks taken/left will be displayed
if ((num >= 1) && (num <= 4)){
if (num >= sticks) {
cout << "You have won!\n";
playerWon = true;
break;
}
cout << "you have removed " << num << " sticks from the pile." << endl;
sticks -= num;
cout << "there are now " << sticks << " sticks left in the pile." << endl;
whoseTurn = compTurn;
continue;
}
// if number is less than 1 or greater than 4, user will be repeatedly prompted until done so
if ((num < 1) || (num > 4)){
cout << "unacceptable answer >:( enter an amount between *1* and *4*" << endl;
// cin >> num;
continue;
}
}
// computer enters value
else {
if ( sticks <= 4){
cout << "\nThe computer took " << sticks << " sticks, it won\n";
playerWon = false;
break;
}
int comp_choice = d(r); // gtting a random number
sticks -= comp_choice;
cout << "\n the computer has taken away " << comp_choice << " sticks from the pile.\n";
whoseTurn = yourTurn;
}
}
cout << (playerWon ? "Congratulations, you won the game." : "Sorry, you lost.") << '\n';
}