]> git.sesse.net Git - stupid/blob - position.h
Initial checkin for move to Git (no prior version history available).
[stupid] / position.h
1 #ifndef _POSITION_H
2 #define _POSITION_H 1
3
4 #include <stdbool.h>
5
6 /* 
7  * 12x10 board notation:
8  * 
9  * xx xx xx xx xx xx xx xx xx xx
10  * xx xx xx xx xx xx xx xx xx xx
11  * xx a1 b1 c1 d1 e1 f1 g1 h1 xx
12  * xx a2 b2 c2 d2 e2 f2 g2 h2 xx
13  * xx a3 b3 c3 d3 e3 f3 g3 h3 xx
14  * xx a4 b4 c4 d4 e4 f4 g4 h4 xx
15  * xx a5 b5 c5 d5 e5 f5 g5 h5 xx
16  * xx a6 b6 c6 d6 e6 f6 g6 h6 xx
17  * xx a7 b7 c7 d7 e7 f7 g7 h7 xx
18  * xx a8 b8 c8 d8 e8 f8 g8 h8 xx
19  * xx xx xx xx xx xx xx xx xx xx
20  * xx xx xx xx xx xx xx xx xx xx
21  *
22  * Lower three bits (0x07) encode piece type, fourth bit (0x08) color,
23  * upper four bits (0xf0) the piece index number.
24  */
25 #define SQUARE_TO_NUM(file, rank) ((file) + (rank) * 10 + 21)
26 #define NUM_TO_FILE(x)            ((x-1) % 10)    /* -1 might be faster than -21 */
27 #define NUM_TO_RANK(x)            ((x-21) / 10)
28
29 #define NUM_PIECES     8
30 #define NUM_SQUARES    (12*10)
31
32 #define PIECE_EMPTY    0
33 #define PIECE_PAWN     1
34 #define PIECE_KNIGHT   2
35 #define PIECE_KING     3
36 #define PIECE_INVALID  4
37 #define PIECE_BISHOP   5
38 #define PIECE_ROOK     6
39 #define PIECE_QUEEN    7
40
41 #define IS_WHITE(x) (((x) & 0x08) == 0)
42 #define IS_BLACK(x) (((x) & 0x08) != 0)
43
44 #define SAME_COLOR(x,y)     ((((x)^(y)) & 0x08) == 0)
45 #define OPPOSITE_COLOR(x,y) ((((x)^(y)) & 0x08) != 0)
46 #define CAN_GOTO(x,y) ((y) == PIECE_EMPTY || ((y) != PIECE_INVALID && OPPOSITE_COLOR((x),(y))))
47
48 #define MAKE_WHITE(x) ((x) & 0x07)
49 #define MAKE_BLACK(x) ((x) | 0x08)
50 #define MAKE_COLORLESS(x) ((x) & 0x07)
51 #define FIND_INDEX(x) (((x) & 0xf0) >> 4)
52
53 #define SLIDES_STRAIGHT(x) (((x) & 0x06) == 0x06)
54 #define SLIDES_DIAGONALLY(x) (((x) & 0x05) == 0x05)
55
56 struct piece {
57         unsigned char type;     // colorless
58         unsigned char square;
59 };
60 struct position {
61         signed char board[NUM_SQUARES];
62         struct piece black_pieces[16], white_pieces[16];
63         bool white_to_move;
64         bool wc_short, wc_long, bc_short, bc_long;
65         unsigned char ep_square;  // 0 if none
66 };
67
68 void init_position(struct position *pos);
69 void validate_position(const struct position * const pos);
70 char piece_to_char(int piece);
71 void print_position(struct position *pos);
72 void real_make_move(struct position *pos, unsigned from, unsigned to, struct piece *from_ptr, struct piece *to_ptr);
73 void make_move(struct position *pos, unsigned from, unsigned to);
74
75 #endif /* !defined(_POSITION_H) */