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.
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;
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.
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.