Thread: Need Help!

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

    Question Need Help!

    Can someone please help me. My code won't decrement the way it should:


    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 nimSticks = pileSize;
     cout << "There are/is " << nimSticks << " 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; 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 )
    {
      int nimSticks = pileSize;
    	if(/*int*/ pileSize = false)
    	cout<<  "The game is over." <<endl<<endl;
    	return false;
    }
    Last edited by Middlechild47; 11-19-2006 at 05:19 PM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Where in the code is the problem? Can you pin point the place? It is hard to read through a large program with no guidence
    Double Helix STL

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    bool isGameOver( int pileSize )
    {
      int nimSticks = pileSize;
    	if(/*int*/ pileSize = false)
    	cout<<  "The game is over." <<endl<<endl;
    	return false;
    }
    is equivalent to:
    Code:
    bool isGameOver( int pileSize )
    {
       int nimSticks = pileSize;
       pileSize = false;
       if(false)
    	cout<<  "The game is over." <<endl<<endl;
       return false;
    }
    [/

    1. use == in if
    2. make return dependent on input value
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > if(/*int*/ pileSize = false)
    As already answered in your other thread
    http://cboard.cprogramming.com/showthread.php?t=85592

    Please don't create multiple threads to solve the same problem - closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed