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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use cratemacros;
const CONGRUENT_MAPS: = ;
const
/// A mapping of positions used to canonicalize functions of a [`Board`](`crate::prelude::Board`).
///
/// To understand what `PositionMapper` does,
/// consider the following board for example.
/// ```text
/// +----+----+----+----+
/// | b | a | | |
/// +----+----+----+----+
/// | B | | | |
/// +----+----+----+----+
/// | | H | | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
/// ```
/// Boards generated by reflections, rotations and translations have
/// effectively the same value (or "equivalent") in the game.
/// For example, boards below are all equivalent to the board above.
/// ```text
/// +----+----+----+----+
/// | a | b | | |
/// +----+----+----+----+
/// | | B | | |
/// +----+----+----+----+
/// | H | | | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
///
/// +----+----+----+----+
/// | | B | b | |
/// +----+----+----+----+
/// | H | | a | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
///
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
/// | | b | a | |
/// +----+----+----+----+
/// | | B | | |
/// +----+----+----+----+
/// | | | H | |
/// +----+----+----+----+
/// ```
/// To reduce the degree of freedom of the game by identifying equivalent boards
/// is important for efficient analysis.
/// `PositionMapper` helps you to do it.
///
/// Now assign numbers from 0 to 15 to 4x4 squares as below:
/// ```text
/// +----+----+----+----+
/// | 0 | 1 | 2 | 3 |
/// +----+----+----+----+
/// | 4 | 5 | 6 | 7 |
/// +----+----+----+----+
/// | 8 | 9 | 10 | 11 |
/// +----+----+----+----+
/// | 12 | 13 | 14 | 15 |
/// +----+----+----+----+
/// ```
/// Correspondence between pieces and numbers is
/// ```text
/// b -> 0, a -> 1, B -> 4 and H -> 9.
/// ```
/// Then a mapping from
/// ```text
/// +----+----+----+----+
/// | b | a | | |
/// +----+----+----+----+
/// | B | | | |
/// +----+----+----+----+
/// | | H | | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
/// (b -> 0, a -> 1, B -> 4, H -> 9)
/// ```
/// to
/// ```text
/// +----+----+----+----+
/// | | B | b | |
/// +----+----+----+----+
/// | H | | a | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
/// | | | | |
/// +----+----+----+----+
/// (b -> 2, a -> 6, B -> 1, H -> 4)
/// ```
/// is expressed by a function f satisfying
/// ```text
/// f(0)=2, f(1)=6, f(4)=1 and f(9)=4.
/// ```
/// In general, all transformations by reflections, rotations and translations
/// can be expressed by a mapping
/// ```text
/// f: {0, 1, 2, ..., 15} -> {0, 1, 2, ..., 15}.
/// ```
/// The number of all combinations of reflections and rotations is 8
/// (2 reflections times 4 rotations) including identity function.
/// It gives 8 kinds of position mappings, or in mathematical notation,
/// ```text
/// f_i: {0, 1, ..., 15} -> {0, 1, ..., 15} (i=0, 1, ..., 7),
/// ```
/// which is what `PositionMapper` provides.
///
/// In the above example, the minimum rectangle, the rectangle that contains all pieces,
/// has a shape of 3x2.
/// The mapper that matches the situation is constructed by the following:
/// ```rust
/// use tokyodoves::analysis::PositionMapper;
/// let mapper = PositionMapper::try_create(3, 2).unwrap();
/// ```
/// The [`map`](`PositionMapper::map`) method maps the position number.
/// The first argument `index` indicates the kind of mapping (from 0 to 7),
/// and the second argument `pos` is the position number (from 0 to 15).
/// Formaly,
/// ```text
/// y = mapper.map(i, x)
/// ```
/// is equivalent to
/// ```text
/// y = f_i(x).
/// ```
/// Thus the mapping in the above example realizes for some `index`.
///
/// Note that `PositionMapper` does NOT treat translational transformations.
/// It supposes that the top-left corner of the minimum rectangle
/// coincides with that of 4x4 matrix, and does the same for mapped boards.
/// For full reduction of the degree of freedom,
/// implement some functions to translate the boards so that the top-left corner
/// of the minimum rectangle moves to that of 4x4 matrix,
/// besides `PositionMapper`,
pub