1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
use api::*; use std::ops::{Index, IndexMut}; pub static mut STATE: Option<State> = None; #[derive(Clone, Debug)] pub struct State { pub ever: EverInfo, pub current: CurrentInfo, pub agents_strat: AgentLocal<AgentState>, } #[derive(Clone, Debug)] pub struct EverInfo { pub plateau: Plateau<Case>, } impl EverInfo { pub fn get() -> Self { let mut plateau = Plateau::fill(|pos| { let mur = match type_case(pos) { CaseType::Libre => false, CaseType::Mur => true, CaseType::Erreur => panic!("invalid pos"), }; Case { alien: None, agent: None, mur, } }); for alien in liste_aliens() { plateau[alien.pos].alien = Some(alien); } for &player in PLAYERS { for &agent in AGENTS { let pos = position_agent(player, agent); plateau[pos].agent = Some(Agent { player: Player::from_id(player), id: agent }); } } EverInfo { plateau, } } } #[derive(Clone, Debug)] pub struct CurrentInfo { pub plateau: Plateau<Case>, pub tour: i32, pub historique: Vec<ActionHist>, pub score: PlayerLocal<i32>, pub aliens: Vec<AlienInfo>, pub agents: PlayerLocal<AgentLocal<AgentInfo>>, } impl CurrentInfo { pub fn get(info: &EverInfo) -> Self { let mut aliens = Vec::new(); let mut plateau = Plateau::fill(|pos| { let alien = info_alien(pos); if let Some(alien) = alien { aliens.push(alien); } Case { alien, agent: None, mur: info.plateau[pos].mur, } }); for &player in PLAYERS { for &agent in AGENTS { let pos = position_agent(player, agent); plateau[pos].agent = Some(Agent { player: Player::from_id(player), id: agent, }); } } CurrentInfo { plateau, tour: tour_actuel(), historique: historique(), score: PlayerLocal(score(moi()), score(toi())), aliens, agents: AgentLocal::fill(|agent| AgentInfo { pos: position_agent(agent.player.to_id(), agent.id), pa: NB_POINTS_ACTION }), } } } #[derive(Clone, Debug)] pub struct Plateau<T>(pub Vec<T>); impl<T> Index<Position> for Plateau<T> { type Output = T; fn index(&self, pos: Position) -> &T { &self.0[(pos.0 * TAILLE_BANQUISE + pos.1) as usize] } } impl<T> IndexMut<Position> for Plateau<T> { fn index_mut (&mut self, pos: Position) -> &mut T { &mut self.0[(pos.0 * TAILLE_BANQUISE + pos.1) as usize] } } impl<T> Plateau<T> { pub fn fill(f: impl FnMut(Position) -> T) -> Self { Plateau(Position::every().map(f).collect()) } } #[derive(Clone, Debug)] pub struct Case { pub agent: Option<Agent>, pub alien: Option<AlienInfo>, pub mur: bool, } #[derive(Clone, Debug)] pub struct Agent { pub player: Player, pub id: i32, } impl Position { pub fn every() -> impl Iterator<Item = Position> { (0..TAILLE_BANQUISE).flat_map(|i| (0..TAILLE_BANQUISE).map(move |j| Position(i, j))) } } #[derive(Clone, Copy, Debug)] pub enum Player { Moi, Toi, } impl Player { pub fn from_id(id: i32) -> Self{ if id == moi() { Player::Moi } else { Player::Toi } } pub fn to_id(self) -> i32 { match(self) { Player::Moi => moi(), Player::Toi => toi(), } } } #[derive(Clone, Debug)] pub struct PlayerLocal<T>(pub T, pub T); impl<T> Index<Player> for PlayerLocal<T> { type Output = T; fn index(&self, player: Player) -> &T { match player { Player::Moi => &self.0, Player::Toi => &self.1, } } } impl<T> IndexMut<Player> for PlayerLocal<T> { fn index_mut (&mut self, player: Player) -> &mut T { match player { Player::Moi => &mut self.0, Player::Toi => &mut self.1, } } } impl<T> PlayerLocal<T> { pub fn fill(mut f: impl FnMut(Player) -> T) -> Self { PlayerLocal(f(Player::Moi), f(Player::Toi)) } } #[derive(Clone, Debug)] pub struct AgentLocal<T>(pub [T; NB_AGENTS as usize]); impl<T> AgentLocal<T> { fn get(&self, agent: Agent) -> &T { &self.0[agent.id as usize] } } impl<T> Index<Agent> for AgentLocal<T> { type Output = T; fn index(&self, agent: Agent) -> &T { &self.0[agent.id as usize] } } impl<T> IndexMut<Agent> for AgentLocal<T> { fn index_mut (&mut self, agent: Agent) -> &mut T { &mut self.0[agent.id as usize] } } impl<T> AgentLocal<T> { pub fn fill(mut f: impl FnMut(Agent) -> T) -> PlayerLocal<Self> { PlayerLocal::fill(|player| { AgentLocal([ f(Agent {player, id: 0}), f(Agent {player, id: 1}), f(Agent {player, id: 2}), f(Agent {player, id: 3}), ]) }) } } #[derive(Clone, Copy, Debug)] pub struct AgentInfo { pos: Position, pa: i32, } #[derive(Clone, Copy, Debug)] pub struct AgentState { }