tetraliane / tetrice

Provides core functions of Tetris.
https://crates.io/crates/tetrice
Apache License 2.0
0 stars 1 forks source link

Field doesn't get updated after a soft drop #24

Open milewski opened 1 year ago

milewski commented 1 year ago

Trying to figure out how to "tick" the game.. looks like it is done via the soft_drop() however, whenever calling it either field doesn't gets updated / or when calling game.save() the is_end is set to true which halts the game ...

loop {
 ...
 game.soft_drop();
 println!("{:?}", game.tetrimino()); // Can see Y getting updated...
 println!("{:?}", game.field().as_vec()); // Everything is empty...
}
loop {
 ...
 game.soft_drop();
 game.save();
 println!("{:?}", game.tetrimino()); // Y stop updating after the first loop
 println!("{:?}", game.field().as_vec()); // field is updated here.. but game also ends immediately
}
tetraliane commented 1 year ago

game.field() doesn't contain the current tetrimino. If you want to know the field with the current tetrimino, use the following code:

fn field_with_current_tetrimino(game: &Game) -> Vec<Vec<Cell>> {
    let mut field = game.field().as_vec().to_owned();
    for (x, y) in game.tetrimino().blocks() {
        if x < 0 || y < 0 {
            continue;
        }
        field[y as usize][x as usize] = Cell::Block(game.tetrimino().kind());
    }
    field
}

loop {
  ...
  game.soft_drop();
  println!("{:?}", field_with_current_tetrimino(&game));
}

The second example fails because game.save() saves the current tetrimino and changes it to the next one. Since the first tetrimino is right below, the next tetrimino can't go down, and it is saved outside, which halts the game.