UNLV CS 135 Assignment #4 Fall 2022Program DescriptionIn this program you will be utilizing loops to implement a game of Nim. In this game, two players will take turns removing one, two,or...

1 answer below »
Please follow the rules of the PDF


UNLV CS 135 Assignment #4 Fall 2022 Program Description In this program you will be utilizing loops to implement a game of Nim. In this game, two players will take turns removing one, two, or three matches from a pile of 21 matches. The player who removes the last match loses. The program will take on the role of one of the two players, which we will label the “computer” player. The user can select to make the first move or allow the computer to go first instead. Although the game appears fair, it is actually possible to prevent the first player from ever winning. The computer player will utilize this strategy to maximize its chances of victory. The game must keep track of the following: The current player (the user or the computer) The number of matches remaining In order to facilitate the user's understanding of the game state, you will need to report that information before every move. Once there is only a single match remaining, the winner must be declared and the user will be given an opportunity to play another round of the game. 1 UNLV CS 135 Assignment #4 Fall 2022 Example Execution User input has been highlighted. The game of Nim. The player to remove the last match loses. Would you like to go first? (Y/N): y Player Turn - Matches: ||||| ||||| ||||| ||||| | Remove (1 - 3): 3 Computer Turn - Matches: ||||| ||||| ||||| ||| Computer removes 1. Player Turn - Matches: ||||| ||||| ||||| || Remove (1 - 3): 2 Computer Turn - Matches: ||||| ||||| ||||| Computer removes 2. Player Turn - Matches: ||||| ||||| ||| Remove (1 - 3): 3 Computer Turn - Matches: ||||| ||||| Computer removes 1. Player Turn - Matches: ||||| |||| Remove (1 - 3): 1 Computer Turn - Matches: ||||| ||| Computer removes 3. Player Turn - Matches: ||||| Remove (1 - 3): 2 Computer Turn - Matches: ||| Computer removes 2. Player Turn - Matches: | Game Over - Computer wins. Would you like to play again? (Y/N): N 2 UNLV CS 135 Assignment #4 Fall 2022 Program Flow 3 UNLV CS 135 Assignment #4 Fall 2022 Player Behavior The user will only have three types of choices to make during the game. The first will be whether they want to play first. The second will be how many matches they want to remove during their turn (1, 2, or 3). Finally, after the game concludes, the user will be asked if they want to play again. For the yes or no questions, you should accept the characters 'Y', 'y', 'N', or 'n' as valid responses. Assuming the computer player is behaving correctly, the player should never be in a position where they must choose from less than 3 matches. As such, you do not have to include any special behavior for that case. In case of any unacceptable inputs from the player, you should display an appropriate error message and ask them again before continuing on. (See the Error Checking section below) Error Checking Use the following error messages in your program when validating the user's input. The first error message should be utilized for both yes/no questions. The second should be used in the case the input stream fails due to a non-integer value being typed into the console. The last two should be used if the user inputs a value out of the allowed range. Use do while loops when asking the user for inputs. Input Error Message Answer is not Y,y,N,n Please, answer Y or N. Non-numeric value Please, type 1, 2, or 3 as your response. Integer less than 1 Must remove at least one match. Integer greater than 1 Cannot remove more than three matches. Printing Matches When displaying the state of the game, the matches should be represented by the pipe character |. Use a for loop to print the matches in groups of five. Matches: ||||| ||||| ||| After each group of five, output a space. This includes if the remaining matches are a multiple of five: 5, 10, 15, and 20. The number of matches should also be displayed after declaring a winner to show the final game state. 4 UNLV CS 135 Assignment #4 Fall 2022 Computer Player Behavior When it is the computer player's turn, they will use the following logic to determine how many matches it should take. Divide the number of matches by 4 and check the remainder using the % operator. Matches Remaining % 4 Matches to RemoveMatches to Remove 0 3 1 1 2 1 3 2 21 % 4 = 1 Computer Removes 1 20 Matches Remaining 20 % 4 = 0 Computer Removes 3 17 Matches Remaining 19 % 4 = 3 Computer Removes 2 17 Matches Remaining 18 % 4 = 2 Computer Removes 1 17 Matches Remaining Note that the computer always sets the remaining matches to a value that is 1 greater than a multiple of 4 when able. Consider playing the game with the matches displayed as below to understand why. Have the player go first and take from the right side. | |||| |||| |||| |||| |||| Game Loop The main part of the game will loop once for each turn of the game. Either the player or computer will take a single turn during each iteration of the loop. You will need to display the current game state during each turn. Include the current player and the number of matches remaining. After the current player has taken their turn, switch the current player value. Check to see if only a single match is remaining to determine if the game should come to an end. Play Again Loop Since the player has the option of restarting the game, you must include a loop that will return the program to the beginning of the game. This loop should start at asking the player whether they want to play first and encompass the rest of the program. Do not include the line explaining how the game works. Make sure that the number of matches is reset to 21 if starting again. 5
Answered 1 days AfterOct 15, 2022

Answer To: UNLV CS 135 Assignment #4 Fall 2022Program DescriptionIn this program you will be utilizing...

Robert answered on Oct 16 2022
52 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here