Answered step by step
Verified Expert Solution
Question
1 Approved Answer
programing Chow to write this part of codefollowing are sample running result thank you Function: print scoreboard Analysis Objects number of computer wins number of
programing Chow to write this part of codefollowing are sample running result
Function: print scoreboard Analysis Objects number of computer wins number of user wins Type int int Movement received received Name comp_wins user_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 ninm Analysis Objects Number of the player that starts Number of the player that won Type int int Movement received returned Name starting player winner 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, here are some hints: .You will need three local integer variables to hold the count for each of the three heaps throughout the gamc 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 turm 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 bypass 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 just 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 Name received passed back chosen heap passed back number to remove heap a, heap b, heap.c 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: l. 2. 3. Calculate the Nim-number of the game by XOR-ing all the heaps together, eg. nin-number- heap a heap_b A heap c;) If that number is already 0, we do not have a good move, so simply remove 1 stone from the first heap that has one available. (We are going to try to stall for time). Otherwise, we need to find a move that changes the Nim-number to 0. To do this we perform the following until we find a heap that works: (a) If (heap A nim number) thank you
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started