diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 50 |
1 files changed, 48 insertions, 2 deletions
@@ -110,14 +110,57 @@ void input_move(int move[2]) { move[1] = notation_to_index(file_destination, rank_destination); }; +int generate_legal_moves(int origin, int* board) { + int piece = board[origin]; + int* move; + + switch (piece) { + case KNIGHT: + move = knightMoves; + break; + case BISHOP: + move = bishopMoves; + break; + case ROOK: + move = rookMoves; + break; + case QUEEN: + move = queenMoves; + break; + case KING: + move = kingMoves; + break; + case PAWN: + move = pawnMoves; + break; + default: + move = NULL; + } + + while(*move) { + for (int square = origin; !(square & 0x88); square += *move) { + if (square != origin) { + if (board[square] != EMPTY) break; + board[square] = VISUAL; + if (piece == PAWN || piece == KNIGHT || piece == KING) break; + } + } + + move++; + } +} + int apply_move(int move[2], int* board) { int origin = move[0]; int destination = move[1]; if ((origin & 0x88) || (destination & 0x88)) return 0; - board[destination] = board[origin]; + int piece = board[origin]; + + board[destination] = piece; board[origin] = EMPTY; + return board[destination]; } @@ -129,7 +172,10 @@ int main() { int move[2]; while (1) { input_move(move); - if (apply_move(move, board)) print_board(board); + if (apply_move(move, board)) { + generate_legal_moves(move[1], board); + print_board(board); + } else puts("Invalid move!"); } |