Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is an assignment I was given for a computer science class. I'm having trouble figuring out how to get specifics to work. You need

This is an assignment I was given for a computer science class. I'm having trouble figuring out how to get specifics to work.

You need to write a bash shell script named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2 or 3 sticks from a pile. Whoever picks the last stick wins.

Usage You run stick without any command line arguments, i.e. stick The game should greet the user, and then ask how many sticks to play with (must be an integer >= 10), and who should go first (c for computer, u for user). An invalid answer should cause the program to re-ask the question (as many times as necessary). On the users turn, ask the user how many sticks to remove, and remove those from the pile. On the computers turn, calculate the correct number of sticks to remove in order to ensure you 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).

Illegal Moves When the user is asked how many sticks to take, they must enter 1, 2 or 3. If their entry is illegal (out of range, not a number, or missing), remind them of their options, and ask once more. If they enter another illegal number, announce that they have forfeited the game, and terminate 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).

Algorithm Your algorithm for picking sticks is simple: given n remaining sticks, pick (n mod 4) sticks, unless (n mod 4)=0; in that case, pick 1 stick.

Here's the code I've tried to write, I deleted parts because they weren't working properly:

image text in transcribed

rfinn@localhost: - echo Hello, welcome to pick up sticks. How many sticks would you like to play with read stickNum echo $stickNum while [I $stickvum -1t 10 11 do echo How many sticks would you like to play with? read stickNum echo $stickNum done echo Would you like to go first or should the computer? read TO echo $TO until [ STOu II I STOc11 do echo Would you like to go first or should the computer? read TO echo $TO done stickNumCnt=$(($stickNum echo SstickNumCnt % 4)) then if SstickNumCnt0 11 then stickNum=$ ( ($stickNum- 1)) echo $stickNum fi if [[ $stickNum != 0 ]] then stickNum=$ ( (stickNum- SstickNumcnt) ) echo $stickNum fi fi echo This is u. then while [[ $sticknum != 0 ]] do echo How many sticks would you like to take? read uSticks stickNum-s ((SstickNum-usticks)) stickNumCnt-s ((sstickNum echo $stickNum % 4)) if SstickNum0 11 then rfinn@localhost: - echo Hello, welcome to pick up sticks. How many sticks would you like to play with read stickNum echo $stickNum while [I $stickvum -1t 10 11 do echo How many sticks would you like to play with? read stickNum echo $stickNum done echo Would you like to go first or should the computer? read TO echo $TO until [ STOu II I STOc11 do echo Would you like to go first or should the computer? read TO echo $TO done stickNumCnt=$(($stickNum echo SstickNumCnt % 4)) then if SstickNumCnt0 11 then stickNum=$ ( ($stickNum- 1)) echo $stickNum fi if [[ $stickNum != 0 ]] then stickNum=$ ( (stickNum- SstickNumcnt) ) echo $stickNum fi fi echo This is u. then while [[ $sticknum != 0 ]] do echo How many sticks would you like to take? read uSticks stickNum-s ((SstickNum-usticks)) stickNumCnt-s ((sstickNum echo $stickNum % 4)) if SstickNum0 11 then

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

=+why do you hold the view that you do?

Answered: 1 week ago