Skip to main content

Matchstick Game using C Programming

Matchstick Game using C Programming

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.

-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



#include<stdio.h>
main()
{

int matchsticks=21, user, computer;

printf("Do not enter Invalid Numbers.\nNumbers above 4 are invalid.");

printf("\nIf you do so, the computer automatically wins.");

while (matchsticks>=1)
{

printf("\nNumber of matchsticks available right now is %d.", matchsticks);

printf("\n\nYour Turn...\n\n\n");

printf("\nPick up the matchstick(s)-- (1-4): ");
scanf ("%d", &user);

if (user>4)

{
printf("Invalid Selection");
break;
}

computer=5-user;

printf("\nComputer's Turn..\n" );
printf("\nComputer chooses:%d", computer);

matchsticks=matchsticks-user-computer;
continue;

if(matchsticks==1)

break;
}

matchsticks--;
printf("\nComputer Wins");

}

Comments

Popular posts from this blog

C Program to print the pattern

C Program to print the pattern X X Y X Y Z ........................................................................................ #include<stdio.h> #include<conio.h> void main () { int i , j ; for ( i= 88 ; i< 91 ;i ++) { for ( j = 88 ;j <= i ; j ++) { printf ( "%c" , j );                    }           printf ( "\n" ); } getch (); } ........................................................................................ OUTPUT

Deen Dayal Upadhyay Gorakhpur University RESULTS

DDU Gorakhpur results chahiye to yaha click kare                                                                       RESULTS  

RESULT DDU Gorakhpur 2016

Deen Dayal Upadhyay Gorakhpur University   Check Your Result click Here  B. Sc. Part-II 26-MAY-2016   M.Sc.(Ag.) Genetics & Plant Breeding (Final) 26-MAY-2016   M.Sc. ( Agriculture ) (Agricultural Economics ) - I 26-MAY-2016   B.A. Part-II 26-MAY-2016   B.A. Part-I 24-MAY-2016   B.A. Part-III 16-MAY-2016   B.Com. Part-III 16-MAY-2016   B.Sc. Home Science-I 16-MAY-2016   B.Sc. Home Science-II 16-MAY-2016   B.Sc. Home Science-III 16-MAY-2016   M.A Economics-Final 16-MAY-2016   M.A Hindi-Prev 16-MAY-2016   M.A Hindi-Final 16-MAY-2016   M.A History-Prev 16-MAY-2016   M.A. Ancient History, Archaeology and Culture (Previous) 12-MAY-2016   M.A. Ancient History, Archaeology and Culture (Final) 12-MAY-2016   B.Com.- I Year 12-MAY-201...