Write a program for playing the game of Nim. The game of Nim starts with three h
ID: 3657707 • Letter: W
Question
Write a program for playing the game of Nim. The game of Nim starts with three heaps and a xed number of objects in each heap. Two players take turns removing objects from the heaps. On a players turn, she chooses a heap and removes one or more objects from that heap. She can remove all objects from the heap. In our version of the game of Nim, the player who takes the last object from the heaps (so that all heaps are empty) wins. (See www.wikipedia.org for further discussion of the game). The algorithm for the game of Nim is provided below. Most steps in the algorithm should be implemented as function calls. You need to implement the algorithm and the function calls as described below. You need to implement each function. You may and should also implement additional functions to help you in writing. For instance, to implementthe function which draws the heaps you should implement a function which draws a rowrepresenting a single heap and call that function three times, once for each heap.Implement the following algorithm for the game of Nim. Each line of the algorithm shouldbe implemented as a call to a single function. Implement the algorithm one function at amtime, writing the functions, checking that it compiles and runs properly and then implementthe next function.
i. Prompt and read number of elements in each heap;
ii. Draw the heaps;
iii. while (some heap is NOT empty) do
iv. Prompt and read the next player move (Prompt and read the heap to modify
and the number of elements to remove from the heap);
v. Remove the specied number of elements from the specied heap;
vi. if (all heaps are empty) then
vii. Print a message congratulating the winning player.
viii. else
ix. Redraw the heaps;
x. Change to the other player;
xi. end
xii. end
1. All functions should be written AFTER the main procedure.
2. All function prototypes should be written for each function and placed BEFORE the
main procedure.
3. Each function should have a comment explaining what it does.
4. Each function parameter should have a comment explaining the parameter.
5. As in nim3_template.cpp, your program should have a global constant denition:
const int NUM_HEAPS = 3;
Your program SHOULD NOT use any other global variables.
6. As in nim3heaps_template.cpp, the number of elements in the heaps should be stored
in an array:
int heap[NUM_HEAPS];
7. As in nim3heaps_template.cpp, the current player (1 or 2) should be stored in an
integer:
int player(1);
8. The function descriptions below do not specify the exact parameters for the functions,
nor whether they are passed by value or by reference nor whether array parameters
should be const. You will need to decide this for yourself.
Scalar (single value) parameters which are modied by the function need to be passed
by reference. All others should be passed by value.
Array parameters for arrays which are modied should NOT be const. All other array
parameters should be const.
9. Write a function which prompts and reads in the number of elements in each heap.
Call this function to implement step (i) of the algorithm. For each heap, the function
should check that the number entered is positive. If it is not positive, the program
should print an error message and prompt and read again the number of elements until
it receives a positive number.
The main program should call a single function f() to read all three heaps. (Choose
a better function name than f()). However, to avoid duplication of code, write a
function to read a single heap and call it three times from function f(), once for each
heap. The function modies the values in the array heap[].
10. Write a function to draw a representation of the heaps. Call this function to implement
steps (ii) and (ix) of the algorithm. Each heap is represented on a separate row. In
the row for heap i, there is a label "Heap i: " followed by n *'s where n is the number
of elements in heap i. For instance if heap 0 has 3 elements heap 1 has zero elements
and heap 2 has 8 elements, the function should output:
Heap 0: ***
Heap 1:
Heap 2: ********
The function to draw the heaps should output an empty line before and after the rst
and last row, respectively.
The main program should call a single function g() to draw all three heaps. (Choose
a better function name than g()). However, to avoid duplication of code, write a
function to draw a single row corresponding to a single heap and call it three times
from function g(), once for each heap.
11. Write a function which returns true if all the heaps have zero elements and returns
false otherwise. Call this function to implement steps (iii) and (vi) of the algorithm.
Note that the same function can be used both for step (iii) and step (vi).
12. Write a function which prompts and reads a players next move. Call this function to
implement step (iv) of the algorithm.
The function rst prompts for the heap from which to remove elements.
If the heap number is not 0, 1, or 2, print an error message that the heap number
is illegal.
If the heap has no elements, print an error message that the heap has no elements.
Continue prompting and reading until the player enters a valid heap number which
represents a heap with one or more elements.
The function next prompts for the number of elements to remove from the specied
heap.
If the number is not positive, print an error message that the number must be
positive.
If the number is greater than the number of elements in the specied heap, print
an error message that there are only x elements in the heap.
Continue prompting and reading until the player enters a valid number of elements
which is less than or equal to the number of elements in the heap.
13. Write a function which removes the specied number of elements from the specied
heap. Call this function to implement step (v) of the algorithm.
The function modies the values in array heap[].
14. Write a function to print a message congratulating the winning player. The message
should identify who won (player 1 or player 2). Call this function to implement step
(vii) of the algorithm.
15. Write a function to change the current player. Call this function to implement step (x)
of the algorithm. If the value of variable player is 1, then the function should change
the value of player to 2. If the value player is 2, then the function should change the
value of player to 1.
The function modies the value of player.
16. Be sure to modify the header comments "File", "Created by", "Creation Date", and
"Synopsis" at the top of the le. Each synopsis should contain a brief description of
what the program does.
17. Be sure that there is a comment documenting each variable.
18. Be sure that your if statements, for and while loops and blocks are properly indented.
19. Check your output against the output from the solution executable provided.
Write your program so that all functions work for any number of heaps, not just three.
By simply changing the value 3 in the global declaration
const int NUM_HEAPS = 3;
your program should be able to work for any number of heaps. The grader will check values
other than 3 for NUM_HEAPS.
Explanation / Answer
#include #include #include #include using namespace std; int compChoose(int counters); int userChoose(int counters); bool whoseTurn(); // Generate random numbers that is between 1 and 3 int random() { int random; random=(rand()%(3-1+1)+1); return random; } int main() { //total counters // const int NUM_COUNTERS = 13; //declare variables string player; int counters; srand((unsigned)time(NULL)); coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.