bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc030.c

luc030.c (2631B)


      1 /* Write a program for a matchstick game being played between the
      2 computer and a user. Your program should ensure that the computer
      3 always wins. Rules for the game are as follows :
      4     - There are 21 matchsticks.
      5     - The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
      6     - After the person picks, the computer does its picking.
      7     - Whoever is forced to pick up the last matchstick loses the game.
      8 */
      9 /* Let Us C, Chap- 5, Page - 87, Qn No.: B(c) */
     10 
     11 /* My Plan: The total number of matchsticks is 21. To guarantee a win,
     12 the computer ensures that after its turn, the number of remaining
     13 matchsticks is always a multiple of 5 plus 1 (i.e., 16, 11, 6, 1).
     14 This is achieved by making the sum of the player's pick and the
     15 computer's pick equal to 5 in each round. This forces the player
     16 to pick the final matchstick. */
     17 
     18 #include <stdio.h>
     19 int main()
     20 {
     21     int remaining_matchsticks = 21, player_pick, computer_pick;
     22     printf("                 --- Matchstick Game ---\n");
     23     printf("Rules: There are 21 matchsticks. You can pick 1, 2, 3, or 4.\n");
     24     printf("Whoever is forced to pick the last matchstick loses the game.\n");
     25     while (remaining_matchsticks > 1)
     26     {
     27         // game start
     28         printf("\n--------------------------");
     29         printf("\nRemaining Matchsticks : %d", remaining_matchsticks);
     30 
     31         // player pick and checking input is valid or not
     32         printf("\nYour turn: Pick 1, 2, 3, or 4 matchsticks: ");
     33         if (scanf("%d", &player_pick) != 1)
     34         {
     35             printf("\n\tPlease enter a number.");
     36             while (getchar() != '\n')
     37                 ;
     38             continue;
     39         }
     40 
     41         // checking player pick is valid or not
     42         if (player_pick < 1 || player_pick > 4)
     43         {
     44             printf("\n\tPlease enter a number among 1, 2, 3 and 4.");
     45             while (getchar() != '\n')
     46                 ;
     47             continue;
     48         }
     49 
     50         if (player_pick > remaining_matchsticks)
     51         {
     52             printf("\nInvalid choice! There are not enough matchsticks left.");
     53             while (getchar() != '\n')
     54                 ;
     55             continue;
     56         }
     57 
     58         // computer_picks
     59         computer_pick = 5 - player_pick;
     60         printf("\ncomputer_picks : %d", computer_pick);
     61 
     62         // remaining matchsticks
     63         remaining_matchsticks = remaining_matchsticks - (player_pick + computer_pick);
     64     }
     65 
     66     // game over
     67     printf("\n----------------------------------\n");
     68     printf("Only 1 matchstick is left.\n");
     69     printf("You are forced to pick the last matchstick. You lose!\n");
     70     printf("The computer wins.\n");
     71 
     72     return 0;
     73 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror