Hey Everyone,
I am working on a school project. This program is supposed to be a 2 person player game *user vs CPU*.
The user inputs a number 1 2 or 3 and it is subtracted from 23. The CPU will "pick" a number 4 - userPick. The one with the last stick loses.
However, I am having trouble where to place the loops that verifies that the number picked and entered is not more than 4, which displays the error to the user.
Also, I need to loop the game to run again if the user chooses to do so.
I have seen many of these projects online, but would prefer this style if possible and I really need to understand placement.
Also, for the last pick, if the sticks are below 4, the CPU is supposed to pick the number that leave just 1 stick. I dont believe there is a scenario the CPU loses like this.
If the user gets the last stick, then they lose and the program prompts to restart.
Any additional suggestions & advice is much appreciated. Thank you for your consideration.
P.S. I know there are simpler, yet more advanced way to organize this, it would be helpful to keep it in this style for me as this is my 2nd month in class.
[#include <iostream>
using namespace std;
int main()
{
int userPick, cPick, toothpicks=23;
char answer;
cout << "Lets play a game of 23!\n";
do
{
cout << "Enter the number of sticks you wish to pick: ";
cin >> userPick;
if (userPick < 1 || userPick >3)
{
cout<< "Wrong number of sticks. Please pick 1, 2, or 3 sticks: ";
}
}
while(userPick<1 || userPick>3);
cout<< "Your turn. Enter the number of sticks you wish to pick: ";
cin >> userPick;
}
else if (toothpicks==4)
{
cPick= toothpicks-3;
toothpicks = toothpicks - cPick;
cout << "Computer picked " << cPick << " sticks. ";
cout<< "Your turn. Enter the number of sticks you wish to pick: ";
cin >> userPick;
}
else if (toothpicks == 3)
{
cPick= toothpicks-2;
toothpicks = toothpicks - cPick;
cout << "Computer picked " << cPick << " sticks. ";
cout<< "Your turn. Enter the number of sticks you wish to pick: ";
cin >> userPick;
}
else if (toothpicks == 2)
{
cPick= toothpicks-1;
toothpicks = toothpicks - cPick;
cout << "Computer picked " << cPick << " sticks. ";
cout<< "Your turn. Enter the number of sticks you wish to pick: ";
cin >> userPick;
}
else if (toothpicks == 1 && userPick == 1)
{
cout << "You picked the last stick. Sorry the computer beat you!" << endl;
}
}
}
while (toothpicks >=1);
write the pseudocode or draw the flow diagram first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
loop
the game
until user doesn't want to play
//the game
loop
ask user for input
determine cpu strategy
until there are no more toothpicks
declare winner
//ask user for input
loop
input
until valid input
putting all together
1 2 3 4 5 6 7 8 9 10
loop
loop
loop
input
until valid input
determine cpu strategy
until there are no more toothpicks
declare winner
until user doesn't want to play
#include <iostream>
usingnamespace std;
int main() {
int userPick, cPick, toothpicks = 23;
char answer;
cout << "Lets play a game of 23!\n";
do {
//the game
do{
//user input
do {
cout << "Enter the number of sticks you wish to pick: ";
cin >> userPick;
if(userPick < 1 || userPick > 3) {
cout << "Wrong number of sticks. Please pick 1, 2, or 3 sticks: ";
}
} while(userPick < 1 || userPick > 3); //should also verify that userPick doesn't exceed the total toothpicks
//cpu strategy
} while (toothpicks >= 1);
//declare the winner (player/cpu)
cout << "¿Want to play again? ";
cin >> answer;
} while (answer == 'y');
cout << "Game over.\n";
}
> I dont believe there is a scenario the CPU loses like this.
The toothpicks would diminish in steps of 4, so it depends on the starting number of toothpicks.
In this case (23) the cpu is letting you win, it ends with 7 toothpicks and your move:
- 3 is a loser move as the cpu counters with 3, letting you only 1 toothpicks and your lost.
- with 1 or 2, the cpu would again make a 4 step, letting you with 3, then choosing 2 is a winning move.
It would be different if you start at, say, 25. Then the strategy is unbeatable.