/************************************************************************/ /* Program: prog6.c */ /* Author: name removed to protect the innocent :-) */ /* */ /* CMP 12A, Winter 2000 */ /* Programming Assignment #5 */ /* March 3, 2000 */ /* */ /* This program is a rudimentary part of chess. Given a chess piece and */ /* its position on the board, the program will print out all the */ /* possible moves. */ /* Input: */ /* Type of piece (rook, knight, bishop, queen, king) and position on */ /* the board (0..63) */ /* Output: */ /* Drawing of board showing the position of the chess piece and all its */ /* possible moves marked with X */ /* Bugs and Limitations: */ /* Program assumes valid piece and position */ /************************************************************************/ #include /* printf, scanf definitions */ #define NUM_COLS 8 /* Number of columns in board */ #define NUM_ROWS 8 /* Number of rows in board */ /* Function Prototypes */ void convert_coord (int pos, int *col, int *row); void clear_board (char board[NUM_COLS][NUM_ROWS]); void play (int col, int row, char piece, char board[NUM_COLS][NUM_ROWS]); void play_rook (int col, int row, char board[NUM_COLS][NUM_ROWS]); void play_knight (int col, int row, char board[NUM_COLS][NUM_ROWS]); void play_bishop (int col, int row, char board[NUM_COLS][NUM_ROWS]); void play_queen (int col, int row, char board[NUM_COLS][NUM_ROWS]); void play_king (int col, int row, char board[NUM_COLS][NUM_ROWS]); void print_board (char board[NUM_COLS][NUM_ROWS]); /************************************************************************/ /* Function: main */ /* */ /* Gets program input, calls convert_coord, clear_board, play, and */ /* print_board */ /* */ /* Input: Chess piece, R (rook), N (knight), B (bishop), Q (queen), or */ /* K (king) and board position (value of 0..63) */ /* Output:none */ /************************************************************************/ int main (void) { int pos, /* Board position, 0..63 */ row, /* Row placement, 0..7 */ col; /* Column placement, 0..7 */ char board[NUM_COLS][NUM_ROWS]; /* Array to represent chess board */ char piece; /* Type of piece to play */ /* Get Input */ printf("Enter Piece (R, N, B, Q, K): "); scanf("%c", &piece); printf("Enter position (0..63): "); scanf("%d", &pos); /* Call convert_coord to get corresponding column & row position */ convert_coord(pos, &col, &row); /* Call clear_board to ensure all cells in array are empty */ clear_board (board); /* Call play to make moves based on piece */ play (col, row, piece, board); /* Display board */ print_board (board); return (0); } /************************************************************************/ /* Function: convert_coord */ /* */ /* Takes numeric position (0..63) and converts to corresponding column, */ /* row coordinates (uses pointers for col and row */ /* */ /* Input: Position from main */ /* */ /* Output: none */ /************************************************************************/ void convert_coord (int pos, int *col, int *row) { *col = pos % NUM_COLS; /* Remainder of operation is column coord */ *row = pos / NUM_ROWS; /* Result of operation is row coord */ } /************************************************************************/ /* Function: clear_board */ /* */ /* Clears all contents of the board array */ /* */ /* Input: board from main */ /* Output: none */ /************************************************************************/ void clear_board (char board[NUM_COLS][NUM_ROWS]) { int row, col; /* Local Variables */ /* Nested for loop, outer loop executes once for each row, each time */ /* outer loop executes, inner loop executes once for each column. */ /* Loop clears all contents of array. */ for (row = 0; row < NUM_ROWS; ++row) { for (col = 0; col < NUM_COLS; ++col) { board[col][row] = ' '; } } } /************************************************************************/ /* Function: play_king */ /* */ /* Places an X in each position directly around the King piece, within */ /* boundaries of the board */ /* */ /* Input: coordinates and board from main */ /* Output: none */ /************************************************************************/ void play_king (int col, int row, char board[NUM_COLS][NUM_ROWS]) { if ((col-1 >= 0) && (col-1 < NUM_COLS) && /* Boundary Checking */ (row-1 >= 0) && (row-1 < NUM_ROWS)) board[col-1][row-1] = 'X'; /* X placement */ if ((col-1 >= 0) && (col-1 < NUM_COLS) && /* Boundary Checking */ (row >= 0) && (row < NUM_ROWS)) board[col-1][row] = 'X'; /* X placement */ if ((col-1 >= 0) && (col-1 < NUM_COLS) && /* Boundary Checking */ (row+1 >= 0) && (row+1 < NUM_ROWS)) board[col-1][row+1] = 'X'; /* X placement */ if ((col >= 0) && (col < NUM_COLS) && /* Boundary Checking */ (row-1 >= 0) && (row-1 < NUM_ROWS)) board[col][row-1] = 'X'; /* X placement */ if ((col >= 0) && (col < NUM_COLS) && /* Boundary Checking */ (row+1 >= 0) && (row+1 < NUM_ROWS)) board[col][row+1] = 'X'; /* X placement */ if ((col+1 >= 0) && (col+1 < NUM_COLS) && /* Boundary Checking */ (row-1 >= 0) && (row-1 < NUM_ROWS)) board[col+1][row-1] = 'X'; /* X placement */ if ((col+1 >= 0) && (col+1 < NUM_COLS) && /* Boundary Checking */ (row >= 0) && (row < NUM_ROWS)) board[col+1][row] = 'X'; /* X placement */ if ((col+1 >= 0) && (col+1 < NUM_COLS) && /* Boundary Checking */ (row+1 >= 0) && (row+1 < NUM_ROWS)) board[col+1][row+1] = 'X'; /* X placement */ } /************************************************************************/ /* Function: play_rook */ /* */ /* Places an X in consecutive positions above, below, to the right and */ /* to the left of the rook piece, within boundaries of the board. */ /* */ /* Input: coordinates and board from main */ /* Output: none */ /************************************************************************/ void play_rook (int col, int row, char board[NUM_COLS][NUM_ROWS]) { int rook_c, rook_r; /* Local variables used to store original coords */ /* Store a copy of original coordinates */ rook_c = col; rook_r = row; /* To start at left-most column */ col = 0; /* While loop to places X's in consecutive empty cells, up to last col */ while (col < NUM_COLS) { if (board[col][row] == ' ') { board[col][row] = 'X'; col = col + 1; } else col = col + 1; } /* Reset variables col and row to original coordinates */ col = rook_c; row = rook_r; /* To start at top-most column */ row = 0; /* While loop to place X's in consecutive empty cells, up to last row */ while (row < NUM_ROWS) { if (board[col][row] == ' ') { board[col][row] = 'X'; row = row + 1; } else row = row + 1; } } /************************************************************************/ /* Function: play_bishop */ /* */ /* Places an X in consecutive positions diagonally in each direction */ /* around the bishop piece, within boundaries of the board. */ /* */ /* Input: coordinates and board from main */ /* Output: none */ /************************************************************************/ void play_bishop (int col, int row, char board[NUM_COLS][NUM_ROWS]) { int bishop_c, bishop_r; /* Local variables to store original coords */ /* Store a copy of original coordinates */ bishop_c = col; bishop_r = row; /* While loop to place X's in consecutive cells, diagonally to the right*/ /* and down from piece, within boundary */ while ((col+1>=0) && (col+1=0) && (row+1=0) && (col+1=0) && (row-1=0) && (col-1=0) && (row+1=0) && (col-1=0) && (row-1= 0) && (col+1 < NUM_COLS) && /* Boundary Checking */ (row-2 >= 0) && (row-2 < NUM_ROWS)) board[col+1][row-2] = 'X'; /* X placement */ if ((col+2 >= 0) && (col+2 < NUM_COLS) && /* Boundary Checking */ (row-1 >= 0) && (row-1 < NUM_ROWS)) board[col+2][row-1] = 'X'; /* X placement */ if ((col+2 >= 0) && (col+2 < NUM_COLS) && /* Boundary Checking */ (row+1 >= 0) && (row+1 < NUM_ROWS)) board[col+2][row+1] = 'X'; /* X placement */ if ((col+1 >= 0) && (col+1 < NUM_COLS) && /* Boundary Checking */ (row+2 >= 0) && (row+2 < NUM_ROWS)) board[col+1][row+2] = 'X'; /* X placement */ if ((col-1 >= 0) && (col-1 < NUM_COLS) && /* Boundary Checking */ (row+2 >= 0) && (row+2 < NUM_ROWS)) board[col-1][row+2] = 'X'; /* X placement */ if ((col-2 >= 0) && (col-2 < NUM_COLS) && /* Boundary Checking */ (row+1 >= 0) && (row+1 < NUM_ROWS)) board[col-2][row+1] = 'X'; /* X placement */ if ((col-2 >= 0) && (col-2 < NUM_COLS) && /* Boundary Checking */ (row-1 >= 0) && (row-1 < NUM_ROWS)) board[col-2][row-1] = 'X'; /* X placement */ if ((col-1 >= 0) && (col-1 < NUM_COLS) && /* Boundary Checking */ (row-2 >= 0) && (row-2 < NUM_ROWS)) board[col-1][row-2] = 'X'; /* X placement */ } /************************************************************************/ /* Function: play_queen */ /* */ /* Calls play_rook and play_bishop because queen is able to move like */ /* both the rook and bishop. */ /* */ /* Input: coordinates and board from main */ /* Output: none */ /************************************************************************/ void play_queen (int col, int row, char board[NUM_COLS][NUM_ROWS]) { int queen_c, queen_r; /* Local variables to store original coords */ /* Store a copy of original coordinates */ queen_c = col; queen_r = row; /* Call play_rook to place X's consecutively up, down, to the left */ /* and to the right of piece */ play_rook (col, row, board); /* Reset variables col and row to original coordinates */ col = queen_c; row = queen_r; /* Call play_bishop to place X's diaogonally around piece */ play_bishop (col, row, board); } /************************************************************************/ /* Function: play */ /* */ /* Given piece and coordinates, play plays appropriate piece */ /* */ /* Input: coordinates, piece and board from main */ /* Output: none */ /************************************************************************/ void play (int col, int row, char piece, char board[NUM_COLS][NUM_ROWS]) { switch (piece) { /* If King, place K in position and call play_king */ case 'K': board[col][row] = 'K'; play_king (col, row, board); break; /* If Rook, place R in position and call play_rook */ case 'R': board[col][row] = 'R'; play_rook (col, row, board); break; /* If Bishop, place B in position and call play_bishop */ case 'B': board[col][row] = 'B'; play_bishop (col, row, board); break; /* If Knight, place B in position and call play_knight */ case 'N': board[col][row] = 'N'; play_knight (col, row, board); break; /* If Queen, place B in position and call play_queen */ case 'Q': board[col][row] = 'Q'; play_queen (col, row, board); break; /* If none of the above, piece entered was invalid */ /* Program assumes valid data, so this case won't occur*/ default: printf("Invalid Piece\n"); } } /************************************************************************/ /* Function: print_board */ /* */ /* Once piece and X's have been assigned to their cells, print_board */ /* displays the chess board, the piece, and the possible moves. */ /* */ /* Input: board from main */ /* Output: none */ /************************************************************************/ void print_board (char board[NUM_COLS][NUM_ROWS]) { int row, col; /* Local Variables */ /* Separates board from lines requesting input */ printf("\n"); /* Prints top line of chess board */ printf("+---+---+---+---+---+---+---+---+\n"); /* For loop to print cells from left to right for each row */ for (row = 0; row < NUM_ROWS; ++row) { for (col = 0; col < NUM_COLS; ++col) { printf("| %c ", board[col][row]); } printf("|\n"); /* Prints bottom board of each row */ printf("+---+---+---+---+---+---+---+---+\n"); } }