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...
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:...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT