File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ /// Map representation
2
+
3
+ use std;
4
+
5
+ import io:: reader_util;
6
+
7
+ enum square {
8
+ bot,
9
+ wall,
10
+ rock,
11
+ lambda,
12
+ closed_lift,
13
+ open_lift,
14
+ earth,
15
+ empty
16
+ }
17
+
18
+ impl of to_str:: to_str for square {
19
+ fn to_str ( ) -> ~str {
20
+ alt self {
21
+ bot { ~"R " }
22
+ wall { ~"#" }
23
+ rock { ~" * " }
24
+ lambda { ~"\\ " }
25
+ closed_lift { ~"L " }
26
+ open_lift { ~"O " }
27
+ earth { ~". " }
28
+ empty { ~" " }
29
+ }
30
+ }
31
+ }
32
+
33
+ fn square_from_char(c: char) -> square {
34
+ alt c {
35
+ 'R' { bot }
36
+ '#' { wall }
37
+ '*' { rock }
38
+ '\\ ' { lambda }
39
+ 'L' { closed_lift }
40
+ 'O' { open_lift }
41
+ '.' { earth }
42
+ ' ' { empty }
43
+ _ {
44
+ #error(" invalid square: %?", c) ;
45
+ fail
46
+ }
47
+ }
48
+ }
49
+
50
+ fn read_board_grid< rdr : owned io:: reader > ( +in : rdr ) -> ~[ ~[ square ] ] {
51
+ let in = in as io:: reader ;
52
+ let mut grid = ~[ ] ;
53
+ for in. each_line |line| {
54
+ let mut row = ~[ ] ;
55
+ for line. each_char |c| {
56
+ vec:: push( row, square_from_char( c) )
57
+ }
58
+ vec:: push( grid, row)
59
+ }
60
+ let width = grid[ 0 ] . len( ) ;
61
+ for grid. each |row| { assert row. len( ) == width }
62
+ grid
63
+ }
64
+
65
+ mod test {
66
+ #[ test]
67
+ fn trivial_to_str( ) {
68
+ assert lambda. to_str( ) == "\\ "
69
+ }
70
+
71
+ #[ test]
72
+ fn read_simple_board( ) {
73
+ let s = #include_str( "./maps/contest1.map" ) ;
74
+ read_board_grid( io:: str_reader( s) ) ;
75
+ }
76
+ }
77
+
78
+ fn main( ) { }
You can’t perform that action at this time.
0 commit comments