From 35da4b5628b664dd517e13a8941a11ce7dc69bc4 Mon Sep 17 00:00:00 2001 From: Maximillian Laumeister Date: Sun, 12 May 2024 21:09:30 -0700 Subject: [PATCH] refactor out won state --- src/game.rs | 11 +++-------- src/game/print.rs | 2 +- src/main.rs | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/game.rs b/src/game.rs index bff72a7..f8ea5b5 100644 --- a/src/game.rs +++ b/src/game.rs @@ -62,9 +62,6 @@ pub struct Game { /// Indicates whether the game is in high contrast mode, where each suit is printed in a different color. high_contrast: bool, - - /// Indicates whether the game has been won. - won: bool, } impl Game { @@ -84,8 +81,7 @@ impl Game { selected_card_opt: None, undo_history: CircularBuffer::new(), move_count: 0, - high_contrast: false, - won: false + high_contrast: false }; // Deal deck onto the board @@ -105,7 +101,8 @@ impl Game { /// /// `true` if the game has been won, otherwise `false`. pub fn is_won(&self) -> bool { - self.won + // Check if all foundation piles are full + self.field.iter().take(FOUNDATIONS).all(|stack| stack.len() == RANKS as usize) } /// Toggles high contrast mode, making diamonds magenta and spades yellow. @@ -245,8 +242,6 @@ impl Game { self.field[to].push(from_card); } self.selected_card_opt = None; - // Check to see if player won or unwon (due to undo) - self.won = self.field.iter().all(|stack| stack.len() == RANKS as usize); } } diff --git a/src/game/print.rs b/src/game/print.rs index e028705..8758d8c 100644 --- a/src/game/print.rs +++ b/src/game/print.rs @@ -59,7 +59,7 @@ impl Game { for (i, stack) in self.field.iter().enumerate() { let mut top_card = stack.last().copied().unwrap_or_default(); - let top_card_is_highlighted = self.highlighted_card == i && !self.won; + let top_card_is_highlighted = self.highlighted_card == i && !self.is_won(); if i < FOUNDATIONS { // Print foundation // If card is a placeholder, assign a suit for decoration diff --git a/src/main.rs b/src/main.rs index 0591137..ca9f476 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ // #TODO X refactor // #TODO X lint // #TODO document -// #TODO get rid of "won" in game state, compute it dynamically from a function +// #TODO X get rid of "won" in game state, compute it dynamically from a function // #TODO ci // #TODO publish // #TODO pet the coyote she has been so good