-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.h
More file actions
85 lines (61 loc) · 2.24 KB
/
chess.h
File metadata and controls
85 lines (61 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include <stdbool.h>
typedef enum { PIECE_TYPE_PAWN, PIECE_TYPE_KNIGHT, PIECE_TYPE_BISHOP, PIECE_TYPE_ROOK, PIECE_TYPE_QUEEN, PIECE_TYPE_KING } piece_type;
struct square {
bool has_piece;
bool is_piece_white; // if has_piece, whether the piece color on this square is white
piece_type piece_type;
};
struct move {
piece_type piece_type;
bool is_piece_white;
int source_rank, source_file;
int target_rank, target_file;
bool is_capture;
piece_type captured_piece_type; // only applies if is_capture == true
bool is_check;
bool is_mate;
// following fields only apply if piece_type == PIECE_TYPE_PAWN
bool is_promotion;
bool is_en_passant;
piece_type piece_type_promoted_to;
};
struct position {
// [rank][file]
struct square squares[8][8];
int white_king_rank;
int white_king_file;
int black_king_rank;
int black_king_file;
bool white_can_castle_kingside;
bool black_can_castle_kingside;
bool white_can_castle_queenside;
bool black_can_castle_queenside;
bool can_en_passant[8]; // per file, regardless of color
};
#define GAME_ONGOING 0
#define WHITE_WON 1
#define BLACK_WON 2
struct game_state {
struct position positions[256];
int n_positions;
struct position *current_position;
bool white_to_move;
int result;
struct move moves[256];
int n_moves;
};
// there up to 8 knight moves for a single knight
// each pair of values here are a potential knight move
static int knight_move_rank_offsets[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
static int knight_move_file_offsets[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
// a king can move in 8 directions, rank_offsets[i] and file_offsets[i] are one directional pair
static int king_move_rank_offsets[8] = { -1, -1, -1, 0, 1, 1, 1, 0 };
static int king_move_file_offsets[8] = { 1, 0, -1, -1, -1, 0, 1, 1 };
#define MOVE_IS_NOT_CHECK_OR_MATE 0
#define MOVE_IS_CHECK 1
#define MOVE_IS_MATE 2
int is_move_check_or_mate(struct position *position, struct move *move);
int find_all_possible_moves_for_piece(struct position *position, struct move *into, int rank, int file);
int find_all_possible_moves_for_color(struct position *position, struct move *into, bool color_is_white);
void apply_move_to_game_state(struct game_state *game_state, const struct move *the_move);