freecell/game_state/validate/
check_foundations.rs1use super::super::GameState;
2use crate::Suit::{Club, Diamond, Heart, Spade};
3use crate::{Foundation, Suit};
4
5pub 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
14fn 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}