Approved.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

pub const START_X: f32 = 20.0;
pub const START_Y: f32 = 20.0;
pub const BOARD_SQUARE_SIZE: f32 = 20.0;
pub struct Board {
pub board: Vec<Vec<u8>>,
pub row: u8,
pub col: u8,
}
pub fn make_board(r: u8, c: u8) -> Board {
let mut init: Vec<Vec<u8>> = vec![vec![0u8; r as usize]; c as usize];
for (row, rowa) in init.iter_mut().enumerate() {
for (col, item) in rowa.iter_mut().enumerate() {
*item = (row + col) as u8 % 8;
}
}
Board {
board: init,
row: r,
col: c,
}
}
impl Board {
pub fn closest_square_coords(&mut self, x: f32, y: f32) -> (u8, u8) {
//First, normalise to within the grid.
let mut x = x;
x = x.max(START_X);
x = x.min(START_X + (BOARD_SQUARE_SIZE * f32::from(self.col)) - 1.0);
let mut y = y;
y = y.max(START_Y);
y = y.min(START_Y + (BOARD_SQUARE_SIZE * f32::from(self.row)) - 1.0);
//Then, remove the start x and y
x -= START_X;
y -= START_Y;
//Then, snap to board coords.
let x = (x / BOARD_SQUARE_SIZE) as u8;
let y = (y / BOARD_SQUARE_SIZE) as u8;
(x, y)
}
}