21 Matchstick Game: C Program

Write a C program for a 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.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C

Logic of 21 Matchstick Game C Program

There are in total 21 match sticks to start the game. First we ask the user to pick either 1 or 2 or 3 or 4 matches per pick. Once the user makes his/her pick, computer makes the picking(same rules apply to the computer i.e., it can pick either 1 or 2 or 3 or 4 matches per pick). The trick is, computers pick is always 5 minus the pick of the user. For example, if computers pick is variable c and user pick is stored in variable p, then:
c = 5 – p;
This makes sure computer always wins the game. That is, the last pick will always be of the user.

Note: We have 1 as the condition in while loop to make sure the while loop keeps executing until a break statement is occurred inside the loop to terminate the execution of the loop. while(1) is considered as an infinite loop(unless we have some ways to break out of the loop programmatically).

Note:
break; breaks out of the loop or terminates the execution of the loop.

continue; skips execution of all the code after it in the loop and goes for the next iteration of the loop.

Video Tutorial: 21 Matchstick Game: C Program


[youtube https://www.youtube.com/watch?v=VO_5XTLxKmc]

YouTube Link: https://www.youtube.com/watch?v=VO_5XTLxKmc [Watch the Video In Full Screen.]

Source Code: 21 Matchstick Game: C Program

#include<stdio.h>

int main()
{
    int m = 21, p, c;

    while(1)
    {
        printf("\nNumber of Match sticks left = %d\n", m);
        printf("Pick 1 or 2 or 3 or 4 matches\n");
        scanf("%d", &p);

        if(p > 4 || p < 1)
            continue;

        m = m - p;

        printf("Number of matches left = %d\n", m);

        c = 5 - p;

        printf("out of which computer picked up %d\n", c);

        m = m - c;

        if(m == 1)
        {
            printf("\nNumber of matches left = %d\n", m);
            printf("You lost the Game\n");
            break;
        }
    }

    return 0;
}

Output:
Number of Match sticks left = 21
Pick 1 or 2 or 3 or 4 matches
2
Number of matches left = 19
out of which computer picked up 3

Number of Match sticks left = 16
Pick 1 or 2 or 3 or 4 matches
3
Number of matches left = 13
out of which computer picked up 2

Number of Match sticks left = 11
Pick 1 or 2 or 3 or 4 matches
1
Number of matches left = 10
out of which computer picked up 4

Number of Match sticks left = 6
Pick 1 or 2 or 3 or 4 matches
4
Number of matches left = 2
out of which computer picked up 1

Number of matches left = 1
You lost the Game

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Programming Interview / Viva Q&A: 5 (while loop)

In this video tutorial lets understand the while loop a little better. We’ve posted source code of a C program below which has while loop. You need to guess the output of the program.

Related Read:
while loop in C programming

Guess output of the program

 

int main()
{
    int count = 0;

    printf("Start of program\n");

    while(count <= 5);
    {
        printf("Count = %d\n", count);
        count = count + 1;
    }

    printf("End of program\n");

    return 0;
}

Output:
Start of program

Since above C source code has semicolon(;) immediately after while(count <= 5) the control enters infinite loop or indefinite loop, and no other statements after the semicolon gets executed. And the program doesn’t get terminated with return 0.

C Programming Interview / Viva Q&A: 5 (While Loop)


[youtube https://www.youtube.com/watch?v=fqYykTU-jrA]

YouTube Link: https://www.youtube.com/watch?v=fqYykTU-jrA [Watch the Video In Full Screen.]


If you remove the semicolon(;) after while(count <= 5) then the program will output numbers from 0 to 5 and the “End of program” statement.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert