Question

In: Computer Science

For this assignment, I need to write a c program named stick which plays a matchstick-picking...

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.

Solutions

Expert Solution

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);
}

Related Solutions

For this assignment, I need to write a c program named stick which plays a matchstick-picking...
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...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
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...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
I need to write a program in C with the following specification * ​​​​​​​Line one of...
I need to write a program in C with the following specification * ​​​​​​​Line one of the standard input will have the total number of people which must not be greater than 10 * Each of the subsequent lines of input will be the first name of a person and their number (ex. "Adam 85") one space between name and number * Each name must be a maximum of 14 characters * Put the first names into an array called...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT