Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need to see a code screenshot for this program, please use comments! Problem Statement The game of Nim has existed in various forms since at
Need to see a code screenshot for this program, please use comments!
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 and less 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. (If it 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 nevw game, the loser of the previous game goes first. Therefore, if both players play perfectly, the games will be split evenly. 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 main program 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 obiects 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 agairn Analysis, Design, and Implementation covered previously. Function: print scoreboard Analysis Note: passed hack parameters will he covered in class on Tuesday, Fehruary 12. They are in Chapter 6 of the extbook and are cnlled "output parameters" there Objects number of computer wins number of user wins Type Name Design: The winning strategy for this game relies on counting and canceling out powers of two. Again, read the Wikipedia article iy aineresled in the details. The short answer is thal we can ind whal is called the Nim mumher of the game by computing the hitwise-exclusive-or (XOR) of all the heaps Forlunately, C has an operator thal can do this( (not eaponentiation) If we can make the Nim-number of the game be0aerevery nove, we euadin int reccived received Design: This function's las is to print out the current numher of wins by the compter and by he user. See the smple run for the exact text format. Here is the algurithm . Calculate the Nim-uumber of the game by XOR-ing all the heaps togcther e g. nim number - 2. If that number is already 0, e do not have a good move, so simply remove I stone from the first heap 3, Otherwise, we need to find a move that changes the Nim-number to O. To do this we perform the Function: play nim Objects Numher of the player that starts Number o the player thal won heap a A heap b A heap ci) that has one available. (We are going to try to stall for time. following until we find a heap that works: pe Name receivedd (a) If (heap A nim number) heap. fo any heap the his is the heap that we will eove stones from. b) The number of stones to remove will be heap Design: This function's task is to play one game of Nim with starting player going first.. The design of much of .You will need three local integer variables to hold the count for cach of the three heaps throughout the .You will need local variables to hold both the letter of the heap being chosen and the number of stones (heap nin-umber) . this functionis left up to you Main Program Objocts Nunher l wins by the compuler Nurnher olwins y the user Slarting player number (nitially the user) being removed. You will need a variable thal racks which player's lun it is. .You will need to only allow legal moves by the user. A move is legal if. The leap chosen is 'a', 'b', of 'c' uppel or lowercase) . .The number of stones chosen to be remove is greater than zero and less than or equal to the slarting player number of stones in the chosen heap. Note that scanf can he used to read in more than one inpt. Just use a speifier 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. Desigm: The main unction for this prgrarn ends up heing lairly simple and similar the Ciuessing Gare. Msl 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 slarting playe the nel gae eds to repealedly call the play ni funt, recrd 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. If it is the computer's turn, you must call get_computer_ move (see next function) to find the move that tbe computer has chosen. NOTE: While writing this function. you might bypass this step and simply aliw, the human to enter moves for hcth players in orilerto simplify debugging .The function loops until all of the heaps are empy. When this occurs, the player who just moved (ie., the one who removed the last slone) is the winner and the numher of that player is returned to the calling Write a C program that implements the pame of Nim Yur prugram must follow the specifications given above to earn full credit. That is, your program must define and use at least the specificd functions. The ames of functions and variables do not have to be exactly the same. Function: get_computer move The output of the program must couform exactly to the following example run now that we have loops, there is anly one sample T;user input shown in hold) Nole there is blank line helore eich player's move, hefore the Objects Type Movenment Name y, and before the D you want to pl and there i sone space after the first e heaps of stones mputer's chosen heap heap a, heap b, heap_ heap seutence punctuation in a two sentence line of output. (E.g. in the error messnges. And as usual, there must be a newline aller the last line ol ulpul char passed hack (.Unipuler's chosen numher enl slones | int passed hack nurnher in remove Wclcome to the Ancient Game of Ni Player ia you (the han) Player 2 ia me the computer) Player 1 goes first this time! layer1 A: 5 Player 2 Heaps A: 3 B: 2 Enter the letter of the heap and number of atones to remove: c2 Player 2 Haapa A: 5 B: 4 Player 1 Heaps A: 2 : 2 Enter the letter of the heap and nunber of stones to reve: b1 COMPUTER noves al layer1 Heapa: Player 2 Heaps A: 2 CCMPUTER movea a Enter the letter of the heap and number of stones to remove B2 Player 1 Heap: Player 2 : 4 B: 2 Enter the letter of the heap and nunber of stones to renove: bi COMPUTER oves al Player2 Heaps: Player 1 Heapa A: 3 e: 2 COMPUTER movea ai Player 2 wins! Enter the letter of the heap and umber of stones to remove: D4 Illegal move! Try again layer1 Heaps Current Soore: Player 1Hunan): Player 2omputer): 1 B: 2 Do you want to play again? yin) Y Player 1 goes firat this timet Enter the letter of the heap and number of stones to remove c2 Illegal move! Try again Playar 1 Heaps A: 5 B: 4 layer1 Heaps D: 2 Enter the letter of the heap and number of stones to remove: cl
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