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.
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.
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;
}
#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
So far we’ve seen sequential flow of our programs. Next we saw decision control statements like if, if else, else if etc. Now lets learn about loop control statements present in C programming language.
In this video tutorial lets learn about while loop in particular.
Here while is the keyword. Inside parenthesis we need to write the condition. These conditions must evaluate to Boolean value. i.e., true(non-zero number) or false(0);
The ‘body of while’ loop keeps executing until the condition is true. Control exits while loop once the condition is false.
#include < stdio.h >
int main()
{
int count = 1;
while(count <= 5)
{
printf("%d\n", count);
count = count + 1;
}
printf("End of loop\n");
return 0;
}
Output: 1 2 3 4 5 End of loop
In above C source code, variable count is initialized to 1. Inside while condition we check if variable count is less than or equal to 5. Until variable count is less than or equal to 5, the while loop keeps executing. Inside body of the while loop we increment the value of count by 1 upon each execution of the loop. The condition is checked on execution of the loop. Once variable count is more than 5, the condition becomes false count <= 5 (once count value is 6, the condition becomes false), the execution control exits while loop.
Note: Here the loop execution counter is called ‘loop counter’ or ‘index variable’.
If there are any documents to iterate inside cursor object, then hasNext() will return true orelse it’ll return false. If hasNext() returns true, then we can iterate through the documents using next() method on the cursor object.
We could modify the cursor object using methods like sort(), limit() and skip(). In above example, we are modifying cursor object using sort() method, and we are sorting it in reverse lexicographical order on the name field.
Here we chain the methods sort(), limit() and skip(). We are sorting in reverse lexicographical order on the name field, then skipping the first 2 documents and then limiting the result/output to 5 documents.
The order in which these 3 methods are applied are: First sort, then skip and then limit. Also note that, these methods modify cursor object at the server side and not on client site.