Fixed a bug that three tiles will merge into one

This commit is contained in:
Coeuvre 2014-05-21 17:08:53 +08:00
parent c46b19e8c3
commit 71f75c87d7

View file

@ -136,7 +136,9 @@ impl Board {
match self.get_tile(col, row) { match self.get_tile(col, row) {
Some(ref d_tile) => { Some(ref d_tile) => {
match self.get_next_tile(col, row, 0, y_step) { 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; found = true;
dx = d_tile.tile_x; dx = d_tile.tile_x;
dy = d_tile.tile_y; dy = d_tile.tile_y;
@ -216,7 +218,9 @@ impl Board {
match self.get_tile(col, row) { match self.get_tile(col, row) {
Some(ref d_tile) => { Some(ref d_tile) => {
match self.get_next_tile(col, row, x_step, 0) { 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; found = true;
dx = d_tile.tile_x; dx = d_tile.tile_x;
dy = d_tile.tile_y; dy = d_tile.tile_y;
@ -335,6 +339,16 @@ impl Board {
None 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) { 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 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; let height = settings::TILE_SIZE * settings::TILE_HEIGHT as f64 + settings::TILE_PADDING * (settings::TILE_HEIGHT + 1) as f64;