summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-06-14 15:55:02 +0300
committereug-vs <eug-vs@keemail.me>2021-06-14 15:55:02 +0300
commitee434da2fcd7873633d076790dd4689e81524fe5 (patch)
tree8b0c03bf797e0a837312cc0759f47de94896f04d
parent82851b83ff9c0b8dd0251636814995c8603a381a (diff)
downloadc-chess-ee434da2fcd7873633d076790dd4689e81524fe5.tar.gz
feat: generate legal moves
-rw-r--r--src/main.c50
-rw-r--r--src/pieces.h8
2 files changed, 55 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 69f0b43..786c882 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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!");
}
diff --git a/src/pieces.h b/src/pieces.h
index 2d5a02a..9821695 100644
--- a/src/pieces.h
+++ b/src/pieces.h
@@ -13,7 +13,7 @@
char* pieces[] = {
- ".", "#",
+ ".", "*",
"♟︎", "♙",
"♞", "♘",
"♝", "♗",
@@ -22,6 +22,12 @@ char* pieces[] = {
"♚", "♔",
};
+int bishopMoves[] = { 17, 15, -17, -15, 0 };
+int rookMoves[] = { 16, 1, -1, -16, 0 };
+int queenMoves[] = { 17, 16, 15, 1, -1, -15, -16, -17, 0 };
+int knightMoves[] = { 33, 31, 18, 14, -33, -31, -18, -14, 0 };
+int kingMoves[] = { 17, 16, 15, 1, -1, -15, -16, -17, 0 };
+int pawnMoves[] = { 17, 16, 15, 0 };
#define FILE_MASK 0b1111