Q.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>
#include<conio.h>
void main()
{
int ms=21,ply,sys=0;
clrscr();
fflush(stdin);
printf("Select between 1-4\n otherwise computer wins\n");
while(ms>=1)
{
if(ms==1)
{
printf("Computer wins\n");
break;
}
printf("Avilable Matchsticks = %d\n",ms);
printf("Player= ");
scanf("%d",&ply);
printf("\nComputer= %d\n",sys=5-ply);
ms=ms-sys-ply;
}
getch();
}
void main()
{
int ms=21,ply,sys=0;
clrscr();
fflush(stdin);
printf("Select between 1-4\n otherwise computer wins\n");
while(ms>=1)
{
if(ms==1)
{
printf("Computer wins\n");
break;
}
printf("Avilable Matchsticks = %d\n",ms);
printf("Player= ");
scanf("%d",&ply);
printf("\nComputer= %d\n",sys=5-ply);
ms=ms-sys-ply;
}
getch();
}
No comments:
Post a Comment