Skip to content

Commit

Permalink
refactor out won state
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxLaumeister committed May 13, 2024
1 parent 6b51c91 commit 35da4b5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35da4b5

Please sign in to comment.