Beispiel #1
0
//------------------------------------------------------------------------------
bool RookPiece::areSquaresLegal(int src_row, int src_col,
                                int dest_row, int dest_col,
                                Piece* game_area[8][8], Chess& game_control)
{
  if(src_row == dest_row)
  {
    // be sure that all squares are empty
    int col_offset = (dest_col - src_col > 0) ? 1 : -1;

    for(int check_col = src_col + col_offset; check_col != dest_col;
        check_col += col_offset)
    {
      // check if the coordinates are valid
      if((check_col < 0 || check_col > 7))
      {
        return false;
      }

      if(game_area[src_row][check_col] != NULL)
      {
        game_control.setErrorCode(ERROR_PIECE_ON_WAY);
        return false;
      }
    }

    return true;
  }
  else if(dest_col == src_col)
  {
    int row_offset = (dest_row - src_row > 0) ? 1 : -1;

    for(int check_row = src_row + row_offset; check_row != dest_row;
        check_row += row_offset)
    {
      // check if the coordinates are valid
      if((check_row < 0 || check_row > 7))
      {
        return false;
      }

      if(game_area[check_row][src_col] != NULL)
      {
        game_control.setErrorCode(ERROR_PIECE_ON_WAY);
        return false;
      }
    }

    return true;
  }

  return false;
}
Beispiel #2
0
//------------------------------------------------------------------------------
bool Piece::isMoveValid(int src_row, int src_col,
                        int dest_row, int dest_col,
                        Piece* game_area[8][8], Chess& game_control)
{
  Piece* dest_area = game_area[dest_row][dest_col];

  if((dest_area == NULL) || (piece_color_ != dest_area->getColor()))
  {
    bool ret = areSquaresLegal(src_row, src_col, dest_row, dest_col, game_area,
                               game_control);

    if(ret == false && game_control.getErrorCode() != ERROR_PIECE_ON_WAY)
    {
      game_control.setErrorCode(ERROR_TARGET_NOT_REACHABLE);
    }

    game_control.setPieceCaptured(false);

    if((ret == true) && (dest_area != NULL) &&
       (piece_color_ != dest_area->getColor()))
    {
      game_control.setPieceCaptured(true);
    }

    return ret;
  }
  else
  {
    bool ret = areSquaresLegal(src_row, src_col, dest_row,
                               dest_col, game_area,
                               game_control);

    if(ret == false && game_control.getErrorCode() != ERROR_PIECE_ON_WAY)
    {
      game_control.setErrorCode(ERROR_TARGET_NOT_REACHABLE);
    }
    else
    {
      game_control.setErrorCode(ERROR_OWN_PIECE_ON_TARGET);
    }
  }

  // check if the player goes to a square, where his own piece is on
  // but also another piece is on the way
  if((dest_area != NULL) && (piece_color_ == dest_area->getColor()))
  {
    areSquaresLegal(src_row, src_col, dest_row, dest_col, game_area,
                    game_control);
  }

  return false;
}