Question: Create a new C++ program and name it GameOfNim3. We will now modify our previous game where we have 3 piles of stones, each created

Create a new C++ program and name it GameOfNim3. We will now modify our previous game where we have 3 piles of stones, each created randomly. Let's create each pile as a random integer between 1 and 9. The rules of only being able to remove between 1 and 3 stones are still in place but the user / computer and can only remove from one pile per turn. This means you can't divide your move across piles like taking 1 stone from pile 2 and two stones from pile 3. The loser of the game is the person / machine which takes the last stone from the board, not per pile. So the game is over when the final stone is removed.

code:

#include #include #include #include

using namespace std;

int randRange(int low, int high){ return rand() % (high - low + 1) + low; }

int main() { srand(time(NULL)); int numStones = 0; int playerChoice = 0; numStones = randRange(15, 31); cout << "There are " << numStones << " stones. How many would you like to take? "; cin >> playerChoice; while (playerChoice < 1 or playerChoice > 3 or playerChoice > numStones){ if (playerChoice < 1 or playerChoice > 3){ cout << "Value must be between 1 and 3." << endl; } else if (playerChoice > numStones){ cout << "Cannot take more than the remaining stones." << endl; } cout << "How many would you like to take? "; cin >> playerChoice; } numStones -= playerChoice; while (numStones > 0){ int computerChoice = randRange(1, 3); while (computerChoice > numStones){ computerChoice = randRange(1, 3); } cout << "There are " << numStones << " stones. The computer takes " << computerChoice << "." << endl; numStones -= computerChoice; if (numStones == 0){ cout << "You win!" << endl; return 0; } cout << "There are " << numStones << " stones. How many would you like to take? "; cin >> playerChoice; while (playerChoice < 1 or playerChoice > 3 or playerChoice > numStones){ if (playerChoice < 1 or playerChoice > 3){ cout << "Value must be between 1 and 3." << endl; } else if (playerChoice > numStones){ cout << "Cannot take more than the remaining stones." << endl; } cout << "How many would you like to take? "; cin >> playerChoice; } numStones -= playerChoice; } cout << "The computer wins!" << endl; return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!