I was asked to write a program for a two-person game of Nim where the user can decide how many objects are each heap. Each player, on their turn, may choose one heap and take one to all the objects in that particular heap. The player who draws the last object wins.

The code does compile, it just doesn't go the way I want it to.

Known problems:

- The while loop will not run because the sum is seen as 0 in the int main (I think)
- Sum not being counted properly.
- Not subtracting remove from sum
- Line 137: If an error occurs, the player # is messed up (does not go back to the current player)
- Line 146: Number of objects in each heap is not displayed/counted.

After the FOR loop on Line 92, the sum amount is correct. But it will not be carried over to the int main(). Is there a way to do this without declaring a global variable?

#include <iostream> #include <iomanip> using namespace std; // Global constant const int NUM_HEAPS = 3; // Number of heaps // FUNCTION PROTOTYPE FOR setup // Number of elements in each heap void setup (int heap[], const int NUM_HEAPS); // FUNCTION PROTOTYPE FOR heap_drawing // Draws heaps as * void heap_drawing (int heap[], const int NUM_HEAPS); // FUNCTION PROTOTYPE FOR turn // Prompts and reads a player's next move void turn (int heap[], int player, const int NUM_HEAPS); // FUNCTION PROTOTYPE FOR removeAmount // Removes the specified number of elements from the specified heap void removeAmount (int heap[], const int NUM_HEAPS, int heapNum, int remove, int player); // FUNCTION PROTOTYPE FOR win // Prints a message congratulating the winning player void win (int player); // FUNCTION PROTOTYPE FOR player_turn // Changes the current player void player_turn (int & player); int main() { int heap[NUM_HEAPS]; // heap[i] = Number of elements in heap i int player(1); // Player 1 or 2 int sum(0); // sum of objects in heap // Read number of elements in each heap setup (heap, NUM_HEAPS); // Draw heaps as rows of *'s heap_drawing (heap, NUM_HEAPS); // WHILE some heap is not empty DO while (sum > 0) { // Read heap to modify and number of elements to remove from heap turn (heap, player, NUM_HEAPS); // Remove elements from heap removeAmount (heap, NUM_HEAPS, heapNum, remove, player); // IF all the heaps are empty, THEN if (sum == 0) { // Player has won. // Print win message. win (player); } // ELSE else { // Play continus // Redraw heaps. heap_drawing (heap, NUM_HEAPS); // Change to other player. player_turn (player); // END IF } // END WHILE } } // FUNCTION DEFINITION FOR setup // Number of elements in each heap void setup (int heap[], const int NUM_HEAPS) { int objects(0); int sum(0); for (int i = 0; i <= NUM_HEAPS-1; i++) { cout << "How many objects are in Heap " << i << ": "; cin >> objects; if (objects < 0) { cout << "Sorry, the number of objects must be positive." << endl; cout << "How many objects are in Heap " << i << ": "; cin >> objects; } heap[i] = objects; sum += heap[i]; // cout << sum << "!!" <<endl; } } // FUNCTION DEFINITION FOR heap_drawing // Draws heaps as * void heap_drawing (int heap[], const int NUM_HEAPS) { int objectsinheap(0); cout << endl; for (int i = 0; i <= NUM_HEAPS-1; i++) { cout << "Heap " << i << ": "; objectsinheap = heap[i]; for (int star = 0; star < objectsinheap; star++) { cout << "*"; } cout << endl; } cout << endl; } // FUNCTION DEFINITION FOR turn // Prompts and reads a player's next move void turn (int heap[], int player, const int NUM_HEAPS) { int heapNum(0); int objectsinheap; if (player == 1) { cout << "Player(1): Which heap would you like to play? "; } else { cout << "Player(2): Which heap would you like to play? "; } cin >> heapNum; while (heapNum > NUM_HEAPS-1 || heapNum < 0) { cout << "Invalid heap number. Please try again." << endl; cout << "Player(" << player+2 << "): Which heap would you like to play? " << endl; cin >> heapNum; } } // FUNCTION DEFINITION FOR removeAmount // Removes the specified number of elements from the specified heap void removeAmount (int heap[], const int NUM_HEAPS, int heapNum, int remove, int player) { int objectsinheap; // fix objectsinheap # cout << "Enter number of objects to remove (" << objectsinheap << " or less) from heap " << heapNum << ": "; cin >> remove; if (objectsinheap <= 0) { cout << "Heap " << heapNum << " has zero objects. Please select a different heap." << endl; cout << "Player(" << player << "): Which heap would you like to play? " << endl; cin >> heapNum; } heap[heapNum] = heap[heapNum] - remove; } // FUNCTION DEFINITION FOR win // Prints a message congratulating the winning player void win (int player) { if (player == 1) { cout << "Congratulations! Player 1 wins."; } else { cout << "Congratulations! Player 2 wins."; } } // FUNCTION DEFINITION FOR player_turn // Changes the current player void player_turn (int & player) { if (player == 1) { player = player - 1; } else { player = player + 1; } }