From 12be9c779da86a4e68ac69819bf22e351172c948 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Tue, 21 Jan 2020 21:49:41 -0800 Subject: fixed tall stack nonsense, improved parser --- src/game.rs | 26 ++++++++++++++++++-------- src/gui.rs | 2 +- src/parser.rs | 24 +++++++++++++++--------- 3 files changed, 34 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/game.rs b/src/game.rs index 4cb6fdd..99704b1 100644 --- a/src/game.rs +++ b/src/game.rs @@ -85,8 +85,8 @@ impl fmt::Display for Action { Action::Place(_, pos, stone) => write!(f, "{}{}", stone, pos), Action::Move(_, pos, direction, drops) => write!( f, - "{}{}{}{}", - drops.iter().map(|&d| d as u32).sum::(), + "{}{}{}", + // drops.iter().map(|&d| d as u32).sum::(), pos, direction, drops.into_iter().map(|q| q.to_string()).collect::() @@ -409,14 +409,15 @@ impl GameState { let idx = copy.pos_to_idx(&pos); let stack: &Stack = &self.board[self.pos_to_idx(pos)]; - let len = copy.board[idx].len(); + let stack_height = copy.board[idx].len(); let mut offset = 0; - if len > self.size as usize { - for _i in 0..self.size { + let carry_capacity = self.size as usize; + if stack_height > carry_capacity { + for _i in 0..carry_capacity { copy.board[idx].pop(); } - offset = self.size - 1; + offset = stack_height - carry_capacity; } else { copy.board[idx].clear(); } @@ -425,10 +426,19 @@ impl GameState { for drop_idx in 0..num_drops { let pos = pos_vec[pos_vec.len() - 1]; let idx = copy.pos_to_idx(&pos); - let num = drops[drop_idx]; + let num = drops[drop_idx] as usize; for i in 0..num { - copy.board[idx].push(stack[(offset + i) as usize]); + // Are we flattening? + if stack[offset + i].stone == Stone::Capstone { + if let Some(top_piece) = copy.board[idx].last() { + if top_piece.stone == Stone::Standing { + let top_stone_idx = copy.board[idx].len() - 1; + copy.board[idx][top_stone_idx].stone = Stone::Flat; + } + } + } + copy.board[idx].push(stack[offset + i]); } offset += num; diff --git a/src/gui.rs b/src/gui.rs index 12bc31f..428c8c1 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -222,7 +222,7 @@ impl BoardGUI { } } // Prevent redraw of the grid itself? - self.board_window.touch(); + self.board_window.untouch(); } pub fn read_action(&mut self) -> BGAction { diff --git a/src/parser.rs b/src/parser.rs index 73457f0..9d50377 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,12 +8,13 @@ fn parse_move(mv: &str, game: &Game) -> Result { return Err(String::from("Empty string cannot be parsed.")); } - let dc = c.unwrap(); - let sum_drops: u8; let x; + let fill_in_number; + let dc = c.unwrap(); + let mut sum_drops: u8 = 1; if (dc >= 'a') && (dc <= 'h') { - // Drop number omitted, must be 1 drop. - sum_drops = 1; + // Optional drop number omitted, assume everything + fill_in_number = true; x = dc; } else { if (dc < '1') || ('9' < dc) { @@ -21,6 +22,7 @@ fn parse_move(mv: &str, game: &Game) -> Result { "Moves must begin with a digit in 1-8 indicating the number of drops.", )); } + fill_in_number = false; sum_drops = dc.to_digit(10).unwrap() as u8; let c = chars.next(); @@ -85,18 +87,22 @@ fn parse_move(mv: &str, game: &Game) -> Result { } } - let sum = drops.iter().map(|&d| d as u32).sum::(); let pos = Position { x: x, y: y }; - if sum == 0 { - // Omitted all the drops, it's a total stack move - drops.push(0); + if fill_in_number { if let Some(stack) = game.query_square(&pos) { - drops.push(std::cmp::min(stack.len() as u8, game.get_size())); + sum_drops = std::cmp::min(stack.len() as u8, game.get_size()); } else { return Err(String::from( "Moves must specify a valid position on the game board.", )); } + } + + let sum = drops.iter().map(|&d| d as u32).sum::(); + if sum == 0 { + // Omitted all the drops, it's a total stack move + drops.push(0); + drops.push(sum_drops); } else if sum != sum_drops as u32 { return Err(format!( "The move called for {} stones, but {} were dropped.", -- cgit v1.3.1