21 Match stick problem



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.


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int matchstick = 21;
    int user,computer;
  
    while (matchstick>=1)
    {
      
        if (matchstick==1)
        {
            printf("\nMatch stick status:%d",matchstick);
            printf("\nYou loose!!!!!!:(:(");               break;
        }
      
        printf("\nMatch stick status:%d",matchstick);      
        printf("\nEnter the choice (1,2,3,4)):");        scanf("%d",&user);
      
        printf("\nYou picked %d",user);      
        if (user>=5 || user <=0)
        {
            printf("\nInvalid value");            continue;
        }
      
        computer = 5 - user;
      
        printf("\nComputer picked%d",computer);      
        matchstick = matchstick - computer - user;
      
    }
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

3 comments:

yogananda said...

Can u explain what this game?

Geeta singh said...

Yes Plzzz explain what is game about

mihir singh said...

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

Post a Comment