Advertisement
Mr_kindle

stickpuzzleauto.c

Dec 6th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | Source Code | 0 0
  1. /*
  2.     Name: stickpuzzle.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 06-12-22 23:35
  6.     Description: Given 21 Matchsticks and 2 users, A and B (computer and user respectively).
  7.     Users and computer both can pick matchsticks not more than four at a time. The one who is forced to
  8.     pick the last matchstick loses.Make an algorithm  so that computer always win.
  9.      Given that user pickup the stick first.
  10.      
  11.      This code run automatically, just keep pressing enter key.
  12.  
  13. */
  14.  
  15.  
  16.  
  17. #include<stdio.h>
  18.  
  19.    
  20.  
  21. int main()
  22. {
  23.     int  stick,u,c;
  24.  
  25.     stick = 21;
  26.     srand(time(0));
  27.    
  28.     while(stick)
  29.     {
  30.        printf("number of stick: %d\n",stick);
  31.         if(stick == 1)
  32.            {    printf("\n*****************************\n\n");
  33.                 printf("\nYOU HAVE TO PICK UP THE LAST STICK\n");
  34.                 printf("YOU LOOSE BETTER LUCK NEXT TIME\n");
  35.                 break;
  36.             }
  37.         printf("\nPress Enter to run further: \n");
  38.         getch();
  39.        u = rand()%4 + 1;
  40.        printf("user picked up : %d\n",u);
  41.      
  42.        stick = stick-u;
  43.     /*Now computer will pickup the stick*/
  44.        c = 5-u;
  45.        printf("computer picked up: %d\n",c);
  46.        stick = stick - c;
  47. }
  48. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement