1 
2 // square.h
3 
4 #ifndef SQUARE_H
5 #define SQUARE_H
6 
7 // includes
8 
9 #include "colour.h"
10 #include "util.h"
11 
12 // constants
13 
14 const int FileNb = 16;
15 const int RankNb = 16;
16 
17 const int SquareNb = FileNb * RankNb;
18 
19 const int FileInc = +1;
20 const int RankInc = +16;
21 
22 const int FileNone = 0;
23 
24 const int FileA = 0x4;
25 const int FileB = 0x5;
26 const int FileC = 0x6;
27 const int FileD = 0x7;
28 const int FileE = 0x8;
29 const int FileF = 0x9;
30 const int FileG = 0xA;
31 const int FileH = 0xB;
32 
33 const int RankNone = 0;
34 
35 const int Rank1 = 0x4;
36 const int Rank2 = 0x5;
37 const int Rank3 = 0x6;
38 const int Rank4 = 0x7;
39 const int Rank5 = 0x8;
40 const int Rank6 = 0x9;
41 const int Rank7 = 0xA;
42 const int Rank8 = 0xB;
43 
44 const int SquareNone = 0;
45 
46 const int A1=0x44, B1=0x45, C1=0x46, D1=0x47, E1=0x48, F1=0x49, G1=0x4A, H1=0x4B;
47 const int A2=0x54, B2=0x55, C2=0x56, D2=0x57, E2=0x58, F2=0x59, G2=0x5A, H2=0x5B;
48 const int A3=0x64, B3=0x65, C3=0x66, D3=0x67, E3=0x68, F3=0x69, G3=0x6A, H3=0x6B;
49 const int A4=0x74, B4=0x75, C4=0x76, D4=0x77, E4=0x78, F4=0x79, G4=0x7A, H4=0x7B;
50 const int A5=0x84, B5=0x85, C5=0x86, D5=0x87, E5=0x88, F5=0x89, G5=0x8A, H5=0x8B;
51 const int A6=0x94, B6=0x95, C6=0x96, D6=0x97, E6=0x98, F6=0x99, G6=0x9A, H6=0x9B;
52 const int A7=0xA4, B7=0xA5, C7=0xA6, D7=0xA7, E7=0xA8, F7=0xA9, G7=0xAA, H7=0xAB;
53 const int A8=0xB4, B8=0xB5, C8=0xB6, D8=0xB7, E8=0xB8, F8=0xB9, G8=0xBA, H8=0xBB;
54 
55 const int Dark  = 0;
56 const int Light = 1;
57 
58 // macros
59 
60 #define SQUARE_IS_OK(square)        ((((square)-0x44)&~0x77)==0)
61 
62 #define SQUARE_MAKE(file,rank)      (((rank)<<4)|(file))
63 
64 #define SQUARE_FILE(square)         ((square)&0xF)
65 #define SQUARE_RANK(square)         ((square)>>4)
66 
67 #define SQUARE_FROM_64(square)      (SquareFrom64[square])
68 #define SQUARE_TO_64(square)        (SquareTo64[square])
69 
70 #define SQUARE_IS_PROMOTE(square)   (SquareIsPromote[square])
71 #define SQUARE_EP_DUAL(square)      ((square)^16)
72 
73 #define SQUARE_COLOUR(square)       (((square)^((square)>>4))&1)
74 
75 #define SQUARE_FILE_MIRROR(square)  ((square)^0x0F)
76 #define SQUARE_RANK_MIRROR(square)  ((square)^0xF0)
77 
78 #define FILE_OPP(file)              ((file)^0xF)
79 #define RANK_OPP(rank)              ((rank)^0xF)
80 
81 #define PAWN_RANK(square,colour)    (SQUARE_RANK(square)^RankMask[colour])
82 #define PAWN_PROMOTE(square,colour) (PromoteRank[colour]|((square)&0xF))
83 
84 // types
85 
86 typedef int sq_t;
87 
88 // "constants"
89 
90 extern const int SquareFrom64[64];
91 extern const int RankMask[ColourNb];
92 extern const int PromoteRank[ColourNb];
93 
94 // variables
95 
96 extern int SquareTo64[SquareNb];
97 extern bool SquareIsPromote[SquareNb];
98 
99 // functions
100 
101 extern void square_init        ();
102 
103 extern int  file_from_char     (int c);
104 extern int  rank_from_char     (int c);
105 
106 extern int  file_to_char       (int file);
107 extern int  rank_to_char       (int rank);
108 
109 extern bool square_to_string   (int square, char string[], int size);
110 extern int  square_from_string (const char string[]);
111 
112 #endif // !defined SQUARE_H
113 
114 // end of square.h
115 
116