Removed a useless Enum and started the moving to piles
This commit is contained in:
@@ -223,7 +223,6 @@ pub struct CardAndPosition {
|
||||
|
||||
#[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)
|
||||
@@ -280,7 +279,6 @@ impl Klondike {
|
||||
// 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
|
||||
@@ -288,12 +286,13 @@ impl Klondike {
|
||||
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!()
|
||||
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 check the cards referenced in source and dest are the ones that are actually there
|
||||
// TODO - ditto this for the "move to pile" function
|
||||
// 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
|
||||
@@ -302,8 +301,7 @@ impl Klondike {
|
||||
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 => {
|
||||
@@ -316,7 +314,7 @@ impl Klondike {
|
||||
self.foundation[foundation_index].push(card);
|
||||
return true;
|
||||
},
|
||||
CardPosition::Deck | CardPosition::Foundation(_) => {
|
||||
CardPosition::Foundation(_) => {
|
||||
unreachable!()
|
||||
},
|
||||
}
|
||||
@@ -324,7 +322,28 @@ impl Klondike {
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
pub fn move_card_to_pile(self, source_card: &CardAndPosition, dest_card: &CardAndPosition) -> bool {
|
||||
pub fn move_card_to_pile(mut self, source_card: &CardAndPosition, dest_card: &CardAndPosition) -> bool {
|
||||
if source_card.card.unwrap().can_be_placed_on_pile(&dest_card.card.unwrap()).is_err() {
|
||||
return false;
|
||||
}
|
||||
let (dpile_index, dcard_index) = match dest_card.pos {
|
||||
CardPosition::Pile(p, i) => (p, i),
|
||||
CardPosition::TopWaste | CardPosition::Foundation(_) => return false
|
||||
};
|
||||
match source_card.pos {
|
||||
CardPosition::TopWaste => {
|
||||
let card = self.waste.pop().unwrap();
|
||||
self.piles[dpile_index].push(card);
|
||||
},
|
||||
CardPosition::Pile(spile_index, scard_index) => {
|
||||
// When I come to this, need to be careful around moving the whole pile
|
||||
|
||||
},
|
||||
CardPosition::Foundation(s_index) => {
|
||||
let card = self.foundation[s_index].pop().unwrap();
|
||||
self.piles[dpile_index].push(card);
|
||||
},
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -334,7 +353,6 @@ impl Klondike {
|
||||
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 {
|
||||
@@ -344,27 +362,19 @@ impl Klondike {
|
||||
None => false
|
||||
}
|
||||
},
|
||||
CardPosition::Deck | CardPosition::TopWaste | CardPosition::Foundation(_) => false
|
||||
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) {
|
||||
// TODO as with many other places - should probably raise an error instead of panic
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user