Thread: C++ NIM with functions

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    C++ NIM with functions

    I have a C++ program that I can not get to run. Can you
    please help me find the problem. The code is listed below.


    *************************************/
    Code:
    #include <iostream> 
    
    
    using namespace std; 
    
      
    
    // Let's define constants to identify the sides
    const bool HUMAN_PLAYER = true;
    const bool COMPUTER_PLAYER = false; 
    
      
    
    // Function Prototypes - Messages to the player
    void welcomeMessage();
    void congratulateWinner( bool theWinner );
    void gameStatus( int pileSize ); 
    
      
    
    // Function Prototypes - User Interface
    // Decide who gets to play first
    bool pickFirstPlayer(); 
    
      
    
    // Function Prototypes - Moves
    int getHumanPlayerMove( int pileSize );
    int getComputerPlayerMove( int pileSize ); 
    
      
    
    // Function Prototypes - Predicates
    bool isGameOver( int pileSize ); 
    
      
    
    // The main NIM Program
    int main()
    {
      // Start by saying hello to the player
      welcomeMessage(); 
    
      
    
      // Now, let's ask the human player who goes first
      bool currentPlayer = pickFirstPlayer();
     
      // Now that we know who plays first, we can start
      // the game by initializing the heap of sticks
      int nimSticks = 22; 
    
      
    
      // CORE GAME LOOP: as long as there are sticks left,
      // we'll need to get moves from the opponents
      while( !isGameOver( nimSticks) )
      {
        // Show the status of the game
        gameStatus( nimSticks ); 
    
      
    
        // Let's get a move from the current player
        int currentPlayerMove;
        if( currentPlayer == HUMAN_PLAYER )
          currentPlayerMove = getHumanPlayerMove( nimSticks );
        else
          currentPlayerMove = getComputerPlayerMove( nimSticks ); 
    
      
    
        // Now that we know how many sticks the current
        // player wants to take, we remove them from the
        // pile
        nimSticks -= currentPlayerMove; 
    
      
    
        // Do we have a winner?
        if( isGameOver( nimSticks ) )
          congratulateWinner( currentPlayer );
     
        // If there are sticks left in the pile, it is
        // now the other player's turn
        else
          currentPlayer = !currentPlayer;
      }
     
      // And we're done!
      return 0;
    } 
    
      
    
    // welcomeMessage() - Say Hi to the player
    void welcomeMessage()
    {
      cout << "Welcome to the Game of Single-Pile Nim!" << endl;
      cout << "---------------------------------------" << endl << endl;
      return;
    } 
    
      
    
    // congratulateWinner() - Tell the player who won
    void congratulateWinner( bool theWinner )
    {
      // If so, who has just taken the last stick?
      if( HUMAN_PLAYER == theWinner )
      {
        cout << "You win. Congratulations!" << endl;
      }
      else
      {
        cout << "I win! Better luck next time..." << endl;
      }
      return;
    } 
    
      
    
    // gameStatus() - Tell the player how many sticks
    // are left in the pile before a turn
    void gameStatus( int pileSize )
    {
     int stickStatus = pileSize;
     cout << "There are/is " <<stickStatus << " stick(s) left." <<endl<<endl;
    	return;
    } 
    
      
    
    // pickFirstPlayer() - Ask the user who goes first
    bool pickFirstPlayer()
    {
      int count = 2;
    	int compTurn;
    	char start;
    	
    	cout << "IT'S NIM TIME!!!!!!" <<endl<<endl;
    
    	while(count >= 0)
    	{
    		cout << "Who starts? [H]uman or [C]omputer?" <<endl;
    		cin >> start;
    		switch(start)
    		{
    		case 'C':
    		case 'c':cout << "Computer start." <<endl; count = -1; compTurn = true; break;
    		case 'H':
    		case 'h':cout << "Human start." <<endl; count = -1; true;compTurn = true; break;
    		default:cout << count << " try(s)left" <<endl;
    		}
    		if(count == 0)
    			cout << "NO GO!!!!!              Sorry." <<endl<<endl;
    	--count;
    	}
    	//return(compTurn);
    	return true;
    } 
    
    
    // getHumanPlayerMove() - Ask the player for a number
    // of sticks to take out of the pile
    int getHumanPlayerMove( int pileSize )
    {
      int pickstick;
    	int no = 3;
    int nimSticks = pileSize;
    	while(no != 0)
    	{
    		cout << "Take how many sticks?: ";
    		cin >> pickstick;
    		cout << endl;
    		if(1 <= pickstick && pickstick <= 4)
    		{
    			nimSticks -= pickstick;
    			cout<< "Number of sticks left: " << nimSticks << endl << endl;
    			/*if(winner() == 1)
    			{
    				cout << "The hu-man wins!" <<endl;
    				exit(0);
    			}*/
    			no = 0;
    		}
    		else
    		{
    			no--;
    			cout << "Error. " << no << " trys left. " <<endl;
    		}
    	}
    	return 0;
    } 
    
    
    // getComputerPlayerMove() - Let the machine play
    int getComputerPlayerMove( int pileSize )
    {  
    	int nimSticks = pileSize;
    	int pick = nimSticks - nimSticks % (1 + 4);
    	int just = nimSticks - pick;
    	if (just == 0)
    	{
    		just = 1 + rand() % (4 - 1 + 1);
    		pick -= just;
    	}
    	nimSticks -= just;
    	cout << "Computer takes " << just <<" stick(s)." <<endl;
    	cout << "Their are " << pick << " left." <<endl;
    	/*if(winner() == 1)
    	{
    		cout << "The COMPUTER wins!" <<endl;
    		exit(0);
    	}*/
    	return 0;
    } 
    
    
    // isGameOver() - Have we reached endgame conditions?
    // if there are no sticks left, yes
    bool isGameOver( int pileSize )
    {
      if(int nimSticks = true)
    	cout<<  "The game is over." <<endl<<endl;
    	return true;

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) Post your errors.
    2) I went straight to the bottom, but I see that this has no closing braces:
    Code:
    bool isGameOver( int pileSize )
    {
      if(int nimSticks = true)
    	cout<<  "The game is over." <<endl<<endl;
      return true;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >case 'C':
    >case 'c':cout << "Computer start." <<endl; count = -1; compTurn = true; break;
    >case 'H':
    >case 'h':cout << "Human start." <<endl; count = -1; true;compTurn = true; break;
    >default:cout << count << " try(s)left" <<endl;

    It really does make a switch statement easer to read when it is like this:

    Code:
    switch ( age )
    {
    case 10:
       cout << "\nYou are age ten!" << endl;
       break;  // we know the break is here without reading the entire string
    
    default:
       cout << "\ncerr << "Wrong age!" << endl;
       break;
    }
    I echo manutd, we need the compiler errors. Also, it may be better to organise a class
    for your game, and place that in a seperate header file, it will reduce the code a little and
    saves stuffing a large program into the one file
    Double Helix STL

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Oh, and again the error is indeed a missing brace at the end of your isGameOver() function
    Double Helix STL

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Well, there we go. Easiest fix in the world, just do what I did in red in my post.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Just thought Id add that your game does nothing. if you enter c or h at the start of the program all it does is say good-bye. This is of course pending you have completed the [rogram, and if this is the case, i advise you to test it ( after you have fixed the error ) and you will see what I mean. looks good though.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    Code:
    bool isGameOver( int pileSize )
    {
      if(int nimSticks = false)
    	cout<<  "The game is over." <<endl<<endl;
      return false;
    }
    I changed the trues to falses and it works, but now I have a different problem. The sticks wont decrement. Here are my functions for the player and the computer:
    Code:
    int getHumanPlayerMove( int pileSize )
    {
      int pickstick;
    	int no = 3;
    int nimSticks = pileSize;
    	while(no != 0)
    	{
    		cout << "Take how many sticks?: ";
    		cin >> pickstick;
    		cout << endl;
    		if(1 <= pickstick && pickstick <= 4)
    		{
    			nimSticks -= pickstick;
    			cout<< "Number of sticks left: " << nimSticks << endl << endl;
    			/*if(winner() == 1)
    			{
    				cout << "The hu-man wins!" <<endl;
    				exit(0);
    			}*/
    			no = 0;
    		}
    		else
    		{
    			no--;
    			cout << "Error. " << no << " trys left. " <<endl;
    		}
    	}
    	return 0;
    } 
    
    
    
    int getComputerPlayerMove( int pileSize )
    {  
    	int nimSticks = pileSize;
    	int pick = nimSticks - nimSticks % (1 + 4);
    	int just = nimSticks - pick;
    	if (just == 0)
    	{
    		just = 1 + rand() % (4 - 1 + 1);
    		pick -= just;
    	}
    	nimSticks -= just;
    	cout << "Computer takes " << just <<" stick(s)." <<endl;
    	cout << "Their are " << pick << " left." <<endl;
    	/*if(winner() == 1)
    	{
    		cout << "The COMPUTER wins!" <<endl;
    		exit(0);
    	}*/
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if(int nimSticks = false)
    That code is still wrong. You are declaring a new int named nimSticks and assigning it the value false. What is that function supposed to be doing?

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    bool isGameOver( int pileSize )
    {
      if(int nimSticks = false)
    	cout<<  "The game is over." <<endl<<endl;
      return false;
    }
    is equivalent to
    Code:
    bool isGameOver( int pileSize )
    {  return false;}
    Is it what you plan to do?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    // Now that we know how many sticks the current
    // player wants to take, we remove them from the
    // pile
    nimSticks -= currentPlayerMove;
    your functions always return 0. So you always decrement the numStick value with 0 (that's why it is not changed. You should return the actual value of taken picks.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM