From 71f75c87d7b8269eb86557b76f54b3fea9f04d10 Mon Sep 17 00:00:00 2001 From: Coeuvre Date: Wed, 21 May 2014 17:08:53 +0800 Subject: [PATCH] Fixed a bug that three tiles will merge into one --- src/board.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/board.rs b/src/board.rs index c12b3fd..f57fdc2 100644 --- a/src/board.rs +++ b/src/board.rs @@ -136,7 +136,9 @@ impl Board { match self.get_tile(col, row) { Some(ref d_tile) => { match self.get_next_tile(col, row, 0, y_step) { - Some(ref s_tile) if d_tile.score == s_tile.score => { + Some(ref s_tile) + if d_tile.score == s_tile.score + && self.get_tile_count(d_tile.tile_x, d_tile.tile_y) == 1 => { found = true; dx = d_tile.tile_x; dy = d_tile.tile_y; @@ -216,7 +218,9 @@ impl Board { match self.get_tile(col, row) { Some(ref d_tile) => { match self.get_next_tile(col, row, x_step, 0) { - Some(ref s_tile) if d_tile.score == s_tile.score => { + Some(ref s_tile) + if d_tile.score == s_tile.score + && self.get_tile_count(d_tile.tile_x, d_tile.tile_y) == 1 => { found = true; dx = d_tile.tile_x; dy = d_tile.tile_y; @@ -335,6 +339,16 @@ impl Board { None } + fn get_tile_count(&self, x: int, y: int) -> int { + let mut count = 0; + for tile in self.tiles.iter() { + if tile.tile_x == x && tile.tile_y == y { + count += 1; + } + } + count + } + fn render_board(&self, c: &Context, gl: &mut Gl) { let width = settings::TILE_SIZE * settings::TILE_WIDTH as f64 + settings::TILE_PADDING * (settings::TILE_WIDTH + 1) as f64; let height = settings::TILE_SIZE * settings::TILE_HEIGHT as f64 + settings::TILE_PADDING * (settings::TILE_HEIGHT + 1) as f64;