Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The game of “23” is a two-player game that begins with a pile of 23 toothpicks.

ID: 3848926 • Letter: T

Question

The game of “23” is a two-player game that begins with a pile of 23 toothpicks. Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays “23”. The human should always move first. When it is the computer’s turn, it should play according to the following rules: •   If   there   are   more   than   4   toothpicks   left,   then   the   computer   should   withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.    If   there   are   2   to   4   toothpicks   left,   then   the   computer   should   withdraw   enough toothpicks to leave 1. •   If   there   is   1   toothpick   left,   then   the   computer   has   to   take   it   and   loses.
When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure that the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.

Please notice that the following requirements:

1. If the user picks illegal number of sticks, the program will prompt the user to repick.

2. At the end of each game, the program will prompt user to choose if the user wants another game.

Let's play a game of 23 Enter the number of sticks you wish to pick: 2 You picked 2 stick 21 lef Computer picked 2 sticks 19 left Your turn. Enter the number of sticks you wish to pick: 3 You picked 3 stick 16 lef Computer picked 1 sticks 15 left Your turn. Enter the number of sticks you wish to pick: 4 Wrong number of sticks. Please pick 1, 2, or 3 sticks: 0 Wrong number of sticks. Please pick 1, 2, or 3 sticks: 2 You picked 2 stick 13 left Computer picked 2 sticks 11 left Your turn. Enter the number of sticks you wish to pick: 3 You picked 3 stick 8 left Computer picked 1 sticks 7 ef Your turn. Enter the number of sticks you wish to pick: 2 You picked 2 stick 5 lef Computer picked 2 sticks 3 left Your turn. Enter the number of sticks you wish to pick: 1 You picked 1 stick 2 left Computer picked 1 sticks 1 ef Your turn. Enter the number of sticks you wish to pick: 1 You picked 1 stick 0 lef You picked the last stick. Sorry, the computer beat you! Do you want to play another game?

Explanation / Answer

Below is your code in C++. Let me know if you have any issue with this: -

#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int main(int argc, char** argv) {
  
//Write a human vs computer program that plays "23"
//c_pick is the computer's choice while h_pick is the human choice
  
  
  
  
int c_pick, h_pick;
int toothpicks= 23;
char strt;
int count;
do {
   count = 0;
   cout << " Lets play a game of "23"!" << endl;
   toothpicks= 23;
   do{
       if(count == 0)
            cout << "Enter the number of sticks you wish to pick: ";
        else
           cout << " Your turn. Enter the number of sticks you wish to pick: ";
       count++;
cin >> h_pick;
  
       if (h_pick>3 || h_pick <=0)
{
cout<< "Wrong number of sticks . Please pick 1,2 or 3 sticks: " <<endl;
  
continue;
}
toothpicks = toothpicks - h_pick;
cout<<" You picked "<<h_pick<<" stick. "<<toothpicks<<" left ";

if (toothpicks > 4) {
c_pick = 4 - h_pick;
} else if (toothpicks == 2) {
c_pick = 1;
} else if (toothpicks == 3) {
c_pick = 2;
} else if (toothpicks == 4) {
c_pick = 3;
} else if (toothpicks == 1) {
c_pick = 1;
}

toothpicks = toothpicks - c_pick;
cout << " Computer picked " << c_pick << " sticks. " <<toothpicks <<" left."<<endl;

if (toothpicks == 1 && h_pick == 1) {
   cout <<"You picked 1 stick. 0 left."<<endl;
cout <<"Sorry! the computer beats you!" << endl;
break;
} else if(toothpicks == 0 && c_pick == 1) {
   cout << "You win!" << endl;
break;
       }
       if(toothpicks == 1) {
           cout <<"You picked 1 stick. 0 left."<<endl;
cout <<"Sorry! the computer beats you!" << endl;
break;
       }

}
while (true);
cout<<"Do you want to play another game? <Y/N>:";
cin>> strt;
   } while (strt == 'Y' || strt == 'y');
  
return 0;
}

Sample Run


Lets play a game of "23"!
Enter the number of sticks you wish to pick: 2

You picked 2 stick. 21 left

Computer picked 2 sticks. 19 left.

Your turn. Enter the number of sticks you wish to pick: 3

You picked 3 stick. 16 left

Computer picked 1 sticks. 15 left.

Your turn. Enter the number of sticks you wish to pick: 4
Wrong number of sticks . Please pick 1,2 or 3 sticks:

Your turn. Enter the number of sticks you wish to pick: 0
Wrong number of sticks . Please pick 1,2 or 3 sticks:

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 13 left

Computer picked 2 sticks. 11 left.

Your turn. Enter the number of sticks you wish to pick: 3

You picked 3 stick. 8 left

Computer picked 1 sticks. 7 left.

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 5 left

Computer picked 2 sticks. 3 left.

Your turn. Enter the number of sticks you wish to pick: 1

You picked 1 stick. 2 left

Computer picked 1 sticks. 1 left.
You picked 1 stick. 0 left.
Sorry! the computer beats you!
Do you want to play another game? <Y/N>:y

Lets play a game of "23"!
Enter the number of sticks you wish to pick: 2

You picked 2 stick. 21 left

Computer picked 2 sticks. 19 left.

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 17 left

Computer picked 2 sticks. 15 left.

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 13 left

Computer picked 2 sticks. 11 left.

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 9 left

Computer picked 2 sticks. 7 left.

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 5 left

Computer picked 2 sticks. 3 left.

Your turn. Enter the number of sticks you wish to pick: 2

You picked 2 stick. 1 left

Computer picked 1 sticks. 0 left.
You win!
Do you want to play another game? <Y/N>:n

--------------------------------
Process exited after 25.41 seconds with return value 0
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote