Chess game move validation

May 14, 2011 at 2:38am
Hi all,

I have a chess game that I wrote and was wondering how I would modify it to use the rules of chess. Meaning that right now all it does is check to see if the move on the board, the cell the piece is moving to is empty, and if the cell is occupied.... if it is an opponent piece. I want to again make the moves of each piece valid according to the rules of chess. Thank you.


#include <iostream> // I/O streams cout & cin
#include <cstdlib> // Common routines (e.g., system)
#include <cctype>

using namespace std; // Import standard namespace

const int SIZE = 8; // 8x8 chessboard
const char EMPTY = '_'; // character for empty cell

// display_game(board): Displays chess BOARD as 8x8 character grid
//
void display_game (char board[SIZE][SIZE])
{
cout << "Current board:" << endl;
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++){

// output cell character
cout << board[row][col];

}
cout << endl;
}
cout << endl;
}

// valid_mode(board, from_row, from_col, to_row, to_col): make sure the move from (from_row, from_col) to (to_row, to_col) makes sense for BOARD
//
bool valid_move (char board[SIZE][SIZE], int from_row, int from_col, int to_row, int to_col)
{


// if row or col is invalid
if((to_row < 0 || to_row > (SIZE -1)) || (to_col < 0 || to_col > (SIZE -1)))
{
return false;
}

// if new location isn't empty
if(board[to_row][to_col] != '_')
{
// if the color of the occupying piece matches the color of the piece
// that's trying to move
if(islower(board[from_row][from_col]) == islower(board[to_row][to_col]))
{
return false;
}
}

return (true);
}

// update_board(board, from_row, from_col, to_row, to_col): update BOARD to reflect move from (from_row, from_col) to (to_row, to_col)
//
void update_board (char board[SIZE][SIZE], int from_row, int from_col, int to_row, int to_col)
{

// change toLocation to match fromLocation
board[to_row][to_col] = board[from_row][from_col];


// make fromLocation empty
board[from_row][from_col] = '_';

}

// moderate_game(board): reads each move and updates the board accordingly
//
void moderate_game (char board[SIZE][SIZE])
{
// Read while more good input
while (cin.good()) {
char move_type;

// Read the move type letter (C or M)
cout << "Enter next move: ";
cin >> move_type;

// Process the move type
if (cin.eof()) {
// do nothing
}
else if (move_type == 'C') {
string rest;
getline(cin, rest);
cerr << "Ignoring comment (C" << rest << ")" << endl;
}
else if (move_type != 'M') {
cout << "Error: Invalid move type (" << move_type << ")" << endl;
}
else {


// Read and validate the starting and ending coordinates
int from_row, from_col, to_row, to_col;
cin >> from_row >> from_col >> to_row >> to_col;

from_row--; from_col--; to_row--; to_col--;

if (! valid_move(board, from_row, from_col, to_row, to_col)) {
cerr << "Invalid move" << endl;
}
else {

update_board(board, from_row, from_col, to_row, to_col);
display_game(board);
}
}
}
}

// main: Entry point for program
//
int main ()
{
char board[SIZE][SIZE] = // Check board with black on top (uppercase)
{
{'R', 'H', 'B', 'Q', 'K', 'B', 'H', 'R'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'_', '_', '_', '_', '_', '_', '_', '_'},
{'_', '_', '_', '_', '_', '_', '_', '_'},
{'_', '_', '_', '_', '_', '_', '_', '_'},
{'_', '_', '_', '_', '_', '_', '_', '_'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'h', 'b', 'q', 'k', 'b', 'h', 'r'},


};

display_game(board);
moderate_game(board);

// Exit with successful execution status
#if defined(WIN32)
system("PAUSE"); // For Dev++ under Windows
#endif
return (0);
}
May 14, 2011 at 2:44am
It's very simple logic tests.

Look at the position where the piece originally was.
Then look at the position that it's moving to.

Use that to determine if it's a legal move or not.
May 14, 2011 at 3:05am
It sounds simple but seems far more complex. For each piece (i.e... pawn, rook, knight, bishop, and etc..) they have limits for a move. Im confused on how I would write the code to apply those limits and really where to even start.
May 14, 2011 at 5:54am
It's as simple as it sounds. All you have to do is translate your thoughts into code.

Think about how you would figure out whether or not a move is legal. Outline the logic you use in your mind, then type that outline out in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if( piece_is_a_pawn )
{
  if( old_is_behind_new )
    return illegal;  // pawns can't move backwards

  if( old_col != dest_col )
  {
    // pawns can't move horizontally
    //  unless they're capturing a piece
    if( pawn_is_moving_diagonal )
    {
      // to capture a piece, an enemy piece must be there
      if( enemy_is_at_dest )
        return legal;
      else
         return illegal;
    }
  }

  //...
}


pawns are actually probably the hardest piece to write logic for, since they have so many wacky rules. I'd write the logic in the following order:

- King (easiest)
- Knight
- Rook
- Bishop
- Queen
- Pawn (hardest)
May 14, 2011 at 5:39pm
Okay, I was thinking of all this in terms of using the coordinates so that is probably why I have found this so difficult. I will try it.
May 15, 2011 at 4:46pm
Despite my best efforts... Im overloaded on this. Errors upon errors. This was an extra credit assignment but past due now. This is difficult for programming one class. thanks Disch for your help anyways.
May 15, 2011 at 4:59pm
I believe my old chess game has the rules you're looking for. No en passent or castling however: http://sourceforge.net/projects/ultifi-chess/files/SDL_Chess4.tar.gz/download
Topic archived. No new replies allowed.