Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: stickpuzzle.c
- Copyright:
- Author: Mr.Kindle
- Date: 06-12-22 23:35
- Description: Given 21 Matchsticks and 2 users, A and B (computer and user respectively).
- Users and computer both can pick matchsticks not more than four at a time. The one who is forced to
- pick the last matchstick loses.Make an algorithm so that computer always win.
- Given that user pickup the stick first.
- This code run automatically, just keep pressing enter key.
- */
- #include<stdio.h>
- int main()
- {
- int stick,u,c;
- stick = 21;
- srand(time(0));
- while(stick)
- {
- printf("number of stick: %d\n",stick);
- if(stick == 1)
- { printf("\n*****************************\n\n");
- printf("\nYOU HAVE TO PICK UP THE LAST STICK\n");
- printf("YOU LOOSE BETTER LUCK NEXT TIME\n");
- break;
- }
- printf("\nPress Enter to run further: \n");
- getch();
- u = rand()%4 + 1;
- printf("user picked up : %d\n",u);
- stick = stick-u;
- /*Now computer will pickup the stick*/
- c = 5-u;
- printf("computer picked up: %d\n",c);
- stick = stick - c;
- }
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement