21 Matchstick Puzzle

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.


Answer:

This is the heart of the program.
if user picks 1 stick then computer can pick 4 sticks
that means, 5 (i.e. max+1) sticks will be picked out for each round.
For 4 rounds, 20 sticks are out and only 1 stick will be left.
And user picks it, he lose the game.

Comments

Post a Comment