diff options
| author | tslil clingman <> | 2020-01-30 21:33:27 -0800 |
|---|---|---|
| committer | tslil clingman <> | 2020-01-30 21:33:27 -0800 |
| commit | cdfddde23c856f1dce37fa4bc3876fd4d77033de (patch) | |
| tree | 48de131976dedab7c198c5e38de277bb33cb2c14 /src/game.rs | |
| parent | 9fbdf6f32c18a938679cb245dbfd0434f7703b3d (diff) | |
Big restructuring for modal UI. Undo & log scrolling added
Diffstat (limited to 'src/game.rs')
| -rw-r--r-- | src/game.rs | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/src/game.rs b/src/game.rs index 5db2c48..4a80ce7 100644 --- a/src/game.rs +++ b/src/game.rs @@ -743,23 +743,28 @@ impl Game { self.size } - pub fn query_action_lines(&self) -> String { - let mut result = String::new(); + pub fn query_action_lines(&self) -> Vec<String> { + let mut result = Vec::new(); let mut newline = true; let num_actions = self.actions.len(); if num_actions > 0 { - result += "1. "; + let mut line = String::new(); + line += "1. "; for i in 0..num_actions { if i > 1 && newline { // TODO: Is placing the opponent's first stone the zeroeth action? - result += &format!("{}. ", i / 2 + 1); + line += &format!("{}. ", i / 2 + 1); } - result += &format!("{} ", self.actions[i]); + line += &format!("{} ", self.actions[i].to_string()); if i > 0 && !newline { - result.push('\n'); + result.push(line); + line = String::new(); } newline = !newline; } + if !line.is_empty() { + result.push(line); + } } result } @@ -834,6 +839,33 @@ impl Game { } } } + + pub fn undo(&mut self) -> Result<Vec<Position>, String> { + if self.actions.len() > 0 { + self.actions.pop(); + self.states.pop(); + self.turn_order = match self.actions.len() { + 0 => TurnOrder::WhitePlacesBlack, + 1 => TurnOrder::BlackPlacesWhite, + _ => TurnOrder::Normal, + }; + self.current_player = match self.current_player { + Player::Black => Player::White, + Player::White => Player::Black, + }; + // I'm too lazy to work out exactly which squares must be + // redrawn when an action is undone + let mut redraw_all: Vec<Position> = Vec::new(); + for x in 0..self.size { + for y in 0..self.size { + redraw_all.push(Position { x, y }); + } + } + Ok(redraw_all) + } else { + Err(String::from("Cannot undo in an empty game!")) + } + } } impl fmt::Display for Game { @@ -845,7 +877,7 @@ impl fmt::Display for Game { self.white_player_name, self.black_player_name, self.size, - self.query_action_lines() + self.query_action_lines().join("\n") ) } } |
