Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I need help with this programming project, I have it pretty much all figured out but I'm having some trouble with the play_nim and get_computer_move

I need help with this programming project, I have it pretty much all figured out but I'm having some trouble with the play_nim and get_computer_move functions. Thank you in advance!

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

Problem Statement The game of Nim has existed in various forms since at least the 16th century (according to Wikipedia, so it must be true). It is fairly simple, yet still provides a challenge. Here are the rules for our version 1. The game board" consists of three heaps of stones initially containing 3, 4, and 5 stones each 2. Players alternate turns removing stones from a single heap each turn. 3. A player may remove as many stones as they wish in a single move, but they must all come from the same heap. 4. The player that removes the last stone wins. Our program will label the heaps as 'A', 'B'. and 'C. A move will be entered as the heap letter followed by the number of stones to be removed. For a move to be legal, the letter must be "A. 'B', or ,C", and the number that follows must be greater than 0 andless than or equal to the number of stones remaining in that heap. See the sample run for some illustrated games. As with Project 2, we want to create a program to allow a human to play this game against a computer opponent. This computer opponent will use a perfect strategy. (Ifit goes first, it will always win. If it goes second, and the human makes a mistake, it will win.) If you want to know more, the Wikipedia article provides more information than you will ever need. The program should allow the player to play as many games as she likes. For each new game, the loser of the previous game goes first. Therefore, if both players play perfectly, the games will be split even Program Specifications Like the last project, you are given the program specification in the form of analysis and design ideas for a series of functions that must be implemented. Each function accomplishes one task needed for this program. You should write a function and then test it with a mainprogram that ensures that the function works by itself. Once the function is working, move on to the next function, which will require a different main function to test it. Once all of the individual functions work, the final main function that actually will play the game is written. Function: print greeting Analysis: No objects Design: This function's task is simply to print out the greeting at the beginning of the program. See the sample run for the exact text format. It should only be run once, when the user first runs the program, not at the start of each game. Function: user_wants to_play_again Analysis, Design, and Implementation covered previously Function: print scoreboard Analysis Objects number of computer wins number of user wins Type int int Name Movement received received wins ser wInS Design: This function's task is to print out the current number of wins by the computer and by the user. See the sample run for the exact text format Function: play nim Analysis Objects Number of the player that starts Number of the player that won Type int int Movement received retumed Name starting player nner Design: This function's task is to play one game of Nim with starting player going first.. The design of much of this function is left up to you However, bere are some hants .You will need three local integer variables to hold the count for each of the three heaps throughout the game .You will need local variables to hold both the letter of the heap being chosen and the number of stones being removed. .You will need a variable that tracks which player's turn it is. .You will need to only allow legal moves by the user. A move is legal if .The heap chosen is 'a'. 'b'. or 'c'(upper or lowercase) .The number of stones chosen to be remove is greater than zero and less than or equal to the number of stones in the chosen heap. Note that scanf can be used to read in more than one input. Just use a specifier for each input, and list the input variables in the same order after the specifier string. Also, remember that there should be a space before the %c specifier .If it is the computer's turn, you must call get computer move (see next function) to find the move that the computer has chosen. NOTE: While writing this function, you might by pass this step and simply allow the human to enter moves for both players in order to simplify debugging. The function loops until all of the heaps are empty. When this occurs, the player who ust moved (i.e., the one who removed the last stone) is the winner and the number of that player is returned to the calling function. . Function: get computer_move Analysis Objects Three heaps of stones Computer's chosen heap Computer's chosen number of stones int Type int char Movement received passed back chosen heap passed back number to_remove Name heap a heap_b, heap Note: passed back parameters will be covered in class on Tuesday, February 12. They are in Chapter 6 of the textbook and are called "output parameters" there Design: The winning strategy for this game relies on counting and canceling out powers of two. Again, read the Wikipedia article if you are interested in the details. The short answer is that we can find what is called the Nim number of the game by computing the bitwise-exclusive-or (XOR) of all the heaps. Fortunately, C has an operator that can do this () (not exponentiation). If we can make the Nim-number of the game be 0 after every move, we are guaranteed to win. Here is the algorithm: 1. Calculate the Nim-number of the game by XOR-ing all the heaps together. (e.g. nim number 2. If that number is already 0, we do not have a good move, so simply remove 1 stone from the first heap 3. Otherwise, we need to find a move that changes the Nim-number to 0. To do this we perform the heap a A heap b A heap c that has one available. (We are going to try to stall for time following until we find a heap that works: (a) If (heap A nim number) heap, for any heap then this is the heap that we will remove stones from. (b) The number of stones to remove will be heap-(heap A nim number) Main Program Analysis Objects Number of wins by the computer Number of wins by the user Starting player number (initially the user) ype int int int Name omp_ wins user wins starting player Design: The main function for this program ends up being fairly simple and similar to the Guessing Game. Most of the complicated logic is in the other functions. Main needs to keep track of the number of wins for each player and which player is the starting player for the next game. It needs to repeatedly call the play_nim function, record the winner, print out the current scoreboard, and ask the user if they want to play again (by calling that function) until the user is done playing. Assignment Write a C program that implements the game of Nim. Your program must follow the specifications given above to earn full credit. That is, your program must define and use at least the specified functions. (The names of functions and variables do not have to be exactly the same.) The output of the program must conform exactly to the following example run (now that we have loops, there is only one sample run; user input shown in bold) Note there is blank line before each player's move, before the scoreboard dis play, and before the "Do you want to play again... prompt, and there is one space after the first sentence punctuation in a two sentence line of output. (Eg. in the error messages.) And as usual, there must be a newline after the last line of output Welcome to the Ancient Game of Nim Player 1 is you (the human) Player 2 is me (the computer) Player 1 goes first this time! Player 1 Heaps: Enter the letter of the heap and number of stones to remove: c2 Player 2 Heaps: COMPUTER Moves a1 Player 1 Heaps: Enter the letter of the heap and number of stones to remove: B2 Player 2 Heaps: B: 2 COMPUTER MOves a1 Player 1 Heaps: B: 2 Enter the letter of the heap and number of stones to remove: D4 Illegal move! Try again Player 1 Heaps: Enter the letter of the heap and number of stones to remove: c2 Illegal move! Try again Player 1 Heaps: Enter the letter of the heap and number of stones to remove: cl Player 2 Heaps: COMPUTER moves al Player Heaps: B: 2 Enter the letter of the heap and number of stones to remove: bl Player 2 Heaps: COMPUTER moves al Player 1 Heaps: C:0 Enter the letter of the heap and number of stones to remove: b1 Player2 Heaps: COMPUTER moves al Player 2 wins! Current Score Player (Human): 0 Player 2 (Computer): 1 Do you want to play again? (y) Y Player 1 goes first this time! Player 1 Heaps: B: 4 Enter the letter of the heap and number of stones to remove: a5 #include 10 #include 11 #include 12 13 void print_greeting (void); 14 int user_wants_to_play_again(void); 15 int print scoreboard(void); 16 int play_nim (void) 17 18 int main 19 20 21 print_greeting(); user_wants_to_play_again); print scoreboard); return 0 23 24 25 void print greeting(){ 26 27 h 28 int user_wants_to_play_again(){ 29 30 31 32 printf("Welcome to the Ancient Game of Nimln"); char response; // 1oop control variable 11. Do the following in a loop 0 1.1 Ask the user if they want to play again printf("Do you want to play again (y)?:") scanf(" %c'. &response); // initialization and update response - tolower(response); if ((response- 'y') && (response 'n')) 34 35 36 37 38 39 40 41 42 43 1.2 Convert user response to lowercase 1.3 Test to see if it is not 'y' or 'n 1.3.1 Print an error message printf("Please enter a y or a nln") /12. While the response is not 'y' or 'n' } while( (response !- 'y') && (response != 'n')); // loop condition /1 3. Return true if the response is 'y'; false if response is 'n return response'y'; // Boolean expression that evaluates to true/false 45 int print_scoreboard) int comp-wins = 0, user_wins-0; printf( "**n") printf("Current score: "); printf! "Player 1 (Human): %d ", @gr.wi.ns); printf("Player 2 (Computer: %d ", comp-wins); return 0 int play nim)f int starting_player, return 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions