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
You 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.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int n;
//greeting the user
printf("\n\t...................Welcome to the game of sticks................\n\n");
//ask user to enter the no of sticks to start with
sscanf(argv[1], "%d", &n);
printf("No. of sticks to start with: %d\n", n);
while (n < 10)
{
printf("Please choose no of sticks >= 10\n");
scanf("%d", &n);
}
//variable to store remaining sticks in pile
int rem_sticks = n;
//show initial pile
for (int i = 1; i <= rem_sticks; i++)
printf("|");
printf("(%d)\n\n", rem_sticks);
//input variable for user inputs during the game
char input[100];
//variable to store warnings
int warnings = 0;
//ask user to choose no. of sticks to remove
printf("Choose the no. of sticks to remove!\n");
//take continuous input from user
//until user presses ^Z
while (fgets(input, 100, stdin))
{
int choice = 0;
//take the first word from the input string
char *tk = strtok(input, " ");
sscanf(tk, "%d", &choice);
//if choice is an illegal move
if (choice != 1 && choice != 2 && choice != 3 && choice != 4)
{
//if user has enetered wrong inputs two times consecutively
//exit the game
if (warnings >= 2)
{
printf("You have fortfeited the game!\n");
return 0;
}
printf("Illegal move!\nLegal moves are: 1, 2, 3 and 4. Enter again: ");
continue;
warnings++;
}
//if the move is legal
else
{
//if the move is greater than the remaining sticks in pile
if (choice > rem_sticks)
{
printf("You have entered greater than what are left! Enter again: ");
continue;
}
else
{
rem_sticks = rem_sticks - choice;
//user wins
if (rem_sticks == 0)
{
printf("Wohoo! You won!\n");
return 0;
}
}
}
//computer's turn
int com_ch = rem_sticks % 5;
if (com_ch == 0)
com_ch = 1;
if (com_ch > rem_sticks)
com_ch = rem_sticks;
rem_sticks = rem_sticks - com_ch;
printf("Computer chooses %d\n", com_ch);
//if computer wins
if (rem_sticks == 0)
{
printf("Computer wins!\n");
return 0;
}
//show remaining pile
for (int i = 1; i <= rem_sticks; i++)
printf("|");
printf("(%d)\n\n", rem_sticks);
//ask user to choose no. of sticks to remove
printf("Choose the no. of sticks to remove!\n");
}
return (0);
}