freecell/game_state/validate/
check_foundations.rs

1use super::super::GameState;
2use crate::Suit::{Club, Diamond, Heart, Spade};
3use crate::{Foundation, Suit};
4
5/// Checks whether there is one foundation of each suit and whether each individual foundation is
6/// correct.
7pub fn check_foundations(game_state: &GameState) -> Result<(), String> {
8    for suit in [Club, Spade, Heart, Diamond].iter() {
9        check_foundation(&game_state.foundations.foundation(*suit), *suit)?;
10    }
11    Ok(())
12}
13
14/// Checks whether the cards in a foundation have the required suit and are in the correct order.
15fn check_foundation(foundation: &Foundation, suit: Suit) -> Result<(), String> {
16    for (i, card) in foundation.iter().enumerate() {
17        if card.suit != suit {
18            return Err(format!(
19                "Foundation of suit {} contains card of suit {}: {}",
20                suit, card.suit, card
21            ));
22        }
23        if card.rank != 1 + i as u8 {
24            return Err(format!(
25                "Foundation of suit {} contains card of rank {} at position {}",
26                suit, card.rank, i + 1
27            ));
28        }
29    }
30    Ok(())
31}