bsc

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

luc030.c (2822B)


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