Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview of all the Requirements We want to write a program that will allow a human to play a little number game against the computer.

image text in transcribed
image text in transcribed
image text in transcribed
Overview of all the Requirements We want to write a program that will allow a human to play a little number game against the computer. The game is easy, we start with 21 available coins. Both the computer and the player then take turn taking between 1 to 3 coins (inclusivel. The player who plches the last remaining coin wins. At the end, the program displays a message identifying who wore computer or human Feature #1 - Bare Necessities (total 1 point] Write a Java program with an empty class and empty main method that compiles and runs successfully even though it is really not doing anything at this point). [1 point) Feature #2 - Keep track of the winner & congratulate them (total 2 points) Keep track of who is winning with a whoWon integer variable, Initialized to 0 [1 point). At the end of your program, display a different message depending on the value inside the variable [1 point) . For value 1, display "The computer won, bow to your Al overlord" For value 2, display instead "The human player won this time... . For any other value display "Nobody won something went wrong" To test this feature, temporarily add a line of code assigning one of these values to the variable who won right before your IF-ELSE stotements. Recompile and re-run the code, trying different values to make sure your program works in all relevant cases Remember to remove this assignment once you are sure that your code is working property and you are ready to start working on the next feature Feature #3 - Adding our game loop (total 3 points) Add a loop to your program that will keep iterating as long as the variable whoWon is equal to 0 1 point) Inside this loop, strt by displaying the number of coins remaining (1 point). You will store that value inside a variable named colnshat you will initialize to 21 before the loop (1 point) To test this feature simply set whoWon to one of the expected values (1 or 2) right before the end of the loop. That will allow you to ensure that the loop starts and stops after a single Iteration. Remember to remove this assignment once you are sure that your code is working properly and you are ready to start working on the next feature Feature #4 - Computer playing alone (total 4 points) Add some code in your loop so that the computer plays the same alone. Start by senerating a random integer number between 1 and 3 that you will store in an integer variable take that you will also have to declare and initialize (1 point). Reminder - The expression to obtain such a random value is (int)(Math.random() * 3) - 1 la marca cinematest Manichilleranteed bathouskoneitsbeintl Add some de nuestras the Sortir dom inte har between tand out with an integritet with how lode and we der Theotondo . Det Now we Dewed by the end Deduct that to the cost and whether is now or if that is the conto the writio what o te thote the state cancer the travail.com descombe cater the eyes when the computer takes the last Feature #5 - Dummy simulated human player joins the game (total 5 points) At the egg your program decare an anger the whole toit point that can be will decat whose comment Ind the moon check the contents of wholdest . c. 1. Next to 2 so that during the extention the human love will be able to put. There the code you wided for feature the computer is playing its tumit point contai2t whole tow that the next ration, the computer will be tre le play it point. This matter wiltumapayer w by mot1 You will remove the right stament once you start working on the extrem de stinget.check Whether to continue thithen the variable wheWonostics that human prayer it took the linkcond met Feature #6 - Real human player joins the game (total 3 points) Meplate med manger de that my decreciente coins by 1 by code that intet om te wetentera le between 1 and indepot invatation the well point and storia w wariable red you Decide that you will showeto de care and we tot the beaching of your era por last but not leastdecentrum of remaining or by me in Decide on Messert that for now, we oume that the human player was provides a valid lang value between and che Feature #7 - Never trust users (total 5 points) but we now want to make our programa be more robust to humano If there than 1, we wildebymesse miding them that they need to tale con due to their hom so they entera legater than 3 wedstava estareminding them that they are more con during each of their turn it on How we want to go further and make that the progom ke prometing the return the provide a proper tween 1 and Inclusive to realize the last feature, introduce another lop, decore any warbles you might red and determine where your code hede in the program points! O a G Add some code in your loop so that the computer plays the same alone. Start by generating a random integer number between 1 and 3 that you will store in an integer variable take that you will also have to declare and initialize (1 point) Reminder - The expression to obtain such a random values Unt Math.random 3) + 1 Display a message statinis "Computer takes N coins where will be replaced by the value of take 1 point), Deduct the value of take from the variable coins (1 point) and check whether coins is now 0 or less. If that is the case, assign 1 to the variable whoWon so as to indicate that the computer just took the last coin and therefore won (1 point) To test this feature, simply run the same and read its output on the console to ensure the number of available coins decreases correctly, based on the rondom number taken by the computer at each lum, and that the same actually ends when the computer takes the last coin Feature #5 - Dummy simulated human player joins the game (total 5 points) At the beginning of your program, declare and initialize an integer variable wholsNext to 1 (1 point). That variable wit indicate whose turn it is computer or human pluyer). Inside the game loop, check the contents of wholsNext . If it contains 1, set wholsNext to 2 so that, during the next iteration, the human player will be able to play (1 point). Then, execute the code you added for feature #4 (the computer is playing its turn) (1 point) If it contains 2 set wholsNext to 1 so that during the next iteration, the computer will be able to play f1 point). Then, simulate (for now) what a human player would do by simply decrementing coins by 1. You will remove this assignment statement once you start working on the next feature and are done testing this one. Next, check whether the value of coins is lesser or equal too. If it is, then assign 2 to the variable whoWon so as to indicate that human player just took the last con and therefore won (1 point) Feature #6 - Real human player joins the game (total 3 points) Replace the simulated human player" code that simply decremented coins by 1 by code that, instead, prompt the user to enter a value between 1 and 3 inclusive) [1 point). read an int value from the user [1 point), and store it in a new variable named you Decide that you will also have to declare and initialize too at the beginning of your program (1 point). Last but not least, decrement the number of remaining coins by the value in youDecide [1 point). Please note that, for now, we are going to assume that the human player always provides a valid value, that is an integer value between 1 and 3 (inclusive) Feature #7 - Never trust users [total 5 points) Last but not least, we now want to make our program a bit more robust to human error If the user enters a value lesser than 1, we will display a message reminding them that they need to take at least 1 coin during each of their turn (1 point). If they enter a value greater than 3, we display a message reminding them that they cannot take more than 3 coins during each of their turn (1 point) However, we want to go further and make sure that the program keeps prompting the user to enter a number of coins until they provide a proper value between 1 and 3 (inclusive). To realize this last feature, introduce another loop, declare any variables you might need and determine where your new code should be added in the program [3 points)

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions