Skip to content

Commit 85ba110

Browse files
committed
.
1 parent 623cdfc commit 85ba110

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use crate::grid::{Cell, Grid, Point};
2+
3+
fn grid_from_ascii(ascii: &str) -> Grid {
4+
let rows: Vec<_> = ascii.trim().split('\n').collect();
5+
6+
let width = rows.iter().fold(0, |max_len, r| max_len.max(r.len()));
7+
let height = rows.len();
8+
9+
let mut grid = Grid::create_empty(width as u8, height as u8);
10+
11+
for x in 0..width {
12+
for y in 0..height {
13+
let c = rows[y].chars().nth(x).unwrap_or(' ');
14+
let cell = match c {
15+
' ' => Cell::Empty,
16+
'_' => Cell::Empty,
17+
'1' => Cell::Color1,
18+
'2' => Cell::Color2,
19+
'3' => Cell::Color3,
20+
'4' => Cell::Color4,
21+
22+
'.' => Cell::Color1,
23+
'o' => Cell::Color2,
24+
'O' => Cell::Color3,
25+
'#' => Cell::Color4,
26+
_ => panic!("unknow char \"{}\"", c),
27+
};
28+
grid.set_cell(
29+
&Point {
30+
x: x as i8,
31+
y: y as i8,
32+
},
33+
cell,
34+
);
35+
}
36+
}
37+
38+
grid
39+
}
40+
41+
pub enum SampleGrid {
42+
Empty,
43+
OneDot,
44+
Realistic,
45+
Labyrinthe,
46+
}
47+
pub fn get_grid_sample(g: SampleGrid) -> Grid {
48+
grid_from_ascii(match g {
49+
SampleGrid::Empty => {
50+
&r#"
51+
_
52+
_
53+
_
54+
_
55+
"#
56+
}
57+
SampleGrid::OneDot => {
58+
&r#"
59+
_
60+
_
61+
. _
62+
_
63+
_
64+
"#
65+
}
66+
SampleGrid::Realistic => {
67+
&r#"
68+
231 412 12213 13 213 421 121131 32123112 332 _
69+
412 12 4 331213 12214431 412 413 42133123 23 21
70+
123 2143 21211 2423 213 123 1233123432 223331233_
71+
31 33 4 21 4 3321123 12331 3213133123 31 4 231 1
72+
34122 3 2144 31 31234 212 2121 211 12 3 123 3123 12
73+
442 12122122 12331123 33443 3311121 111 223 333_
74+
31413 31231 2 213321 123 32123 3332 12312 3 33 2 3
75+
"#
76+
}
77+
SampleGrid::Labyrinthe => {
78+
&r#"
79+
################################################## #
80+
# #
81+
# ##################################################
82+
# #
83+
################################################## #
84+
#. #
85+
####################################################
86+
"#
87+
}
88+
})
89+
}

0 commit comments

Comments
 (0)