mons_rust/models/
board.rs

1use crate::*;
2
3#[derive(Debug, PartialEq, Eq, Clone)]
4pub struct Board {
5    pub items: std::collections::HashMap<Location, Item>,
6}
7
8impl Board {
9    pub fn new() -> Self {
10        Self { items: Config::initial_items() }
11    }
12
13    pub fn new_with_items(items: std::collections::HashMap<Location, Item>) -> Self {
14        Self { items }
15    }
16
17    pub fn remove_item(&mut self, location: Location) {
18        self.items.remove(&location);
19    }
20
21    pub fn put(&mut self, item: Item, location: Location) {
22        self.items.insert(location, item);
23    }
24
25    pub fn item(&self, location: Location) -> Option<&Item> {
26        self.items.get(&location)
27    }
28
29    pub fn square(&self, location: Location) -> Square {
30        *Config::squares().get(&location).unwrap_or(&Square::Regular)
31    }
32
33    pub fn all_mons_bases(&self) -> Vec<Location> {
34        Config::squares()
35            .iter()
36            .filter_map(|(location, square)| {
37                if let Square::MonBase { .. } = square {
38                    Some(*location)
39                } else {
40                    None
41                }
42            })
43            .collect()
44    }
45
46    pub fn supermana_base(&self) -> Location {
47        *Config::squares()
48            .iter()
49            .find(|(_, square)| matches!(square, Square::SupermanaBase))
50            .expect("Expected at least one supermana base")
51            .0
52    }
53
54    pub fn all_mons_locations(&self, color: Color) -> Vec<Location> {
55        self.items
56            .iter()
57            .filter_map(|(location, item)|
58                if let Some(mon) = item.mon() {
59                    if mon.color == color {
60                        Some(*location)
61                    } else {
62                        None
63                    }
64                } else {
65                    None
66                }    
67            )
68            .collect()
69    }
70
71    pub fn all_free_regular_mana_locations(&self, color: Color) -> Vec<Location> {
72        self.items
73            .iter()
74            .filter_map(|(location, item)| match item {
75                Item::Mana { mana } => match mana {
76                    Mana::Regular(mana_color) if *mana_color == color => Some(*location),
77                    _ => None,
78                },
79                _ => None,
80            })
81            .collect()
82    }
83
84    pub fn base(&self, mon: Mon) -> Location {
85        *Config::squares()
86            .iter()
87            .find(|(_, square)| matches!(square, Square::MonBase { kind, color } if kind == &mon.kind && color == &mon.color))
88            .expect("Expected at least one base for the given mon")
89            .0
90    }
91
92    pub fn fainted_mons_locations(&self, color: Color) -> Vec<Location> {
93        self.items
94            .iter()
95            .filter_map(|(location, item)| match item {
96                Item::Mon { mon } if mon.color == color && mon.is_fainted() => Some(*location),
97                _ => None,
98            })
99            .collect()
100    }
101
102    pub fn find_mana(&self, color: Color) -> Option<Location> {
103        self.items
104            .iter()
105            .find_map(|(location, item)| match item {
106                Item::Mana { mana } => match mana {
107                    Mana::Regular(mana_color) if *mana_color == color => Some(*location),
108                    _ => None,
109                },
110                _ => None,
111            })
112    }
113
114    pub fn find_awake_angel(&self, color: Color) -> Option<Location> {
115        self.items.iter().find_map(|(location, item)|
116            if let Some(mon) = item.mon() {
117                if mon.color == color && mon.kind == MonKind::Angel && !mon.is_fainted() {
118                    Some(*location)
119                } else {
120                    None
121                }
122            } else {
123                None
124            }
125        )
126    }
127}