Movement with only starting cards is mostly good
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,7 +263,7 @@ 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 {
|
||||
@@ -273,7 +273,47 @@ fn handle_move_highlighted(current_position: &CardPosition, direction: Direction
|
||||
}
|
||||
},
|
||||
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) => {
|
||||
match direction {
|
||||
@@ -288,11 +328,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(2, 0);
|
||||
CardPosition::Pile(2, i)
|
||||
},
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user