1 Replies - 401 Views - Last Post: 27 November 2012 - 10:57 PM Rate Topic: -----

#1 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

game of Nim

Posted 27 November 2012 - 09:01 PM

Hello again. I'm not looking for a straight up answer for this, but I believe I've just been looking at this for too long. I'm aware there are multiple errors to this code, but from expert eyes, do you see any (more) blatant error that I am missing? I apologize in advance: this is probably the messiest code I've ever written. Which actually isn't saying much because I haven't written much code, but I would still like to apologize.

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;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: game of Nim

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4221
  • View blog
  • Posts: 13,231
  • Joined: 25-December 09

Re: game of Nim

Posted 27 November 2012 - 10:57 PM

Quote

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?

Yes, you could return this value to the calling function by changing the function return type from void to int and then return sum.

You also have some other warnings you should take care of:

Quote

main.cpp||In function ‘int main()’:|
main.cpp|51|error: ‘heapNum’ was not declared in this scope|
main.cpp||In function ‘void setup(int*, int)’:|
main.cpp|75|warning: declaration of ‘NUM_HEAPS’ shadows a global declaration [-Wshadow]|
main.cpp|7|warning: shadowed declaration is here [-Wshadow]|
main.cpp||In function ‘void heap_drawing(int*, int)’:|
main.cpp|99|warning: declaration of ‘NUM_HEAPS’ shadows a global declaration [-Wshadow]|
main.cpp|7|warning: shadowed declaration is here [-Wshadow]|
main.cpp||In function ‘void turn(int*, int, int)’:|
main.cpp|119|warning: declaration of ‘NUM_HEAPS’ shadows a global declaration [-Wshadow]|
main.cpp|7|warning: shadowed declaration is here [-Wshadow]|
main.cpp|122|warning: unused variable ‘objectsinheap’ [-Wunused-variable]|
main.cpp|119|warning: unused parameter ‘heap’ [-Wunused-parameter]|
main.cpp||In function ‘void removeAmount(int*, int, int, int, int)’:|
main.cpp|144|warning: declaration of ‘NUM_HEAPS’ shadows a global declaration [-Wshadow]|
main.cpp|7|warning: shadowed declaration is here [-Wshadow]|
main.cpp|144|warning: unused parameter ‘NUM_HEAPS’ [-Wunused-parameter]|
||=== Build finished: 1 errors, 11 warnings ===|


Your NUM_HEAPS is a const global variable, you don't need to send this value to your function, just use the global.

Quote

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

Actually if you look at the above messages you should see one error, so this code doesn't actually compile.

Also your removeAmount() function doesn't seem to be correct. You are passing parameters to this function that are probably not needed. The variables should be local to the function. For example, heapNum you are passing by value, which means that any changes made to this variable in the function will not be reflected in the calling function. Which may be okay except you use this variable to get input from the user before you use the variable for anything else. So there is really no reason to pass this variable into the function, just make it local (declare the variable in the function).


You may want to study the function tutorials contained in my signature. They may help you better understand how to create and use functions.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1