Thread: Same problem, different code

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Same problem, different code

    <<< split from 5 year old thread -> C++ Checkers Game >>>

    I'm having the same problem, it keeps telling me the move is invalid even though it should be valid. I used a different approach at the game, I have different classes for piece and players. Here is part of the Human class, the function isValidMove() checks to see if the move the player choose is valid.

    Code:
    void Human::getPiece()
    {
        validPiece = false;
    	
    	do
    	{
    	cout << "Which piece would you like to move, row then column" << endl;
        cin >> x >> y;
    		/*********************************************************************/
    		/**/if (board[x][y]->getColor() == 'R')
    		/**/{
    		/**/	if (x == 0)
    		/**/	{ 
    		/**/		if (board[x--][y++] -> getColor() == '-')
    		/**/			validPiece = true;
    		/**/	}
    		/**/	else if (x == 7)
    		/**/	{
    		/**/		if (board[x--][y--] -> getColor() == '-')
    		/**/			validPiece = true;
    		/**/	}
    		/**/	else if (x > 0 && x < 7)
    		/**/	{
    		/**/		if (board[x--][y--] -> getColor() == '-' || board[x--][y++] -> getColor() == '-')
    		/**/			validPiece = true;
    		/**/	}
    		/**/}
    		/****************************************************************************************************/
    		
    		
    	if (!validPiece)
    		cout << "Invalid Piece..." << endl;
    	}
    	while (!validPiece);
    	
    	cout << "Valid Piece Selected" << endl;
    	
    }
    
    bool Human::isValidMove()
    {
        isValid = false;
    	isJump = false;
    	
    	do
    	{
    	cout << "Where do you want to move to, row then column" << endl;
        cin >> x2 >> y2;
        
            if (x2 == x-- && y2 == y--)// Checks to see that the piece is moving forward one spot and to the left
            {
                if (board[x2][y2]->getColor() == '-') // Checks to see that the location is empty
    				isValid = true;
    			cout << "Moved diagnolly left" << endl;
            }
    		else if (x2 = x-- && y2 == y++)// Checks to see that the piece is moving forward one spot and to the right
    		{
    			if (board[x2][y2]->getColor() == '-') // Checks to see that the location is empty
    				isValid = true;
    			cout << "Moved diagnolly right" << endl;
    		}
    	
    			
    		/***Checking a Jump Move*************************************************************/
    		if (x2 == x-2 && y2 == y+2)
    		{
    			if (board[x--][y++]->getColor() == 'W' || board[x--][y++]->getColor() == 'w')
    			{
    				isJump == true;
    				isValid == true;
    				count++;
    				cout << "Jumped diagnolly right" << endl;;
    			}
    		}
    		else if (x2 == x-2 && y2 == y-2)
    		{
    			if (board[x--][y--]->getColor() == 'W' || board[x--][y--]->getColor() == 'w')
    			{
    				isJump == true;
    				isValid == true;
    				count++;
    				cout << "Jumped diagnolly left" << endl;
    			}
    		}
    		/************************************************************************************/
    		if (!isValid)
    			cout << "Invalid move..." << endl;
    	}
    	while (isValid != true);
    	
    	cout << "Valid Move Selected" << endl;
    
    	/**KING***********************************************************************************/
    	if (x2 == 0 && isValid)
    		board[x][y] -> setKing();
    	
    	/******Checking a king move in opposite direction*****************************************/
    	if (board[x][y] -> isKing()) // lowercase for king
    	{
    		if (x2 == x++ && (y2 == y++ || y2 == y--))
    			isValid = true;
    		
    		/***Checking a jump move for a king in opposite directoin*****************************/
    		if (x2 == x+2 && y2 == y+2)
    		{
    			if (board[x++][y++]->getColor() == 'W' || board[x++][y++]->getColor() == 'w')
    			{
    				isJump == true;
    				isValid == true;
    				count++;
    			}
    		}
    		else if (x2 == x+2 && y2 == y-2)
    		{
    			if (board[x++][y--]->getColor() == 'W' || board[x++][y--]->getColor() == 'w')
    			{
    				isJump == true;
    				isValid == true;
    				count++;
    			}
    		}
    		/**************************************************************************************/
    		
    	}
    	
    	return isValid;
    }
    Any help?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Check this line of code. You are using the assignment operator (=), I suspect you should be using the comparison operator (==).
    Code:
    else if (x2 = x-- && y2 == y++)// Checks to see that the piece is moving forward one spot and to the right
    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (board[x++][y--]->getColor() == 'W' || board[x++][y--]->getColor() == 'w')
    Overuse of increment operators.
    If it isn't 'W', then x and y get changed TWICE.

    Write something like
    Code:
    			if (board[x][y]->getColor() == 'W' || board[x][y]->getColor() == 'w')
    			{
    				isJump == true;
    				isValid == true;
    				count++;
    				x++;
    				y--;
    			}
    You do this a lot in your code - simplify it!
    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

Similar Threads

  1. problem with my C code
    By datainjector in forum C Programming
    Replies: 2
    Last Post: 10-04-2009, 07:32 PM
  2. Problem with my code :( please help
    By yrostran in forum C Programming
    Replies: 4
    Last Post: 11-29-2006, 04:22 PM
  3. code problem
    By NightWalker in forum C Programming
    Replies: 8
    Last Post: 11-16-2003, 05:58 AM
  4. problem with my code
    By stilllearning in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 03:02 PM
  5. Help!!problem with my code
    By Daniel Decosta in forum C Programming
    Replies: 8
    Last Post: 09-28-2002, 09:44 AM