Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Last active December 29, 2018 16:42
Show Gist options
  • Save terakilobyte/7b49f3d0defd6708ed2abe435fea1f37 to your computer and use it in GitHub Desktop.
Save terakilobyte/7b49f3d0defd6708ed2abe435fea1f37 to your computer and use it in GitHub Desktop.
borrow checker is very unhappy
fn flood_fill(board: &mut Vec<Vec<Cell>>, cell_x: usize, cell_y: usize) {
let mut queue: VecDeque<&Cell> = VecDeque::new();
queue.push_front(&board[cell_x][cell_y]);
let is: Vec<i8> = vec![-1, 0, 1];
while !queue.is_empty() {
let mut cell = queue.pop_front().unwrap();
let (x, y) = (cell.position[0] as usize, cell.position[1] as usize);
if cell.rust_count == 0 {
board[x][y].is_hidden = false; <--- mutable borrow occurs here
'outer: for i in &is {
for j in &is {
if cell.position[0] == 0. && *i == -1
|| cell.position[0] == BOARD_SIDE as f32 - 1. && *i == 1
{
continue 'outer;
}
if cell.position[1] == 0. && *j == -1
|| cell.position[1] == BOARD_SIDE as f32 - 1. && *j == 1
{
continue;
}
if *i != 0 || *j != 0 {
if !board[(cell.position[0] as i8 + *i) as usize]
[(cell.position[1] as i8 + *j) as usize]
.is_rust
{
let new_cell = &board[(cell.position[0] as i8 + *i) as usize]
[(cell.position[1] as i8 + *j) as usize];
queue.push_back(new_cell);
}
}
}
}
}
}
}
// calling fn
fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: i32, y: i32) {
let cell_x = (x / 40) as usize;
let cell_y = (y / 40) as usize;
if self.board[cell_x][cell_y].rust_count == 0 {
flood_fill(&mut self.board, cell_x, cell_y);
}
println!("Mouse pressed: {:?}, x: {}, y: {}", button, x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment