From d3cb4d6039ea594e9ad7d8ce504177a54fef8ccb Mon Sep 17 00:00:00 2001 From: xiaoxiae <tomas@slama.dev> Date: Wed, 19 Feb 2025 15:58:11 +0100 Subject: [PATCH] =?UTF-8?q?Rust=20implementace=20nem=C4=9Bla=20stavy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- asteracer-rust/src/asteracer.rs | 47 +++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/asteracer-rust/src/asteracer.rs b/asteracer-rust/src/asteracer.rs index a893f28..f5f2631 100644 --- a/asteracer-rust/src/asteracer.rs +++ b/asteracer-rust/src/asteracer.rs @@ -161,28 +161,10 @@ pub struct Simulation { _grid: HashMap<(isize, isize), Vec<Asteroid>>, _cell_size: isize, + + _pushed_states: Vec<(Racer, Vec<bool>)>, } -/// -/// # Examples -/// ``` -/// let map_path = PathBuf::from("../../maps/test.txt"); -/// -/// let mut simulation = Simulation::load(&map_path); -/// -/// let mut tick_result: TickResult = 0; -/// -/// println!("Running simulation until collision..."); -/// -/// while tick_result & TickFlag::COLLIDED == 0 { -/// tick_result = simulation.tick(Instruction::new(0, MAX_ACCELERATION)); -/// -/// println!("{:?}", simulation.racer); -/// } -/// -/// println!("Bam!"); -/// ``` -/// impl Simulation { pub fn new(racer: Racer, asteroids: Vec<Asteroid>, goals: Vec<Goal>, bbox: BoundingBox) -> Self { let reached_goals = vec![false; goals.len()]; @@ -196,6 +178,7 @@ impl Simulation { reached_goals, _grid: HashMap::new(), _cell_size: CELL_SIZE, + _pushed_states: Vec::new(), }; for &asteroid in &simulation.asteroids { @@ -446,4 +429,28 @@ impl Simulation { Self::new(racer, asteroids, goals, bbox) } + pub fn push(&mut self) { + // Save a copy of the current racer and reached goals + self._pushed_states.push((self.racer.clone(), self.reached_goals.clone())); + } + + pub fn pop(&mut self) { + // Ensure there is a state to pop + assert!(!self._pushed_states.is_empty(), "No states to pop!"); + + // Restore the last pushed state + let (racer, reached_goals) = self._pushed_states.pop().unwrap(); + self.racer = racer; + self.reached_goals = reached_goals; + } + + pub fn apply(&mut self) { + // Ensure there is a state to apply + assert!(!self._pushed_states.is_empty(), "No states to apply!"); + + // Apply the last pushed state without removing it + let (racer, reached_goals) = self._pushed_states.last().unwrap().clone(); + self.racer = racer; + self.reached_goals = reached_goals; + } }