]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
223e47cc | 11 | |
c34b1796 AL |
12 | // Map representation |
13 | ||
1a4d82fc | 14 | use std::fmt; |
9346a6ac | 15 | use std::io::prelude::*; |
1a4d82fc | 16 | use square::{bot, wall, rock, lambda, closed_lift, open_lift, earth, empty}; |
223e47cc LB |
17 | |
18 | enum square { | |
19 | bot, | |
20 | wall, | |
21 | rock, | |
22 | lambda, | |
23 | closed_lift, | |
24 | open_lift, | |
25 | earth, | |
26 | empty | |
27 | } | |
28 | ||
85aaf69f | 29 | impl fmt::Debug for square { |
1a4d82fc JJ |
30 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
31 | write!(f, "{}", match *self { | |
32 | bot => { "R".to_string() } | |
33 | wall => { "#".to_string() } | |
34 | rock => { "*".to_string() } | |
35 | lambda => { "\\".to_string() } | |
36 | closed_lift => { "L".to_string() } | |
37 | open_lift => { "O".to_string() } | |
38 | earth => { ".".to_string() } | |
39 | empty => { " ".to_string() } | |
40 | }) | |
223e47cc LB |
41 | } |
42 | } | |
43 | ||
44 | fn square_from_char(c: char) -> square { | |
45 | match c { | |
46 | 'R' => { bot } | |
47 | '#' => { wall } | |
48 | '*' => { rock } | |
49 | '\\' => { lambda } | |
50 | 'L' => { closed_lift } | |
51 | 'O' => { open_lift } | |
52 | '.' => { earth } | |
53 | ' ' => { empty } | |
54 | _ => { | |
1a4d82fc JJ |
55 | println!("invalid square: {}", c); |
56 | panic!() | |
223e47cc LB |
57 | } |
58 | } | |
59 | } | |
60 | ||
9346a6ac | 61 | fn read_board_grid<rdr:'static + Read>(mut input: rdr) |
1a4d82fc | 62 | -> Vec<Vec<square>> { |
9346a6ac | 63 | let mut input: &mut Read = &mut input; |
1a4d82fc JJ |
64 | let mut grid = Vec::new(); |
65 | let mut line = [0; 10]; | |
66 | input.read(&mut line); | |
67 | let mut row = Vec::new(); | |
85aaf69f | 68 | for c in &line { |
1a4d82fc | 69 | row.push(square_from_char(*c as char)) |
223e47cc | 70 | } |
1a4d82fc | 71 | grid.push(row); |
223e47cc | 72 | let width = grid[0].len(); |
62682a34 | 73 | for row in &grid { assert_eq!(row.len(), width) } |
223e47cc LB |
74 | grid |
75 | } | |
76 | ||
77 | mod test { | |
78 | #[test] | |
1a4d82fc | 79 | pub fn trivial_to_string() { |
62682a34 | 80 | assert_eq!(lambda.to_string(), "\\") |
223e47cc | 81 | } |
223e47cc LB |
82 | } |
83 | ||
84 | pub fn main() {} |