Polish styling, add row sum

This commit is contained in:
Amanda Graven 2025-03-22 22:58:15 +01:00
parent 560f88949f
commit 972d18afbe
Signed by: amanda
GPG key ID: F747582C5608F4CB
2 changed files with 50 additions and 13 deletions

View file

@ -15,16 +15,20 @@ impl State {
if self.rows.is_empty() {
self.rows.push(Vec::new());
}
let row = match self.rows.last_mut() {
Some(row) if row.len() > 1 && row.last().copied() == Some(5) => {
self.rows.push(Vec::new());
self.rows.last_mut().unwrap()
}
Some(row) => row,
None => unreachable!(),
};
let row = self.rows.last_mut().unwrap();
row.push(rand::thread_rng().gen_range(1..=6));
}
pub fn row_finished(&self) -> bool {
match self.rows.last() {
Some(row) => row.len() > 1 && row.last().copied() == Some(5),
None => true,
}
}
pub fn new_row(&mut self) {
self.rows.push(Vec::new());
}
}
fn main() {
@ -34,7 +38,7 @@ fn main() {
#[component]
fn App() -> Element {
let mut rows = use_signal(State::default);
let snow = true;
let snow = false;
rsx! {
document::Link { rel: "icon", href: FAVICON }
document::Link { rel: "stylesheet", href: MAIN_CSS }
@ -49,16 +53,33 @@ fn App() -> Element {
tr {
th { colspan: 100, "Game 1" }
}
for row in &rows().rows {
for row in &rows.read().rows {
tr {
for number in &row {
td { class: "number", "{number}" }
}
td {
class: "number",
colspan: (19 - row.len()).to_string()
}
td {
class: "number",
b {
{row.iter().copied().sum::<u8>().to_string()}
}
}
}
}
}
}
button { onclick: move |_| rows.write().roll(), "Roll" }
div {
id: "controls",
if rows.read().row_finished() {
button { onclick: move |_| rows.write().new_row(), "Start new row" }
} else {
button { onclick: move |_| rows.write().roll(), "Roll" }
}
}
}
if snow {
Snow {}