Like Tree2Likes
  • 1 Post By jimblumberg
  • 1 Post By Salem

Same problem, different code

This is a discussion on Same problem, different code within the C++ Programming forums, part of the General Programming Boards category; <<< split from 5 year old thread -> http://cboard.cprogramming.com/cplus...kers-game.html >>> I'm having the same problem, it keeps telling me the ...

  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
    2,690
    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
    Salem likes this.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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!
    jimblumberg likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03:22 PM
  3. code problem
    By NightWalker in forum C Programming
    Replies: 8
    Last Post: 11-16-2003, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21