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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! [`Sokoban`](crate::Tessellation::Sokoban) board text: parsing, printing and resizing.
//!
//! A stub by design, and it should stay one. Every method body comes from the `BoardParser` /
//! `BoardPrinter` / `BoardResizer` defaults in `parser.rs`; all this module declares is which
//! tessellation's padding rule those defaults apply. Shared behavior therefore belongs in a trait
//! default rather than here.
use super::parser::{BoardParser, BoardPrinter, BoardResizer};
use crate::Tessellation;
pub struct SokobanBoardParser {}
impl BoardParser for SokobanBoardParser {}
impl BoardPrinter for SokobanBoardParser {}
impl BoardResizer for SokobanBoardParser {
const TESSELLATION: Tessellation = Tessellation::Sokoban;
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::super::*;
/// A 90 degree turn, on the smallest board where the *sense* of the turn is unmistakable.
///
/// Nothing else in the suite pins it. Injectivity, adjacency preservation, tile multisets and
/// round trips are all satisfied by a 180 degree map or by a rotation composed with a
/// translation, and `content_signature()` in `game_board.rs` normalizes translation away by
/// construction. Here the whole board is asserted, position by position.
///
/// Hand-derived, and worth reproducing rather than trusting: the raw map is `(x, y) -> (-y, x)`
/// (`doc/dev-notes/board-geometry-and-transformations.md` section 4). Over `x` in `0..5` and
/// `y` in `0..3` that spans `x'` in `-2..1` and `y'` in `0..5`, and Sokoban admits every
/// translation, so the smallest anchor is `dx = 2, dy = 0` - giving `(x, y) -> (2 - y, x)` and
/// a 3x5 result. The pusher starts leftmost, so it must end up nearest the *top*; that is the
/// direction check of section 4, and it is what a flipped sense cannot survive.
mod test_rotation {
use super::*;
#[test]
fn it_rotates_board_clockwise() -> Result<(), BoardSyntaxError> {
let src = indoc! {"#####
#@$.#
#####"};
let expected = indoc! {"###
#@#
#$#
#.#
###"};
let mut board = GameBoard::parsed(src, Tessellation::Sokoban)?;
board.rotate(Rotation::Clockwise);
assert_eq!(board.width(), 3);
assert_eq!(board.height(), 5);
assert_eq!(board.printed(true), expected);
Ok(())
}
#[test]
fn it_rotates_board_counter_clockwise() -> Result<(), BoardSyntaxError> {
let src = indoc! {"#####
#@$.#
#####"};
// The same column as clockwise, read the other way: `@` was leftmost, so counter
// clockwise sends it to the bottom.
let expected = indoc! {"###
#.#
#$#
#@#
###"};
let mut board = GameBoard::parsed(src, Tessellation::Sokoban)?;
board.rotate(Rotation::CounterClockwise);
assert_eq!(board.width(), 3);
assert_eq!(board.height(), 5);
assert_eq!(board.printed(true), expected);
Ok(())
}
}
/// Mirroring does not dispatch to a `BoardParser` implementor - it goes through
/// `transform::transformed()` - but the expected board text is Sokoban geometry, so it
/// belongs beside Sokoban.
mod test_mirroring {
use super::*;
#[test]
fn it_mirrors_board_up_down() -> Result<(), BoardSyntaxError> {
let src = indoc! {" #####
# #
#$ #
### $##
# $ $ #
### # ## # ######
# # ## ##### ..#
# $ $ ..#
##### ### #@## ..#
# #########
#######"};
let result = indoc! {"----#######--------
----#-----#########
#####-###-#@##--..#
#-$--$----------..#
#---#-##-#####--..#
###-#-##-#---######
--#--$-$-#---------
--###--$##---------
----#$--#----------
----#---#----------
----#####----------"};
let mut board = GameBoard::parsed(src, Tessellation::Sokoban)?;
board.mirror(MirrorAxis::Horizontal);
assert_eq!(board.width(), 19);
assert_eq!(board.height(), 11);
assert_eq!(board.printed(true), result);
Ok(())
}
#[test]
fn it_mirrors_board_left_right() -> Result<(), BoardSyntaxError> {
let src = indoc! {" #####
# #
#$ #
### $##
# $ $ #
### # ## # ######
# # ## ##### ..#
# $ $ ..#
##### ### #@## ..#
# #########
#######"};
let result = indoc! {"----------#####----
----------#---#----
----------#--$#----
---------##$--###--
---------#-$-$--#--
######---#-##-#-###
#..--#####-##-#---#
#..----------$--$-#
#..--##@#-###-#####
#########-----#----
--------#######----"};
let mut board = GameBoard::parsed(src, Tessellation::Sokoban)?;
board.mirror(MirrorAxis::Vertical);
assert_eq!(board.width(), 19);
assert_eq!(board.height(), 11);
assert_eq!(board.printed(true), result);
Ok(())
}
}
/// Sokoban needs no perpendicular padding on any edge, so a resize is a plain crop or
/// extend. The parity-sensitive tessellations are covered in `parser.rs`.
mod test_resizing {
use super::*;
#[test]
fn test_it_can_shrink_game_board() -> Result<(), BoardSyntaxError> {
let src = indoc! {"------------------------
------------------------
------#####-------------
------#---#-------------
------#$--#-------------
----###--$##------------
----#--$-$-#------------
--###-#-##-#---######---
--#---#-##-#####--..#---
--#-$--$----------..#---
--#####-###-#@##--..#---
------#-----#########---
------#######-----------
------------------------
------------------------
------------------------"};
let result = indoc! {" #####
# #
#$ #
### $##
# $ $ #
### # ## # ######
# # ## ##### ..#
# $ $ ..#
##### ### #@## ..#
# #########
#######"};
let mut board = GameBoard::parsed(src, Tessellation::Sokoban)?;
board.remove_columns(VerticalEdge::Left, 2);
board.remove_columns(VerticalEdge::Right, 3);
board.remove_rows(HorizontalEdge::Top, 2);
board.remove_rows(HorizontalEdge::Bottom, 3);
assert_eq!(board.width(), 19);
assert_eq!(board.height(), 11);
assert_eq!(board.printed(false), result);
Ok(())
}
#[test]
fn test_it_can_enlarge_game_board() -> Result<(), BoardSyntaxError> {
let src = indoc! {" #####
# #
#$ #
### $##
# $ $ #
### # ## # ######
# # ## ##### ..#
# $ $ ..#
##### ### #@## ..#
# #########
#######"};
let result = indoc! {"------------------------
------------------------
------#####-------------
------#---#-------------
------#$--#-------------
----###--$##------------
----#--$-$-#------------
--###-#-##-#---######---
--#---#-##-#####--..#---
--#-$--$----------..#---
--#####-###-#@##--..#---
------#-----#########---
------#######-----------
------------------------
------------------------
------------------------"};
let mut board = GameBoard::parsed(src, Tessellation::Sokoban)?;
board.add_columns(VerticalEdge::Left, 2);
board.add_columns(VerticalEdge::Right, 3);
board.add_rows(HorizontalEdge::Top, 2);
board.add_rows(HorizontalEdge::Bottom, 3);
assert_eq!(board.width(), 24);
assert_eq!(board.height(), 16);
assert_eq!(board.printed(true), result);
Ok(())
}
}
}