<<< 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?