Compare commits

..

2 Commits

Author SHA1 Message Date
9f1eac75a0 Added Foundation to Pile movement
And made it make a bit more sense wrt Pile to Foundation
2025-06-28 00:30:35 +01:00
16be88c703 Movement with only starting cards is mostly good 2025-06-28 00:23:13 +01:00
2 changed files with 86 additions and 12 deletions

View File

@@ -376,6 +376,18 @@ impl Klondike {
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 {

View File

@@ -236,10 +236,10 @@ impl App {
KeyCode::Char('1') => self.cards.num_cards_turned = 1,
KeyCode::Char('3') => self.cards.num_cards_turned = 3,
KeyCode::Char('h') => self.show_help = !self.show_help, // toggle
KeyCode::Left => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Left),
KeyCode::Right => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Right),
KeyCode::Up => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Up),
KeyCode::Down => self.highlighted_card = handle_move_highlighted(&self.highlighted_card, Direction::Down),
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, &self.cards),
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, &self.cards),
_ => {}
}
}
@@ -263,17 +263,67 @@ enum Direction {
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 {
CardPosition::TopWaste => {
match direction {
Direction::Up | Direction::Left => { CardPosition::TopWaste },
Direction::Right => { CardPosition::Foundation(0) }
Direction::Down => { CardPosition:: Pile(0, 0) }
Direction::Down => {
let lowest_shown_card = cards.clone().lowest_visible_card_in_pile_from_index(1, 0);
CardPosition:: Pile(1, lowest_shown_card)
}
}
},
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 {
match *p {
0 | 1 => CardPosition::TopWaste,
2 | 3 => CardPosition::Foundation(0),
4 => CardPosition::Foundation(1),
5 => CardPosition::Foundation(2),
6 => CardPosition::Foundation(3),
_ => panic!("Should be on Pile over 6")
}
} 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) => {
match direction {
@@ -288,11 +338,23 @@ fn handle_move_highlighted(current_position: &CardPosition, direction: Direction
}
Direction::Down => {
match f {
0 => *current_position, // FIXME - need a "lowest visible card" function - also need to pass more info into here
1 => *current_position,
2 => *current_position,
3 => *current_position,
_ => panic!("Should be on a higher foundation")
0 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(3, 0);
CardPosition::Pile(3, i)
},
1 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(4, 0);
CardPosition::Pile(4, i)
},
2 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(5, 0);
CardPosition::Pile(5, i)
},
3 => {
let i = cards.clone().lowest_visible_card_in_pile_from_index(6, 0);
CardPosition::Pile(6, i)
},
_ => panic!("Can't be on a foundation this high")
}
}
}