Now with GUI display

This commit is contained in:
Greg Hellings
2024-10-16 14:33:21 -05:00
parent 5a50e37de4
commit 529a1be7df
3 changed files with 45 additions and 5 deletions
+17 -1
View File
@@ -1,7 +1,7 @@
use crate::cell::{Cell, CellValue}; use crate::cell::{Cell, CellValue};
use std::collections::HashSet; use std::collections::HashSet;
use std::fmt; use std::fmt;
use ndarray::prelude::*; use ndarray::{prelude::*, ViewRepr};
const REGION_SIZE: usize = 9; const REGION_SIZE: usize = 9;
const SUB_REGION_SIZE: usize = 3; const SUB_REGION_SIZE: usize = 3;
@@ -74,6 +74,22 @@ impl Board {
pub fn candidates(&self, point: &Point) -> HashSet<CellValue> { pub fn candidates(&self, point: &Point) -> HashSet<CellValue> {
self.cells[point.coordinates()].candidates() self.cells[point.coordinates()].candidates()
} }
pub fn row_range(&self) -> std::ops::Range<usize> {
0..REGION_SIZE
}
pub fn col_range(&self) -> std::ops::Range<usize> {
0..REGION_SIZE
}
pub fn row_iter(&self, row: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
self.cells.slice(s![row, ..])
}
pub fn col_iter(&self, col: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
self.cells.slice(s![.., col])
}
} }
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
+1
View File
@@ -3,4 +3,5 @@ mod board;
pub use board::Board; pub use board::Board;
pub use board::Point; pub use board::Point;
pub use cell::Cell;
pub use cell::CellValue; pub use cell::CellValue;
+27 -4
View File
@@ -1,8 +1,6 @@
use iced::widget::button; use iced::widget::button;
use iced::widget::text; use iced::widget::text;
use rustdoku::Board; use rustdoku::Board;
use rustdoku::CellValue;
use rustdoku::Point;
use iced; use iced;
@@ -25,12 +23,37 @@ impl Default for State {
} }
} }
fn update(state: &mut State, message: Message) { fn display_cell(cell: &rustdoku::Cell) -> iced::Element<Message> {
let display = match cell.value() {
Some(v) => v.to_string(),
None => "?".to_string(),
};
button(text(display)).on_press(Message::Pressed).into()
}
fn update(state: &mut State, _message: Message) {
state.message = "Hello, world".to_string(); state.message = "Hello, world".to_string();
} }
fn display(state: &State) -> iced::Element<Message> { fn display(state: &State) -> iced::Element<Message> {
button(text(&state.message)).on_press(Message::Pressed).into() let mut column: Vec<iced::Element<Message>> = Vec::with_capacity(9);
for row_idx in state.board.row_range() {
let row: Vec<iced::Element<Message>> = state.board
.row_iter(row_idx)
.into_iter()
.map(|c| display_cell(c))
.into_iter()
.collect();
column.push(
iced::widget::Row::with_children(row)
.spacing(3)
.into()
);
}
iced::widget::Column::with_children(column)
.spacing(3)
.into()
} }
fn main() -> iced::Result { fn main() -> iced::Result {