Skip to content

Commit

Permalink
Added functionality to set up a randomized field
Browse files Browse the repository at this point in the history
  • Loading branch information
Tastaturtaste committed Jul 1, 2023
1 parent c7c6693 commit dbf2fa8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lights-out"
version = "0.1.1"
version = "0.1.2"
authors = ["Tastaturtaste <[email protected]>"]
edition = "2021"
build = "build.rs"
Expand All @@ -9,6 +9,7 @@ license = "Apache-2.0" # Has to be compatible with slint's GPLv3
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = { version = "0.8.5", default-features = false, features = ["small_rng", "getrandom"] }
slint = "1.0"

[build-dependencies]
Expand Down
41 changes: 36 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use std::{
cell::{Cell, RefCell},
rc::Rc,
Expand All @@ -23,13 +25,16 @@ impl LightsOutState {
}
fn resize(&mut self, size: usize) {
self.size = size;
let n = size * size;
self.activations.resize(n, false);
self.lights.resize(n, false);
self.switch_to_solve.resize(n, false);
self.deactivate_all();
}
fn deactivate_all(&mut self) {
let n = self.size * self.size;
self.activations = vec![false; n];
self.lights = vec![false; n];
self.switch_to_solve = vec![false; n];
self.activations = self.activations.iter().map(|_| false).collect();
self.lights = self.lights.iter().map(|_| false).collect();
self.switch_to_solve = self.switch_to_solve.iter().map(|_| false).collect();
}
}

Expand Down Expand Up @@ -133,7 +138,7 @@ fn main() -> Result<(), slint::PlatformError> {
}
});
ui.on_notify_switch_individual_lights_clicked({
let state_ref = lights_out_state;
let state_ref = lights_out_state.clone();
move |switch_individual_lights_on_click: bool| {
let mut lights_out_state = state_ref.borrow_mut();
// When switching to individual light mode nothing special has to be done.
Expand All @@ -148,6 +153,32 @@ fn main() -> Result<(), slint::PlatformError> {
);
}
});
ui.on_notify_randomize_clicked({
let state_ref = lights_out_state;
let ui_handle = ui.as_weak();
let mut rng = SmallRng::from_entropy();
move || {
let ui = ui_handle.unwrap();
let mut lights_out_state = state_ref.borrow_mut();
lights_out_state.deactivate_all();
for light in &mut lights_out_state.lights {
*light = rng.gen::<bool>();
}
lights_out_state.activations = solve_switch_system(
build_light_system_matrix(lights_out_state.size),
lights_out_state.lights.clone(),
);
ui.set_button_field_lights(
Rc::new(slint::VecModel::from(lights_out_state.lights.clone())).into(),
);
ui.set_button_field_switch_to_solve(
Rc::new(slint::VecModel::from(
lights_out_state.switch_to_solve.clone(),
))
.into(),
);
}
});
ui.run()
}

Expand Down
4 changes: 4 additions & 0 deletions ui/appwindow.slint
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export component LightsOut inherits Window {
placeholder-text: "Field size";
max-width: 100px;
}
randomize_button := Button{
text: "Randomize";
}
solve_button := Button {
text: "Solve";
}
Expand Down Expand Up @@ -83,6 +86,7 @@ export component LightsOut inherits Window {
callback notify_solve_clicked <=> solve_button.clicked;
callback notify_resize_request <=> resize_field.accepted;
callback notify_switch_individual_lights_clicked(bool);
callback notify_randomize_clicked <=> randomize_button.clicked;
in-out property <int> button_field_size <=> button_field.size;
in property <[bool]> button_field_lights <=> button_field.lit;
out property <string> requested_size <=> resize_field.text;
Expand Down

0 comments on commit dbf2fa8

Please sign in to comment.