summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-06-14 16:01:19 +0300
committereug-vs <eug-vs@keemail.me>2021-06-14 16:02:31 +0300
commit78c966ef9c0a3451aecb4abcc48f9e0944757990 (patch)
tree32766c7c32067dff6c29afddedfd008410c2f9f9 /src
parentee434da2fcd7873633d076790dd4689e81524fe5 (diff)
downloadc-chess-78c966ef9c0a3451aecb4abcc48f9e0944757990.tar.gz
fix: generate moves for black correctly
Diffstat (limited to 'src')
-rw-r--r--src/main.c17
-rw-r--r--src/pieces.h3
2 files changed, 11 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index 786c882..cbdd862 100644
--- a/src/main.c
+++ b/src/main.c
@@ -114,7 +114,7 @@ int generate_legal_moves(int origin, int* board) {
int piece = board[origin];
int* move;
- switch (piece) {
+ switch (piece & NO_COLOR) {
case KNIGHT:
move = knightMoves;
break;
@@ -131,21 +131,20 @@ int generate_legal_moves(int origin, int* board) {
move = kingMoves;
break;
case PAWN:
- move = pawnMoves;
+ if (piece & BLACK) move = blackPawnMoves;
+ else move = pawnMoves;
break;
default:
+ puts("No piece found!");
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;
- }
+ for (int square = origin + *move; !(square & 0x88); square += *move) {
+ if (board[square] != EMPTY) break;
+ board[square] = VISUAL;
+ if ((piece & NO_COLOR) == PAWN || piece == KNIGHT || piece == KING) break;
}
-
move++;
}
}
diff --git a/src/pieces.h b/src/pieces.h
index 9821695..2289f2c 100644
--- a/src/pieces.h
+++ b/src/pieces.h
@@ -1,5 +1,7 @@
#define WHITE 0b0000
#define BLACK 0b0001
+#define NO_COLOR 0b1110
+
#define EMPTY 0b0000
#define VISUAL 0b0001
@@ -28,6 +30,7 @@ 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 };
+int blackPawnMoves[] = { -17, -16, -15, 0 };
#define FILE_MASK 0b1111