Need Help 23 Toothpicks Program *I'm not experienced at all*

Hi, I'm running into some issues with a project thats due tonight that I've been trying to fix for a couple days by myself. I cannot see what the issues are besides maybe two areas. Can you take a look and see what I did wrong and what I can do to improve it?

The project is a game of 23 toothpicks.

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 the following rules:

a. If there are more than 4 toothpicks left, the computer should withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.
b. If there are 2 to 4 toothpicks left, the computer should withdraw enough toothpicks to lave 1.
c. If there is 1 toothpick left, 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 the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* 
Author: Justin 
Date: 9/21/2015
Project 3: 23 Toothpicks
Program plays the game 23 and whoever chooses the last toothpick loses.
*/

#include <iostream>
using namespace std;
int main()
{
	int toothpicks, h_pick, c_pick;
	bool game_finished;
	// initializing variables. h_pick = human pick. c_pick = computer pick.
	do {
		game_finished = false;
		toothpicks = 23; //starts new game.
		while (!game_finished)
		{
			cout << "Welcome to the toothpick game!" << endl;
				// get player's pick, validation is needed.
				cout << "Please pick up your toothpick(s). Choose between 1, 2 , and 3." << endl;
				cin >> h_pick;
				// update toothpicks remaining.
				toothpicks = toothpicks - h_pick;
				cout << toothpicks << " toothpicks are remaining." << endl;
			
				if (toothpicks == 1 && c_pick == 1)
				{
					// if computer picked last toothpick
					cout << "You have won! Congratulations!" << endl; // print out computer loses message.
					game_finished = true; // set game_finished to be true.
				}
				if (toothpicks > 4)
				{
					c_pick = 4 - h_pick; // get computer's pick.
					cout << "The computer took " << c_pick << " toothpick(s)." << endl; // update toothpicks remaining.
				}
					else if (toothpicks >= 2 && toothpicks <= 4 )
				{
						c_pick = toothpicks - 1; // left one only
						cout << "The computer took " << c_pick << " toothpick(s)." << endl;
				}
					else 
						c_pick = 1; // only one left
				if (toothpicks == 1 || h_pick == 1)
					// if player picked last toothpick
					cout << "The computer has won! Too bad!" << endl; // print out player loses message.
					game_finished = true; // set game_finished to be true.
				if (h_pick > 3 || h_pick < 1)
				{
						cout << "Please enter a correct value." << endl;
						continue;
				}
				if (toothpicks < 0)
				{
					cout << "Game will now restart. (Press 4 to quit)" << endl;
					cin >> h_pick;
					toothpicks = 23;
					continue;
				}
		}
	}

	while (h_pick != 4);
		game_finished = true;
		return 0;
}
		
			
			
		
			

Last edited on
You have most of the logic right, but the sequencing is wrong. Remember that the code executes sequentially.

Line 28 Since the human just moved, it doesn't matter whether c_pick == 1, the human wins if toothpicks == 1 regardless of what the computer's last move was.

Line 32: You need to exit the loop at this point, so I'd change it to break;.

Lines 34-45 set c_pick, but you never actually subtract this amount from toothpicks. Do that just before line 46.

Line 46: Now you're checking if the computer won. There's no need to check if h_pick==1, the computer wins if there is one toothpick left regardless of the human's last move.

Line 49: I'd change this to break;

Lines 50-54: These should move to right after line 23. After all, you want to validate the human's input right after they give it.

Lines 55-61: This code asks whether the player wants to play again. It should go outside the play loop, so after line 62.

Line 66 is outside both loops. Get rid of it.

That should make the program work.

It looks to me like you get confused about what is happening where. That's easy to do. One way to avoid this problem is to use functions. For example you might write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void playOneGame(); // plays the game
int getHumanMove(); // get the human's move. Keep prompting until it's valid
int getComputerMove(); // get the computer's move.
bool playAgain();  // ask user if they want to play again. Return true if yes.

int main()
{
    do (
        playOneGame();
    } while (playAgain());
}

playOneGame()
{
    int toothpicks = 23;
    int pick;
    while (true) {
       // do the human's move
       pick = getHumanMove();
       toothPicks -= pick;
       if (toothPicks == 1) {
           cout << "You won!\jn";
           break;
       }

       // Do the computer's move
       pick = getComputerMove();
       toothPicks -= pick;
       if (toothPicks == 1) {
           cout << "Computer won!\n";
           break;
       }
    }
}

etc.

You will find that some of the functions need access to some data. For example, getComputerMove() needs to know what the human's move was. So you'll have to pass that as a parameter. Better yet, the values would be members of a class and the functions would be methods.

1
2
       if (toothPicks == 1) {
           cout << "You won!\jn"; //or Computer won 
that's incorrect. The game should end when there are no more toothPicks, no when one remains.
That would leave open a suicidal strategy, when there are 2 or 3 remaining and the player decides to take them all.
Last edited on
Topic archived. No new replies allowed.