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

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.

Solutions

Expert Solution

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

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 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...
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