Sudoku up through chapter 7

This commit is contained in:
2022-11-26 09:42:41 -05:00
parent 7d2ac20ce1
commit 7f2f579241
3 changed files with 88 additions and 37 deletions

View File

@@ -19,6 +19,7 @@ pub struct GameboardViewSettings {
pub cell_edge_radius: f64,
pub selected_cell_background_color: Color,
pub text_color: Color,
pub loaded_cell_background_color: Color,
}
impl GameboardViewSettings {
@@ -36,6 +37,7 @@ impl GameboardViewSettings {
cell_edge_radius: 1.0,
selected_cell_background_color: [0.9, 0.9, 1.0, 1.0],
text_color: [0.0, 0.0, 0.1, 1.0],
loaded_cell_background_color: [1.0, 1.0, 1.0, 1.0],
}
}
}
@@ -70,21 +72,27 @@ impl GameboardView {
Rectangle::new(settings.background_color).draw(board_rect, &c.draw_state, c.transform, g);
for i in 0..9 {
for j in 0..9 {
if controller.gameboard.cells[i][j].loaded {
color_cell(
settings,
[j, i],
settings.loaded_cell_background_color,
c,
g,
);
}
}
}
if let Some(ind) = controller.selected_cell {
let cell_size = settings.size / 9.0;
let pos = [ind[0] as f64 * cell_size, ind[1] as f64 * cell_size];
let cell_rect = [
settings.position[0] + pos[0],
settings.position[1] + pos[1],
cell_size,
cell_size,
];
Rectangle::new(settings.selected_cell_background_color).draw(
cell_rect,
&c.draw_state,
c.transform,
g,
);
let color = if !controller.gameboard.cells[ind[1]][ind[0]].loaded {
settings.selected_cell_background_color
} else {
settings.loaded_cell_background_color
};
color_cell(settings, ind, color, c, g);
}
let text_image = Image::new_color(settings.text_color);
@@ -147,3 +155,23 @@ impl GameboardView {
);
}
}
fn color_cell<G: Graphics>(
settings: &GameboardViewSettings,
ind: [usize; 2],
color: [f32; 4],
c: &Context,
g: &mut G,
) {
use graphics::Rectangle;
let cell_size = settings.size / 9.0;
let pos = [ind[0] as f64 * cell_size, ind[1] as f64 * cell_size];
let cell_rect = [
settings.position[0] + pos[0],
settings.position[1] + pos[1],
cell_size,
cell_size,
];
Rectangle::new(color).draw(cell_rect, &c.draw_state, c.transform, g);
}