Implemented the card moving

Need to actually check it's working though
This commit is contained in:
2025-03-09 23:21:20 +00:00
parent b3b9fb61a5
commit ee895d81eb

View File

@@ -148,8 +148,6 @@ impl Card {
} }
pub fn can_be_placed_on_foundation(&self, top: &Option<Card>) -> Result<(), StackingError> { pub fn can_be_placed_on_foundation(&self, top: &Option<Card>) -> Result<(), StackingError> {
// TODO check suit is correct
println!("hello1");
match top { match top {
None => { None => {
if self.value == Value::Ace { if self.value == Value::Ace {
@@ -160,7 +158,6 @@ impl Card {
}, },
Some(c) => { Some(c) => {
if self.suit != c.suit { if self.suit != c.suit {
println!("hellosuitsame");
return Err(StackingError::WrongSuit); return Err(StackingError::WrongSuit);
} }
if self.value.indexed_values() + 1 == c.value.indexed_values() { if self.value.indexed_values() + 1 == c.value.indexed_values() {
@@ -330,14 +327,28 @@ impl Klondike {
CardPosition::Pile(p, i) => (p, i), CardPosition::Pile(p, i) => (p, i),
CardPosition::TopWaste | CardPosition::Foundation(_) => return false CardPosition::TopWaste | CardPosition::Foundation(_) => return false
}; };
if dcard_index != self.piles[dpile_index].len() - 1 {
// Can't move to anything other than top of pile
// this should already have been checked in the is_card_top...
// maybe I'll just delete this check...
return false;
}
match source_card.pos { match source_card.pos {
CardPosition::TopWaste => { CardPosition::TopWaste => {
let card = self.waste.pop().unwrap(); let card = self.waste.pop().unwrap();
self.piles[dpile_index].push(card); self.piles[dpile_index].push(card);
}, },
CardPosition::Pile(spile_index, scard_index) => { CardPosition::Pile(spile_index, scard_index) => {
// When I come to this, need to be careful around moving the whole pile let num_cards_to_take = self.piles[spile_index].len() - scard_index; // -1 maybe?
let mut cards: Vec<Card> = Vec::new();
for _ in 0..num_cards_to_take {
cards.push(self.piles[spile_index].pop().unwrap());
}
for card in cards {
self.piles[dpile_index].push(card);
}
// TODO Properly learn rust and why I can't use drain & extend methods
// https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain
}, },
CardPosition::Foundation(s_index) => { CardPosition::Foundation(s_index) => {
let card = self.foundation[s_index].pop().unwrap(); let card = self.foundation[s_index].pop().unwrap();
@@ -636,8 +647,53 @@ mod tests {
} }
#[test] #[test]
fn klondike() { fn move_pile_card_to_good_pile() {
let k = Klondike::default(); let mut klon = Klondike::default();
//println!("{:#?}", k); let ace = Card {
suit: Suit::Heart,
value: Value::Ace,
.. Default::default()
};
let two = Card {
suit: Suit::Spade,
value: Value::Two,
.. Default::default()
};
klon.piles[0].push(two.clone());
klon.piles[1].push(ace.clone());
let source_card = CardAndPosition {
card: Some(ace.clone()),
pos: CardPosition::Pile(1, 2),
};
let dest_card = CardAndPosition {
card: Some(two.clone()),
pos: CardPosition::Pile(0, 1),
};
assert!(klon.move_card_to_pile(&source_card, &dest_card));
}
#[test]
fn move_pile_card_to_bad_pile() {
let mut klon = Klondike::default();
let ace = Card {
suit: Suit::Heart,
value: Value::Ace,
.. Default::default()
};
let two = Card {
suit: Suit::Diamond,
value: Value::Two,
.. Default::default()
};
klon.piles[0].push(two.clone());
klon.piles[1].push(ace.clone());
let source_card = CardAndPosition {
card: Some(ace.clone()),
pos: CardPosition::Pile(1, 2),
};
let dest_card = CardAndPosition {
card: Some(two.clone()),
pos: CardPosition::Pile(0, 1),
};
assert!(!klon.move_card_to_pile(&source_card, &dest_card));
} }
} }