Question: modify this c++ program so that the pile cant go under 0 and the user cant enter number above 3 or below 1 code: #include
modify this c++ program so that the pile cant go under 0 and the user cant enter number above 3 or below 1
code:
#include
using namespace std;
int randRange(int low, int high){ return rand() % (high - low + 1) + low; }
int main() { srand(time(NULL)); int piles[3]; for (int i = 0; i < 3; i++) { piles[i] = randRange(1, 9); cout << "Pile " << i+1 << " has " << piles[i] << " stones." << endl; } while (true){ int pile = 0; int stones = 0; cout << "Your turn: Choose a pile (1-3) and how many stones to remove (1-3)." << endl; cin >> pile >> stones; if (stones > 3 or stones < 0 or stones > piles[pile]) { cout << "Invalid move. Try again." << endl; cout << "Your turn: Choose a pile (1-3) and how many stones to remove (1-3)." << endl; cin >> pile >> stones; pile--; } pile--; piles[pile] -= stones; cout << "Pile " << pile+1 << " now has " << piles[pile] << " stones." << endl; int total = piles[0] + piles[1] + piles[2]; if (total == 0) { cout << "You took the last stone. You lose!" << endl; return 0; } cout << "Computer's turn..." << endl;
pile = randRange(0, 2); while (piles[pile] == 0) { pile = randRange(0, 2); } stones = randRange(1, min(3, piles[pile])); piles[pile] -= stones; cout << "Computer took " << stones << " from pile " << pile+1 << "." << endl; cout << "Pile " << pile+1 << " now has " << piles[pile] << " stones." << endl; total = piles[0] + piles[1] + piles[2]; if (total == 0) { cout << "Computer took the last stone. You win!" << endl; return 0; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
