Skip to content

Commit 5d57227

Browse files
committed
Fixed bug in CardDeck card count when including muck
1 parent 2b0c909 commit 5d57227

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/core/carddeck.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl CardDeck {
266266
pub fn check_deal_cards(&self, cards_to_deal: usize, include_muck: bool) -> bool {
267267
let mut total_cards = self.deck.len();
268268
if include_muck {
269-
total_cards = self.muck.len();
269+
total_cards += self.muck.len();
270270
}
271271
total_cards >= cards_to_deal
272272
}
@@ -495,6 +495,46 @@ mod tests {
495495
}
496496
}
497497

498+
#[test]
499+
fn test_calculate_cards_left_without_muck() {
500+
let mut deck = CardDeck::new(None).expect("Deck could not be created");
501+
502+
deck.shuffle(None).expect("Deck could not be shuffled");
503+
504+
let enough_cards = [true, true, true, true, true, false];
505+
const CARDS_TO_DEAL: usize = 10;
506+
const INCLUDE_MUCK: bool = false;
507+
for expect_enough_cards in enough_cards {
508+
assert!(deck.check_deal_cards(CARDS_TO_DEAL, INCLUDE_MUCK) == expect_enough_cards);
509+
if expect_enough_cards {
510+
let drawn_cards = deck
511+
.deal_cards(CARDS_TO_DEAL, INCLUDE_MUCK)
512+
.expect("Cards could not be drawn");
513+
deck.muck_cards(drawn_cards);
514+
}
515+
}
516+
}
517+
518+
#[test]
519+
fn test_calculate_cards_left_with_muck() {
520+
let mut deck = CardDeck::new(None).expect("Deck could not be created");
521+
522+
deck.shuffle(None).expect("Deck could not be shuffled");
523+
524+
let enough_cards = [true, true, true, true, true, true];
525+
const CARDS_TO_DEAL: usize = 10;
526+
const INCLUDE_MUCK: bool = true;
527+
for expect_enough_cards in enough_cards {
528+
assert!(deck.check_deal_cards(CARDS_TO_DEAL, INCLUDE_MUCK) == expect_enough_cards);
529+
if expect_enough_cards {
530+
let drawn_cards = deck
531+
.deal_cards(CARDS_TO_DEAL, INCLUDE_MUCK)
532+
.expect("Cards could not be drawn");
533+
deck.muck_cards(drawn_cards);
534+
}
535+
}
536+
}
537+
498538
// This test relies on random entropy seeding. By the very nature of random numbers and normal
499539
// curves, there will be a subset of runs that will fail since the actual percentage lands
500540
// outside if the bounds of the expected percentage (+/- 0.2%).

0 commit comments

Comments
 (0)