blob: 704e8f79fb11ff5d47e376332f0518c576b1ad4c (
plain)
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
|
/// Map representation
extern mod std;
enum square {
bot,
wall,
rock,
lambda,
closed_lift,
open_lift,
earth,
empty
}
impl square: to_str::ToStr {
fn to_str() -> ~str {
match self {
bot => { ~"R" }
wall => { ~"#" }
rock => { ~"*" }
lambda => { ~"\\" }
closed_lift => { ~"L" }
open_lift => { ~"O" }
earth => { ~"." }
empty => { ~" " }
}
}
}
fn square_from_char(c: char) -> square {
match c {
'R' => { bot }
'#' => { wall }
'*' => { rock }
'\\' => { lambda }
'L' => { closed_lift }
'O' => { open_lift }
'.' => { earth }
' ' => { empty }
_ => {
#error("invalid square: %?", c);
fail
}
}
}
fn read_board_grid<rdr: Owned io::Reader>(+in: rdr) -> ~[~[square]] {
let in = in as io::Reader;
let mut grid = ~[];
for in.each_line |line| {
let mut row = ~[];
for line.each_char |c| {
vec::push(row, square_from_char(c))
}
vec::push(grid, row)
}
let width = grid[0].len();
for grid.each |row| { assert row.len() == width }
grid
}
mod test {
#[legacy_exports];
#[test]
fn trivial_to_str() {
assert lambda.to_str() == "\\"
}
#[test]
fn read_simple_board() {
let s = #include_str("./maps/contest1.map");
io::with_str_reader(s, read_board_grid)
}
}
fn main() {}
|