Compare commits

...

4 Commits

Author SHA1 Message Date
367ad78198 Writing some more test - I don't think all of them should be passing
I'm a bit unsure about the suit checking.
And the whole thing overall - it's kind of just working well first time. Not enough
real code is needing to be edited hah!
2025-03-08 02:51:52 +00:00
8ce03a4b1c Moving to foundations seems to be alright
I think there's probably a few more tests I should write
2025-03-08 02:36:18 +00:00
408978dd4f Added code to push to foundation
Need to write some tests. I have no idea if it works
2025-03-08 02:05:04 +00:00
804ff3f8b1 Adding some pile and foundation stuff
Still not finished though!
2025-03-08 01:20:26 +00:00

View File

@@ -128,10 +128,12 @@ pub enum StackingError {
SameColour,
#[error("{0} is not \"next\" to {1}")]
NotAdjacent(String, String),
#[error("Foundations need to be the same suit")]
WrongSuit,
}
impl Card {
pub fn can_be_placed_on_top(&self, top: &Card) -> Result<(), StackingError> {
pub fn can_be_placed_on_pile(&self, top: &Card) -> Result<(), StackingError> {
// Can't be the same Colour
if self.suit.colour() == top.suit.colour() {
return Err(StackingError::SameColour);
@@ -144,6 +146,31 @@ impl Card {
Ok(())
}
pub fn can_be_placed_on_foundation(&self, top: &Option<Card>) -> Result<(), StackingError> {
// TODO check suit is correct
println!("hello1");
match top {
None => {
if self.value == Value::Ace {
return Ok(());
} else {
return Err(StackingError::NotAdjacent(self.to_string(), "an empty foundation".to_string()));
}
},
Some(c) => {
if self.suit != c.suit {
println!("hellosuitsame");
return Err(StackingError::WrongSuit);
}
if self.value.indexed_values() + 1 == c.value.indexed_values() {
return Ok(());
} else {
return Err(StackingError::NotAdjacent(self.to_string(), c.to_string()));
}
}
}
}
}
#[derive(Debug)]
@@ -188,6 +215,20 @@ impl Deck {
}
}
#[derive(Debug)]
pub struct CardAndPosition {
card: Option<Card>,
pos: CardPosition,
}
#[derive(Debug, PartialEq)]
pub enum CardPosition {
Deck, // I don't think this will need to be used
TopWaste, // I don't think we'd ever interact with anything other than the top of the Waste
Pile(usize, usize), // (PileNumber, Index)
Foundation(usize)
}
#[derive(Debug, Copy, Clone)]
pub enum NumPassesThroughDeck {
Unlimited,
@@ -234,7 +275,96 @@ impl Klondike {
self.current_num_passes_through_deck += 1;
}
}
//pub fn move_one_stack_to_another(self, )
pub fn move_card(self, source_card: &CardAndPosition, dest_card: &CardAndPosition) -> bool {
// TODO raise errors properly
assert!(source_card.card.is_some());
assert_eq!(source_card.card.unwrap().visible, true);
source_card.pos.is_valid_source();
dest_card.pos.is_valid_dest();
// Maybe TODO - check the .cards is the actual card in that position
match dest_card.pos {
CardPosition::Pile(_, _) => self.move_card_to_pile(&source_card, &dest_card),
CardPosition::Foundation(_f) => self.move_card_to_foundation(source_card, dest_card),
CardPosition::Deck | CardPosition::TopWaste => unreachable!()
}
}
pub fn move_card_to_foundation(mut self, source_card: &CardAndPosition, dest_card: &CardAndPosition) -> bool {
// TODO Check whether the card is the top of a pile / waste - it needs to be
// TODO actually learn Rust properly so I can figure out why I need to clone the whole struct to check a value
if source_card.pos != CardPosition::TopWaste && !self.clone().is_card_top_of_pile(&source_card.pos) {
// TODO as above - make all these proper errors
return false;
}
if source_card.card.unwrap().can_be_placed_on_foundation(&dest_card.card).is_err() {
return false;
}
// TODO actually move the cards - it should be possible from here
// There really must be a better way to extract an enum "value" than this...
if let CardPosition::Foundation(foundation_index) = dest_card.pos {
match source_card.pos {
CardPosition::TopWaste => {
let card = self.waste.pop().unwrap();
self.foundation[foundation_index].push(card);
return true;
},
CardPosition::Pile(pile_index, _) => {
let card = self.piles[pile_index].pop().unwrap();
self.foundation[foundation_index].push(card);
return true;
},
CardPosition::Deck | CardPosition::Foundation(_) => {
unreachable!()
},
}
}
unreachable!();
}
pub fn move_card_to_pile(self, source_card: &CardAndPosition, dest_card: &CardAndPosition) -> bool {
true
}
fn is_card_top_of_pile(self, pos: &CardPosition) -> bool {
// TODO consider, at which point the Pos::Pile() ranges etc are correct
match pos {
CardPosition::Pile(pile_index, card_index) => {
match self.piles.get(*pile_index) {
Some(pile_index) => {
// TODO - this is the correct palce to put a - 1
if *card_index == (pile_index.len() - 1) {
true
} else {
false
}
}
None => false
}
},
CardPosition::Deck | CardPosition::TopWaste | CardPosition::Foundation(_) => false
}
}
}
impl CardPosition {
// Unsure this is "correct" to just panic - but it really shouldn't happen
fn is_valid_dest(&self) {
match self {
CardPosition::Deck => panic!("You can't move cards to deck"),
CardPosition::TopWaste => panic!("You can't move cards to waste"),
CardPosition::Pile(_, _) | CardPosition::Foundation(_) => (),
}
}
fn is_valid_source(&self) {
match self {
CardPosition::Deck => panic!("You can't move cards from deck"),
CardPosition::TopWaste | CardPosition::Pile(_, _) | CardPosition::Foundation(_) => (),
}
}
}
impl Default for Klondike {
@@ -282,45 +412,222 @@ mod tests {
value: Value::Six,
..Default::default()
};
assert_eq!(testing_card.can_be_placed_on_top(&bad_same_suit), Err(StackingError::SameColour));
assert_eq!(testing_card.can_be_placed_on_pile(&bad_same_suit), Err(StackingError::SameColour));
let bad_same_colour = Card {
suit: Suit::Diamond,
value: Value::Six,
..Default::default()
};
assert_eq!(testing_card.can_be_placed_on_top(&bad_same_colour), Err(StackingError::SameColour));
assert_eq!(testing_card.can_be_placed_on_pile(&bad_same_colour), Err(StackingError::SameColour));
let should_stack_card = Card {
suit: Suit::Club,
value: Value::Six,
..Default::default()
};
assert_eq!(testing_card.can_be_placed_on_top(&should_stack_card), Ok(()));
assert_eq!(testing_card.can_be_placed_on_pile(&should_stack_card), Ok(()));
let value_too_high = Card {
suit: Suit::Club,
value: Value::Seven,
..Default::default()
};
let not_adj_error = testing_card.can_be_placed_on_top(&value_too_high);
let not_adj_error = testing_card.can_be_placed_on_pile(&value_too_high);
if let Err(e) = not_adj_error {
match e {
StackingError::NotAdjacent(_, _) => assert!(true),
StackingError::SameColour => assert!(false, "Colour is different - incorrect error"),
StackingError::WrongSuit => unreachable!(),
}
} else {
assert!(false, "Cards are not adjacent - should be an error")
}
}
#[test]
fn basic_foundation_stacking() {
let testing_card = Card {
suit: Suit::Spade,
value: Value::Ace,
..Default::default()
};
assert_eq!(testing_card.can_be_placed_on_foundation(&None), Ok(()));
let testing_card = Card {
suit: Suit::Spade,
value: Value::Two,
..Default::default()
};
assert!(testing_card.can_be_placed_on_foundation(&None).is_err());
}
#[test]
fn moving_to_foundation() {
// GOOD: Ace from Pile to Empty Foundation
let mut klon = Klondike::default();
let ace = Card {
suit: Suit::Spade,
value: Value::Ace,
.. Default::default()
};
klon.piles[0].push(ace.clone());
let source_card = CardAndPosition {
card: Some(ace.clone()),
pos: CardPosition::Pile(0, 1)
};
let dest_card = CardAndPosition {
card: None,
pos: CardPosition::Foundation(0),
};
assert!(klon.move_card_to_foundation(&source_card, &dest_card));
// BAD: Non-Ace from Pile to Empty Foundaction
let mut klon = Klondike::default();
let two = Card {
suit: Suit::Spade,
value: Value::Two,
.. Default::default()
};
klon.piles[0].push(two.clone());
let source_card = CardAndPosition {
card: Some(two.clone()),
pos: CardPosition::Pile(0, 1)
};
let dest_card = CardAndPosition {
card: None,
pos: CardPosition::Foundation(0),
};
assert!(!klon.move_card_to_foundation(&source_card, &dest_card));
// GOOD: Ace from TopWaste to Empty Foundaction
let mut klon = Klondike::default();
let ace = Card {
suit: Suit::Spade,
value: Value::Ace,
.. Default::default()
};
klon.waste.push(ace.clone());
let source_card = CardAndPosition {
card: Some(ace.clone()),
pos: CardPosition::TopWaste,
};
let dest_card = CardAndPosition {
card: None,
pos: CardPosition::Foundation(0),
};
assert!(klon.move_card_to_foundation(&source_card, &dest_card));
// BAD: Non-Ace from TopWaste to Empty Foundaction
let mut klon = Klondike::default();
let two = Card {
suit: Suit::Spade,
value: Value::Two,
.. Default::default()
};
klon.waste.push(two.clone());
let source_card = CardAndPosition {
card: Some(two.clone()),
pos: CardPosition::TopWaste,
};
let dest_card = CardAndPosition {
card: None,
pos: CardPosition::Foundation(0),
};
assert!(!klon.move_card_to_foundation(&source_card, &dest_card));
// GOOD: Two from TopWaste to Foundaction with Ace there
let mut klon = Klondike::default();
let ace = Card {
suit: Suit::Spade,
value: Value::Ace,
.. Default::default()
};
let two = Card {
suit: Suit::Spade,
value: Value::Two,
.. Default::default()
};
klon.waste.push(two.clone());
klon.piles[0].push(ace.clone());
let source_card = CardAndPosition {
card: Some(ace.clone()),
pos: CardPosition::TopWaste,
};
let dest_card = CardAndPosition {
card: None,
pos: CardPosition::Foundation(0),
};
assert!(klon.move_card_to_foundation(&source_card, &dest_card));
// TODO the following cases:
// - moving a card from pile to foundation when something is already there
// - moving Ace from waste to top of pile
// - moving a card frmo waste to foundation when something is already there
// => for cases where it'll both work and not work - when cards are / aren't present
}
#[test]
fn move_to_foundation_with_wrong_suit() {
let mut klon = Klondike::default();
let ace = Card {
suit: Suit::Heart,
value: Value::Ace,
.. Default::default()
};
let two = Card {
suit: Suit::Spade,
value: Value::Two,
.. Default::default()
};
klon.waste.push(two.clone());
klon.piles[0].push(ace.clone());
let source_card = CardAndPosition {
card: Some(ace.clone()),
pos: CardPosition::TopWaste,
};
let dest_card = CardAndPosition {
card: Some(two.clone()),
pos: CardPosition::Foundation(0),
};
assert!(!klon.move_card_to_foundation(&source_card, &dest_card));
// TODO this is passing - but I don't think it's passing for the right reason
}
#[test]
fn move_ace_to_foundation_with_card_present() {
let mut klon = Klondike::default();
let ace_heart = Card {
suit: Suit::Heart,
value: Value::Ace,
.. Default::default()
};
let ace_spade = Card {
suit: Suit::Spade,
value: Value::Ace,
.. Default::default()
};
klon.waste.push(ace_heart.clone());
klon.piles[0].push(ace_spade.clone());
let source_card = CardAndPosition {
card: Some(ace_heart.clone()),
pos: CardPosition::TopWaste,
};
let dest_card = CardAndPosition {
card: Some(ace_spade.clone()),
pos: CardPosition::Foundation(0),
};
assert!(!klon.move_card_to_foundation(&source_card, &dest_card));
}
#[test]
fn get_a_whole_deck() {
let d = Deck::default();
assert_eq!(d.cards.len(), 52); // Probably should test whether all cards are in... eh
println!("{:#?}", d); // A "manual" review looks alright
//println!("{:#?}", d); // A "manual" review looks alright
}
#[test]
fn klondike() {
let k = Klondike::default();
println!("{:#?}", k);
//println!("{:#?}", k);
}
}