1 /* square.cpp
2
3 GNU Chess engine
4
5 Copyright (C) 2001-2011 Free Software Foundation, Inc.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22 // square.cpp
23
24 // includes
25
26 #include "colour.h"
27 #include "square.h"
28 #include "util.h"
29
30 namespace engine {
31
32 // "constants"
33
34 const int SquareFrom64[64] = {
35 A1, B1, C1, D1, E1, F1, G1, H1,
36 A2, B2, C2, D2, E2, F2, G2, H2,
37 A3, B3, C3, D3, E3, F3, G3, H3,
38 A4, B4, C4, D4, E4, F4, G4, H4,
39 A5, B5, C5, D5, E5, F5, G5, H5,
40 A6, B6, C6, D6, E6, F6, G6, H6,
41 A7, B7, C7, D7, E7, F7, G7, H7,
42 A8, B8, C8, D8, E8, F8, G8, H8,
43 };
44
45 const int RankMask[ColourNb] = { 0, 0xF };
46 const int PromoteRank[ColourNb] = { 0xB0, 0x40 };
47
48 // variables
49
50 int SquareTo64[SquareNb];
51 bool SquareIsPromote[SquareNb];
52
53 // functions
54
55 // square_init()
56
square_init()57 void square_init() {
58
59 int sq;
60
61 // SquareTo64[]
62
63 for (sq = 0; sq < SquareNb; sq++) SquareTo64[sq] = -1;
64
65 for (sq = 0; sq < 64; sq++) {
66 SquareTo64[SquareFrom64[sq]] = sq;
67 }
68
69 // SquareIsPromote[]
70
71 for (sq = 0; sq < SquareNb; sq++) {
72 SquareIsPromote[sq] = SQUARE_IS_OK(sq) && (SQUARE_RANK(sq) == Rank1 || SQUARE_RANK(sq) == Rank8);
73 }
74 }
75
76 // file_from_char()
77
file_from_char(int c)78 int file_from_char(int c) {
79
80 ASSERT(c>='a'&&c<='h');
81
82 return FileA + (c - 'a');
83 }
84
85 // rank_from_char()
86
rank_from_char(int c)87 int rank_from_char(int c) {
88
89 ASSERT(c>='1'&&c<='8');
90
91 return Rank1 + (c - '1');
92 }
93
94 // file_to_char()
95
file_to_char(int file)96 int file_to_char(int file) {
97
98 ASSERT(file>=FileA&&file<=FileH);
99
100 return 'a' + (file - FileA);
101 }
102
103 // rank_to_char()
104
rank_to_char(int rank)105 int rank_to_char(int rank) {
106
107 ASSERT(rank>=Rank1&&rank<=Rank8);
108
109 return '1' + (rank - Rank1);
110 }
111
112 // square_to_string()
113
square_to_string(int square,char string[],int size)114 bool square_to_string(int square, char string[], int size) {
115
116 ASSERT(SQUARE_IS_OK(square));
117 ASSERT(string!=NULL);
118 ASSERT(size>=3);
119
120 if (size < 3) return false;
121
122 string[0] = file_to_char(SQUARE_FILE(square));
123 string[1] = rank_to_char(SQUARE_RANK(square));
124 string[2] = '\0';
125
126 return true;
127 }
128
129 // square_from_string()
130
square_from_string(const char string[])131 int square_from_string(const char string[]) {
132
133 int file, rank;
134
135 ASSERT(string!=NULL);
136
137 if (string[0] < 'a' || string[0] > 'h') return SquareNone;
138 if (string[1] < '1' || string[1] > '8') return SquareNone;
139 if (string[2] != '\0') return SquareNone;
140
141 file = file_from_char(string[0]);
142 rank = rank_from_char(string[1]);
143
144 return SQUARE_MAKE(file,rank);
145 }
146
147 } // namespace engine
148
149 // end of square.cpp
150
151