Please help

Need help on a program I have to write for class. It is a "player vs computer" game of "23". The player moves first by withdrawing 1-3 toothpicks. The rules for the computers are in the "if" and "else if" statements. Whoever takes the last toothpick loses. It's pretty basic. What I really need help with is having the program figure out whether the "player" or "computer" took the last toothpick, and setting up a closing message displaying the winner and loser. Hope this makes sense. The instructions were pretty vague out of the book.

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
int main()
{
	// Declare variables below here
	int toothpicks, X;
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	toothpicks = 23;
	do
	{
	cout<<"Please withdraw 1-3 toothpicks."<<endl;
	cin>>X;
	if ((X >= 1) && (X <= 3))
	{
		toothpicks = toothpicks - X;
	}
	else
	{
		cout<<"ERROR! Must withdraw 1-3 toothpicks. Please try again."<<endl;
		toothpicks = 23;
	}
	
	cout<<toothpicks<<" toothpicks left."<<endl;
	cout<<"Computer's turn..."<<endl;

	if (toothpicks > 4)
	{
		toothpicks = toothpicks - (4 - X);
	}

	else if ((toothpicks >= 2) && (toothpicks <= 4))
	{
		toothpicks = toothpicks - (toothpicks - 1);
	}

	else if (toothpicks = 1)
	{
		toothpicks = toothpicks - 1;
	}

	cout<<toothpicks<<" toothpicks left."<<endl;

	}while (toothpicks > 0);

	return 0;
}
Personally I would have a bool that keeps track of if it is currently the players turn or not. Something like

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
bool GameOver = false;
bool PlayersTurn = true;

while(!GameOver)
{
    if(PlayersTurn)
    //PlayerLogic

    else
    //ComputerLogic

    if(toothpicks == 0)
    {
         if(PlayersTurn)
              //You Won

         else
              //You lost

        GameOver = true;  
    }

    if(!GameOver)
        SwitchPlayers();
}


But any variation on that will work fine. Also you should be using == on line 37 (comparison) not = (assignment)
else if (toothpicks = 1)
Topic archived. No new replies allowed.