Removed a useless Enum and started the moving to piles

This commit is contained in:
2025-03-09 22:19:46 +00:00
parent e86c705b5b
commit b3b9fb61a5
2 changed files with 47 additions and 34 deletions

View File

@@ -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 {

View File

@@ -209,7 +209,6 @@ impl App {
let selected = self.selected_card.is_some() && *c == self.selected_card.unwrap();
let a_card = match c.visible {
true => card_widget(c, is_top_card, highlight, selected),
//false => turned_over_card(is_top_card),
false => card_widget(c, is_top_card, highlight, selected),
};
frame.render_widget(
@@ -223,20 +222,7 @@ impl App {
}
if self.show_help {
let block = Block::bordered().title("Help");
let text =
"You are playing \"Legends of Soltar\" - a Klondike thingy
Press 'q' to Quit
Press '1' or '3' to change the number of cards you draw from the deck
Press 'd' to draw from your deck
Press 'w' to put the waste pile back into the deck (you can only do this when the waste is empty)";
let p = Paragraph::new(text).wrap(Wrap { trim: true });
let vertical = Layout::vertical([Constraint::Max(10)]).flex(Flex::Center);
let horizontal = Layout::horizontal([Constraint::Percentage(70)]).flex(Flex::Center);
let [area] = vertical.areas(area);
let [area] = horizontal.areas(area);
frame.render_widget(Clear, area);
frame.render_widget(p.block(block), area);
show_help(frame, &area);
}
}
@@ -276,6 +262,23 @@ fn main() -> io::Result<()> {
app_result
}
fn show_help(frame: &mut Frame, area: &Rect) {
let block = Block::bordered().title("Help");
let text =
"You are playing \"Legends of Soltar\" - a Klondike thingy
Press 'q' to Quit
Press '1' or '3' to change the number of cards you draw from the deck
Press 'd' to draw from your deck
Press 'w' to put the waste pile back into the deck (you can only do this when the waste is empty)";
let p = Paragraph::new(text).wrap(Wrap { trim: true });
let vertical = Layout::vertical([Constraint::Max(10)]).flex(Flex::Center);
let horizontal = Layout::horizontal([Constraint::Percentage(70)]).flex(Flex::Center);
let [area] = vertical.areas(*area);
let [area] = horizontal.areas(area);
frame.render_widget(Clear, area);
frame.render_widget(p.block(block), area);
}
fn deck_widget(cards_in_deck: &Vec<card_stuffs::Card>) -> Paragraph<'static> {
let card_image = format!(