Movement with only starting cards is mostly good

This commit is contained in:
2025-06-28 00:23:13 +01:00
parent 7786cac7f5
commit 16be88c703
2 changed files with 75 additions and 11 deletions

View File

@@ -376,6 +376,18 @@ impl Klondike {
CardPosition::TopWaste | CardPosition::Foundation(_) => false CardPosition::TopWaste | CardPosition::Foundation(_) => false
} }
} }
pub fn lowest_visible_card_in_pile_from_index(self, pile: usize, index: usize) -> usize {
if self.piles[pile].len() == 0 {
return 0;
}
for (i, card) in self.piles[pile].iter().skip(index).enumerate() {
if card.visible {
return i;
}
}
panic!("Pile with only facedown cards - this is wrong")
}
} }
impl CardPosition { impl CardPosition {

View File

@@ -236,10 +236,10 @@ impl App {
KeyCode::Char('1') => self.cards.num_cards_turned = 1, KeyCode::Char('1') => self.cards.num_cards_turned = 1,
KeyCode::Char('3') => self.cards.num_cards_turned = 3, KeyCode::Char('3') => self.cards.num_cards_turned = 3,
KeyCode::Char('h') => self.show_help = !self.show_help, // toggle KeyCode::Char('h') => self.show_help = !self.show_help, // toggle
KeyCode::Left => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Left), KeyCode::Left => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Left, &self.cards),
KeyCode::Right => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Right), KeyCode::Right => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Right, &self.cards),
KeyCode::Up => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Up), KeyCode::Up => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Up, &self.cards),
KeyCode::Down => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Down), KeyCode::Down => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Down, &self.cards),
_ => {} _ => {}
} }
} }
@@ -263,7 +263,7 @@ enum Direction {
Right, Right,
} }
fn handle_move_highlighted(current_position: &CardPosition, direction: Direction) -> CardPosition { fn handle_move_highlighted(current_position: &CardPosition, direction: Direction, cards: &card_stuffs::Klondike) -> CardPosition {
match current_position { match current_position {
CardPosition::TopWaste => { CardPosition::TopWaste => {
match direction { match direction {
@@ -273,7 +273,47 @@ fn handle_move_highlighted(current_position: &CardPosition, direction: Direction
} }
}, },
CardPosition::Pile(p, i) => { CardPosition::Pile(p, i) => {
*current_position // FIXME match direction {
Direction::Down => {
if *i == cards.piles[*p].len() - 1 {
CardPosition::Pile(*p, *i)
} else {
CardPosition::Pile(*p, *i + 1)
}
},
Direction::Up => {
let lowest_shown_card = cards.clone().lowest_visible_card_in_pile_from_index(*p, 0);
if *i == lowest_shown_card {
CardPosition::TopWaste // FIXME - should move to the appropriate Waste or Foundation
} else {
CardPosition::Pile(*p, *i - 1)
}
},
Direction::Left => {
if *p == 0 {
CardPosition::Pile(*p, *i)
} else {
let lowest_shown_card = cards.clone().lowest_visible_card_in_pile_from_index(*p - 1, 0);
if lowest_shown_card <= *i && cards.piles[*p].len() <= *i {
CardPosition::Pile(*p - 1, *i) // CHECK
} else {
CardPosition::Pile(*p - 1, lowest_shown_card)
}
}
},
Direction::Right => {
if *p == 6 {
CardPosition::Pile(*p, *i)
} else {
let lowest_shown_card = cards.clone().lowest_visible_card_in_pile_from_index(*p + 1, 0);
if lowest_shown_card <= *i && cards.piles[*p].len() <= *i {
CardPosition::Pile(*p + 1, *i) // CHECK
} else {
CardPosition::Pile(*p + 1, lowest_shown_card)
}
}
},
}
}, },
CardPosition::Foundation(f) => { CardPosition::Foundation(f) => {
match direction { match direction {
@@ -288,11 +328,23 @@ fn handle_move_highlighted(current_position: &CardPosition, direction: Direction
} }
Direction::Down => { Direction::Down => {
match f { match f {
0 => *current_position, // FIXME - need a "lowest visible card" function - also need to pass more info into here 0 => {
1 => *current_position, let i = cards.clone().lowest_visible_card_in_pile_from_index(2, 0);
2 => *current_position, CardPosition::Pile(2, i)
3 => *current_position, },
_ => panic!("Should be on a higher foundation") 1 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(3, 0);
CardPosition::Pile(3, i)
},
2 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(4, 0);
CardPosition::Pile(4, i)
},
3 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(5, 0);
CardPosition::Pile(5, i)
},
_ => panic!("Can't be on a foundation this high")
} }
} }
} }