In: Computer Science
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins.
Usage
The user can run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print out the initial pile of sticks as a set of pipes (|) side by side, followed by the number of sticks in parenthesis. Then, the game should greet the user, and then ask how many sticks to play with (must be an integer >= 10). The user always goes first.
On the user's turn, ask the user how many sticks to remove, and remove those from the pile. On the computer's turn, calculate the correct number of sticks to remove in order to ensure that the computer will (hopefully) win the game.
Play continues until there are no sticks left, at which point your program announces who won.You must show the set of sticks after each player's move. Show this as a set of pipes (|) side by side, followed by the number of sticks in (parenthesis), for example: ||||||| (7)
Illegal Moves
When the user is asked how many sticks to take, they must enter 1, 2, 3 or 4. If their entry is illegal (anything other than 1 2 3 or 4), remind them of their options, and ask once more. If they enter another illegal number, announce that they have forfeited the game, and exit the program. If the user entered a valid number the second try, gameplay continues (in this case, if later the user again enters an illegal number, you should again given them a second chance).
•the user may be badly behaved: they might enter anything at anytime, so your program must be prepared to handle any input (including non integers, etc.)
Algorithm
Your algorithm for picking sticks is simple: given n remaining sticks, pick (n mod 5) sticks, unless (n mod 5)=0; in that case, pick 1 stick.
IMPORTANT
Note that you must use fgets and sscanf to test this input. Do not simply compare the character to '1' '2 and '3' or use "atoi" The user might enter " 3" or "001" If sscanf accepts these as integers, then your program must also.
For this assignment, you are allowed to ignore extraneous input in the number of sticks to take. For example, if the user says "3 hahahayou" can take their input as “take 3 sticks” and ignore the “hahaha.” In class we’ll go over how to process input in a aay that handles all of these cases.
The code is below:
#include <stdio.h>
void print_sticks (int n) {
for (int i = 0; i < n; i++)
printf ("|");
printf (" (%d)\n", n);
}
int main(int argc, char *argv[])
{
int no_sticks = 0, i, remove;
char str [101];
if (argc == 2)
sscanf (argv[1], "%d", &no_sticks);
do {
printf ("Please enter the number of sticks (>=10):");
fgets (str, 100, stdin);
sscanf (str, "%d", &no_sticks);
} while (no_sticks < 10);
while (no_sticks > 0) {
print_sticks (no_sticks);
for (i = 0; i < 2; i++) {
printf ("Please enter number of sticks to remove (1,2,3,4):");
fgets (str, 100, stdin);
remove = 0;
sscanf (str, "%d", &remove);
if ((remove < 1) || (remove > 4) ) {
printf ("Incorrect number of sticks entered\n");
}
else
break;
}
if ( (remove < 1) || (remove > 4) ) {
printf ("You have forfeited the game\n");
return 0;
}
if (remove > no_sticks)
remove = no_sticks;
no_sticks -= remove;
print_sticks (no_sticks);
if (no_sticks == 0) {
printf ("You won!\n");
return 0;
}
remove = no_sticks % 5;
if (remove == 0) remove = 1;
printf ("Computer removing %d sticks now\n", remove);
no_sticks -= remove;
if (no_sticks == 0) {
printf ("Computer won!\n");
return 0;
}
}
return 0;
}