Write a program for playing the game of Nim. The game of Nim starts with three h
ID: 3626579 • Letter: W
Question
Write a program for playing the game of Nim. The game of Nim starts with three
heaps and a fixed 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 the 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.
Here is my code so far. They want me to use functions for most of the program. all thats left to do is minor errors that i cant find. if somebody could help that would be amazing
#include
#include
#include
#include
#include
using namespace std;
// Global constant
// Number of heaps
// FUNCTION PROTOTYPES GO HERE:
//heap_setup prototype
void Heap_setup(int heapcount[], int numberofheaps);
//Player turn indicator prototype
void Player_turn(int player);
//read_heaps prototype
void read_heaps(const int heapsread[], int numberofheaps);
//drawing heaps as * prototype
void heap_drawing(int heapsread[], int numberofheaps);
//turn prototype
void turn(int heapsread[], int player, int numberofheaps);
//win prototype
void win(int Player);
int main()
{
int x = 0; // the number of heaps
int Players = 1; //the 1 is important for the player turn indicator
cout << "how many heaps do you want to use: ";
cin >> x;
int heaps[x];
for (int i = 0; i < x; i++)
{
heaps[i] = 0;
}
Heap_setup(heaps[], x);
// Player 1 or 2
Player_turn(int Players);
// Read number of elements in each heap
read_heaps(const int heaps[], int x);
//sum objects in heap
int sum = 0;
for (int i = 0; i < x ; i++)
{
sum += heaps[i];
}
// Draw heaps as rows of *'s
heap_drawing(int heaps[], int x);
// WHILE some heap is not empty DO
While (sum > 0)
{
// Read heap to modify and number of elements to remove from heap
turn(int heaps[], int Players, int x); // Remove elements from heap
// IF all the heaps are empty, THEN
for (int i = 0; i < x ; i++)
{
sum = 0;
sum += heaps[i];
}
read_heaps(const int heaps[], int x);
heap_drawing(int heaps[], int x);
Player_turn(int Players);
}
win(int Player);
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
//Heap setup sets up the number of objectsb in the heaps
void Heap_setup(int heapcount[], int numberofheaps)
{
int objects = 0
cout << "How many objects do you want in each heap? (for this game of nim, all heaps begin with the same ammount of objects): ";
cin >> objects;
for(int i = 0; i < numberofheaps; i++)
{
heapcount[i] = objects;
}
}
//Player turn indicator
void Player_turn(int & player)
{
if(player == 1)
{
player=player-1;
}
else
{
player=player+1;
}
if(player == 1)
{
cout << "Player 2's turn" << endl;
}
else
{
cout << "Player 1's turn" << endl;
}
}
//read_heaps prototype
void read_heaps(const int heapsread[], int numberofheaps)
{
for (int i = 0; i < numberofheaps; i++)
{
cout << "Heap " << i << " has " << heapsread[i] << " Objects." << endl;
}
}
//drawing heaps as *
void heap_drawing(int heapsread[], int numberofheaps)
{
int objectsinheap = 0;
for (int i = 0; i < numberofheaps; i++)
{
cout << "heap " << i << ": ";
objectsinheap = heapsread[i];
for (int star = 0; star < objectsinheap; star++)
{
cout <<"*";
}
cout<}
}
//the player's turn
void turn(int heapsread[], int player, int numberofheaps)
{
int heap_number = 0;
int remove = 0;
if(player == 1)
{
cout << "Player 2, what heap would you like to take objects from: ";
}
else
{
cout << "Player 1 what heap would you like to take objects from: ";
}
cin >> heap_number;
while(heap_number > numberofheaps || heap_number < 0)
{
cout << "must enter a valid heap: ";
cin >> heap_number;
}
cout << "how many heaps would you like to remove?";
cin >> remove;
while(remove > heapsread[heap_number])
{
cout << "cannot remove more objects than are already there, enter valid ammount :";
cin >> remove;
}
heapsread[heap_number]= heapsread[heap_number]-remove;
}
void win(int Player)
{
if(player == 1)
{
cout << "Player 1 wins" << endl;
}
else
{
cout << "Player 2 wins" << endl;
}