1. ## Matchstick game

Can anybody gimme an idea as to how this can be done..?

Write a program for a matchstick game being played between the computer and a user.
Your program should ensure that the computer always wins. Rules for the game are as follows:

-There are 21 matchsticks.
-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
-After the person picks, the computer does its picking.
-Whoever is forced to pick up the last matchstick loses the game.

I tried out some stuff..but as it is..my program was not quite organised..

Code:
```#include<stdio.h>
main()
{
int matchsticks, total;

total = 21;

printf("Enter the number of matchsticks you wanna pick up at once:");
scanf("%d", &matchsticks);

matchsticks = total - matchsticks;

while (matchsticks<= 4)
{
printf("\n Computer Turn");

/*	...the rest of the program is mystery! */```

2. Code:
```		if (matchsticks == )
matchsticks=4;
else if (matchsticks;```
What is this supposed to be?

3. The program is incomplete man..

I was just trying to figure it out..and then I posted it here...

I will edit that part though..

4. Actually I was trying to figure out the logic...

How do we make the computer win?
How do we know what move to make..how many matchsticks the computer should pick up..in case the user enters n number of matchsticks..?

My First game..hehe

5. The thing is, this actually requires you to use your brain. A lot of programming involves finding patterns and this is a good example of that. The trick is to keep the player from getting into a position where s/he can't beat you. The first thing you should realize is that if you can always get the player to end up at 5 matchsticks, there's no way s/he can win. If you make a little chart of how many matchsticks you want to pick up, it becomes clear:
Code:
```1 1
2 2
3 3
4 4
6 1
7 2
8 3
9 4
11 1
12 2
13 3
14 4
16 1
17 2
18 3
19 4
21 1```
e.g. if there are 6 sticks left and you pick up 1 then you leave the player with 5. There's no choice s/he can make from there that will beat you. From the chart you can see a clear pattern emerge. Any multiple of 5 is bad so the key is to get the player to always end up on a multiple of 5. This is very easy to code.

HINT: Use the modulus operator.

EDIT: I had a whole working program in there, but decided to remove it for the OP's benefit.

6. > How do we make the computer win?
Have you figured out how you would win if playing against another person?

-There are 21 matchsticks.
-Whoever is forced to pick up the last matchstick loses the game.
So the last one is special, so it's all about how to get rid of 20 matches in pairs of turns.

-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
So if we reduce the total by 5 each round, the sequence will go
21 16 11 6 1
In effect, whatever number the user picks (n), the computer picks 5-n

7. Hmm...the logic I used breaks down if the user picks 1 every time. Oops.

8. The bit about the brain was good though