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

Canvas clearRect: HTML5

Rectangle is the only primitive type supported by canvas.

It has 3 functions:
strokeRect(x, y, w, h);
fillRect(x, y, w, h);
clearRect(x, y, w, h);

In our previous video tutorials we’ve seen using strokeRect and fillRect methods. In this short video tutorial, we’re illustrating clearRect() method.

clearRect method erases the given rectangle and makes the area fully transparent.

index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5

JavaScript file
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context         = myCanvas.getContext("2d");
 
context.fillStyle = "red";
context.fillRect(10, 10, 75, 75);
 
context.fillStyle = "blue";
context.fillRect(90, 10, 75, 75);
 
context.clearRect(5, 40, 200, 40)
}
}

Here we first draw 2 rectangles: red and blue.
Next clear part of these two rectangles using clearRect method.

We start clearRect() from 5px x-axis, and 40px y-axis; with a width of 200px, so that it covers both red and blue rectangles.

Canvas clearRect: HTML5


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

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



Advantages of clearRect() method may not seem obvious at first, but will be helpful in game development – to draw transparent paths between your drawings/graphics.