Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part B: The Game of Nim The game of Nim is an ancient game in which two players take turns removing items from the table.

image text in transcribed
image text in transcribed
Part B: The Game of Nim The game of Nim is an ancient game in which two players take turns removing items from the table. The player who removes the last remaining item loses. The constraints for our game of Nim are as follows: - The players for this game will be one human (the user) and one inanimate (the computer) - The game begins with 13 marbles on the table. The human player goes first. - When it is their turn, a player must take 1, 2, or 3 marbles off the table. No player can remove more marbles than are currently on the table. - We are not going to simulate any intelligence for this version of the game, so the computer randomly chooses to take 1,2 or 3 marbles on its turn. - The game continues until there is a winner. The following is one example of how game play might appear to the user: \$ Java Nim The object of the gase is to make your opponent take the last marble off the table. You get to choose between 1 and 3 marbles when it is your turn. There are 13 marble(s). How many wI11 you piek up? 1 There are 12 marble(s). Coeputer renoves 1 There are 11 marble(s). How many wil1 you pick up? 3 There are 8 narble(s). Conputer renoves 3 There are 5 marble (s). How many wil1 you pick up? 2 There are 3 marble s}. Conputer removes 1 There are 2 marble(s). How many will you pick up? 1 There are 1 narble(s). Conputer removes 1 . You won! Play againz (y) y There are 13 marble (s). How many will you pick up? 8 You must choose between 1 and 3. How many will you piek up? 2 There are 11 marble (s). Conputer renoves: 3 There are 8 marble(s). How many will you pick up? 3 There are 5 marble (s), Conputer renoves 3 There are 2 marble(s). How many wil1 you pick up? 3 You must choose between 1 and 2 , How many will you pick up? 0 You must choose between 1 and 2. How many will you pick up? 2 Sorry, you lost the gane Play again? (y) n Use a while loop to accomplish the turns - each turn is one time through the loop. Let's call this the "once-per-turn" loop. Use a variable to keep track of whose tum it is. You may use an int or boolean for this variable, whichever you prefer. (This is similar to "add versus subtract" in Part A.) Each time through the loop your code does the following: - If it is the human's turn, display a prompt for the user and call a private static method called getUserSelection(). This method enables the user to enter the number of marbles they wish to pick up. - The getUserSelection() method calls a private static method named isValidMove() to check whether the user's entry is between 1 and the maximum allowable move (which is 3 or the current number of marbles on the table, whichever is smaller). The isValidMove(0 method returns a boolean value. - The getUserSelection() method includes a while loop that continues as long as the user enters values for which isValidMove() returns false. - If it is the computer's turn, your main method calls a private static method called getComputerSelection(). This method generates and returns a random number between 1 and the maximum allowable move (as defined above). - Both getUserSelection[) and getComputerSelection[) must return an int value between 1 and 3 . - Once the number of marbles has been selected for the current move, reduce the number of marbles based on the selection, then determine whether the game is over (and if so, who won). - The "once-per-turn" loop repeats as long as the game is not over. Don't forget about incremental developmentl Similar to what I described in Part A of this assignment, you should employ quite a few iterations of "code a little, test a little" to get to the point where your program can play one complete game. Once you've got that working. put all of that functionality inside another while loop that lets the user play until they no longer wish to play again. This is the "once-per-game" loop. Don't forget to set the number of marbles to 13 and your "whose turn is it" variable to "the human" each time you begin a new game. With all these loops and decisions, be sure to comment your code well. You only need to write one class for Part B. Your class will include a main method, as well as the private static methods mentioned above. Provide sample output from playing two games with a single execution of your program. Include a winning game and a losing game. As I have done in the sample output on the previous page, this output should demonstrate that your program can handle multiple invalid attempts by the user to enter their selection for a given turn

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

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions

Question

=+8. Why is productivity important?

Answered: 1 week ago

Question

=+ 9. What is inflation and what causes it?

Answered: 1 week ago