LINE1 10 20 30 40 50 60 70 80 90 100 110 120 1301use serde::Serialize;23pub const WIDTH: usize = 24;4pub const HEIGHT: usize = 17;5pub const TILE: f32 = 16.0;67891011121314const ROWS: [&str; HEIGHT] = [15 "####ww######ww##########",16 "#GLZCSOBNF#............#",17 "#kkkkkkkkk#....c.c.....#",18 "#kkkkkkkkk#...TTTTT....#",19 "#kkIIkkkkk#...TTTTT....#",20 "#kkkkkkkkk#....c.c..p..#",21 "#kkkkkkkkk#.p.........p#",22 "####.###########.#######",23 "#p...................KK#",24 "#..VVVV................#",25 "#........RRRRRRRR......#",26 "#........RRQQRRRR......#",27 "#........RRRRRRRR......#",28 "#........RRRRRRRR......#",29 "#....................p.#",30 "#........KKK..........p#",31 "########################",32];3334pub fn is_solid(c: u8) -> bool {35 matches!(36 c,37 b'#' | b'w' | b'C' | b'I' | b'G' | b'L' | b'Z' | b'S' | b'O' | b'B' | b'N' | b'F' | b'T' | b'c' | b'K' | b'p' | b'V' | b'Q'38 )39}4041pub struct Map {42 rows: Vec<Vec<u8>>,43}4445#[derive(Serialize)]46pub struct MapView {47 pub width: usize,48 pub height: usize,49 pub tile: f32,50 pub rows: Vec<String>,51}5253impl Map {54 pub fn new() -> Self {55 let rows: Vec<Vec<u8>> = ROWS.iter().map(|r| r.as_bytes().to_vec()).collect();56 Self { rows }57 }5859 pub fn char_at(&self, tx: i32, ty: i32) -> u8 {60 if tx < 0 || ty < 0 || tx as usize >= WIDTH || ty as usize >= HEIGHT {61 return b'#';62 }63 self.rows[ty as usize][tx as usize]64 }6566 pub fn solid(&self, tx: i32, ty: i32) -> bool {67 is_solid(self.char_at(tx, ty))68 }6970 pub fn view(&self) -> MapView {71 MapView {72 width: WIDTH,73 height: HEIGHT,74 tile: TILE,75 rows: ROWS.iter().map(|r| r.to_string()).collect(),76 }77 }78}7980#[cfg(test)]81mod tests {82 use super::*;8384 #[test]85 fn rows_have_the_right_shape() {86 assert_eq!(ROWS.len(), HEIGHT);87 for (i, row) in ROWS.iter().enumerate() {88 assert_eq!(row.len(), WIDTH, "row {i} has the wrong width");89 }90 }9192 #[test]93 fn there_are_four_seats() {94 let seats = ROWS.iter().flat_map(|r| r.chars()).filter(|&c| c == 'c').count();95 assert_eq!(seats, 4);96 }97}