]>
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 | ||
14 | #![feature(old_io)] | |
223e47cc | 15 | |
85aaf69f | 16 | use std::old_io; |
1a4d82fc JJ |
17 | use std::fmt; |
18 | use square::{bot, wall, rock, lambda, closed_lift, open_lift, earth, empty}; | |
223e47cc LB |
19 | |
20 | enum square { | |
21 | bot, | |
22 | wall, | |
23 | rock, | |
24 | lambda, | |
25 | closed_lift, | |
26 | open_lift, | |
27 | earth, | |
28 | empty | |
29 | } | |
30 | ||
85aaf69f | 31 | impl fmt::Debug for square { |
1a4d82fc JJ |
32 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
33 | write!(f, "{}", match *self { | |
34 | bot => { "R".to_string() } | |
35 | wall => { "#".to_string() } | |
36 | rock => { "*".to_string() } | |
37 | lambda => { "\\".to_string() } | |
38 | closed_lift => { "L".to_string() } | |
39 | open_lift => { "O".to_string() } | |
40 | earth => { ".".to_string() } | |
41 | empty => { " ".to_string() } | |
42 | }) | |
223e47cc LB |
43 | } |
44 | } | |
45 | ||
46 | fn square_from_char(c: char) -> square { | |
47 | match c { | |
48 | 'R' => { bot } | |
49 | '#' => { wall } | |
50 | '*' => { rock } | |
51 | '\\' => { lambda } | |
52 | 'L' => { closed_lift } | |
53 | 'O' => { open_lift } | |
54 | '.' => { earth } | |
55 | ' ' => { empty } | |
56 | _ => { | |
1a4d82fc JJ |
57 | println!("invalid square: {}", c); |
58 | panic!() | |
223e47cc LB |
59 | } |
60 | } | |
61 | } | |
62 | ||
85aaf69f | 63 | fn read_board_grid<rdr:'static + old_io::Reader>(mut input: rdr) |
1a4d82fc | 64 | -> Vec<Vec<square>> { |
85aaf69f | 65 | let mut input: &mut old_io::Reader = &mut input; |
1a4d82fc JJ |
66 | let mut grid = Vec::new(); |
67 | let mut line = [0; 10]; | |
68 | input.read(&mut line); | |
69 | let mut row = Vec::new(); | |
85aaf69f | 70 | for c in &line { |
1a4d82fc | 71 | row.push(square_from_char(*c as char)) |
223e47cc | 72 | } |
1a4d82fc | 73 | grid.push(row); |
223e47cc | 74 | let width = grid[0].len(); |
85aaf69f | 75 | for row in &grid { assert!(row.len() == width) } |
223e47cc LB |
76 | grid |
77 | } | |
78 | ||
79 | mod test { | |
80 | #[test] | |
1a4d82fc JJ |
81 | pub fn trivial_to_string() { |
82 | assert!(lambda.to_string() == "\\") | |
223e47cc | 83 | } |
223e47cc LB |
84 | } |
85 | ||
86 | pub fn main() {} |