Saturday, July 16, 2016

A program to play a matchstick party game with the computer

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
Let Us C Page 129 [B] (f)

Do you love playing games on computer, like me? In that case you might really like this program where you can not simply defeat the computer because of the strategy in the game. I will discuss the strategy in a few minutes, but here is the source code of the program:

/* Matchstick game : the one to pick up at last loses

Author : Viks Von Doom
Date : 16/07/2016 Time 04.15 PM IST */

#include<stdio.h>

int main()
{
    int user_choice;
    int max_pick = 4;
    int tot_match = 21;
    int com_choice;

    while(tot_match > 1)
    {
        printf("\n There are %d matchsticks. You can pick at once- 1,2,3 or 4 matchsticks.    \n Pick now : \n", tot_match);
        scanf("%d", &user_choice);

        com_choice = max_pick +1 - user_choice;
        printf(" \n Computer picks : %d \n", com_choice);
        tot_match = tot_match - user_choice - com_choice;

        if(tot_match == 1)
        printf(" \n There is only 1 match stick left for you yo pick up \n You lose! \n");
    }

return 0;
}




The Strategy: You can read about the strategy in this number game at hobbylark.com This game seems to be one of the popular party games for kids as well!


No comments:

Post a Comment