summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-18 13:12:05 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-18 13:12:05 +0300
commita8d84eca19819b0c49a35009ce1b6e0c85406b48 (patch)
tree2e1ce258df8dc7b1d5a69bf9fbf0800583946a36 /src
parent4d12ec1ccd1f29088117b7665652c0a73475be0f (diff)
downloadc-chess-a8d84eca19819b0c49a35009ce1b6e0c85406b48.tar.gz
feat: add castle
Diffstat (limited to 'src')
-rw-r--r--src/main.c79
1 files changed, 76 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index acfc37e..52724cf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
+#include <stdlib.h>
#include "config.h"
#include "pieces.h"
@@ -171,7 +172,36 @@ int validate_move(int move[2], int color, int* board) {
legal_move++;
}
- return -5;
+ // Handle castling
+ if ((piece & NO_COLOR) == KING) {
+ if (piece % 2 == WHITE && origin == 4) {
+ if (destination == 2) {
+ for (int i = 1; i < 4; i++) {
+ if (board[i] != EMPTY) return -5;
+ }
+ return 0;
+ } else if (destination == 6) {
+ for (int i = 5; i < 7; i++) {
+ if (board[i] != EMPTY) return -5;
+ }
+ return 0;
+ }
+ } else if (piece % 2 == BLACK && origin == 116) {
+ if (destination == 114) {
+ for (int i = 114; i < 116; i++) {
+ if (board[i] != EMPTY) return -5;
+ }
+ return 0;
+ } else if (destination == 118) {
+ for (int i = 117; i < 119; i++) {
+ if (board[i] != EMPTY) return -5;
+ }
+ return 0;
+ }
+ }
+ }
+
+ return -6;
}
char* VALIDATION_ERRORS[] = {
@@ -180,6 +210,7 @@ char* VALIDATION_ERRORS[] = {
"opponent piece on origin square",
"trying to capture your own piece",
"move is not allowed",
+ "castle is not allowed",
};
int apply_move(int move[2], int* board) {
@@ -195,6 +226,27 @@ int apply_move(int move[2], int* board) {
// printf("Captured %s\n", pieces[target_piece]);
}
+ if ((piece & NO_COLOR) == KING && abs(destination - origin) == 2) {
+ // CASTLE!
+ if (destination == 2) {
+ int rook = board[0];
+ board[0] = EMPTY;
+ board[3] = rook;
+ } else if (destination == 6) {
+ int rook = board[7];
+ board[7] = EMPTY;
+ board[5] = rook;
+ } else if (destination == 114) {
+ int rook = board[112];
+ board[112] = EMPTY;
+ board[115] = rook;
+ } else if (destination == 118) {
+ int rook = board[119];
+ board[119] = EMPTY;
+ board[117] = rook;
+ }
+ }
+
return target_piece;
}
@@ -205,6 +257,27 @@ void reverse_move(int move[2], int captured_piece, int* board) {
board[origin] = piece;
board[destination] = captured_piece;
+
+ if ((piece & NO_COLOR) == KING && abs(destination - origin) == 2) {
+ // CASTLE!
+ if (destination == 2) {
+ int rook = board[3];
+ board[3] = EMPTY;
+ board[0] = rook;
+ } else if (destination == 6) {
+ int rook = board[5];
+ board[5] = EMPTY;
+ board[7] = rook;
+ } else if (destination == 114) {
+ int rook = board[115];
+ board[115] = EMPTY;
+ board[112] = rook;
+ } else if (destination == 118) {
+ int rook = board[117];
+ board[117] = EMPTY;
+ board[119] = rook;
+ }
+ }
}
@@ -244,7 +317,7 @@ int compute_material_advantage(int* board, int color) {
int sign = (piece % 2 == color) ? 1 : -1;
switch (piece & NO_COLOR) {
case KING:
- counter += 1000 * sign;
+ counter += INFINITY * sign;
break;
case PAWN:
counter += evaluate_pawn(piece, rank) * sign;
@@ -407,7 +480,7 @@ int main() {
int color = WHITE;
while (1) {
- if (color == WHITE) {
+ if (color == BLACK) {
printf("Current score is %i\n", compute_score(board));
printf("White coverage: %i, black coverage: %i\n", compute_coverage(board, WHITE), compute_coverage(board, BLACK));
printf("Enter a move for %s:\n", COLORS[color]);