def test_score_pegging_stack_no_score(self): stack = [] sc, desc = score.score_pegging_stack(stack) self.assertEqual(0, sc) stack = Card.from_str_list('AC') sc, desc = score.score_pegging_stack(stack) self.assertEqual(0, sc) stack = Card.from_str_list('AC, 2S, 5D') sc, desc = score.score_pegging_stack(stack) self.assertEqual(0, sc) stack = Card.from_str_list('AC, 2S, AD') sc, desc = score.score_pegging_stack(stack) self.assertEqual(0, sc)
def next_pegging_card(self, stack, hand, turn_card): stack_score = sum([x.value for x in stack]) score_per_card = [] # tuples (score, stack_score) for card in hand: if stack_score + card.value <= 31: score_per_card.append((score.score_pegging_stack(stack + [card])[0], stack_score + card.value)) else: score_per_card.append((None, None)) if all(x[0] is None for x in score_per_card): return None best_score = max(x[0] for x in score_per_card if x[0] is not None) possible_plays = [] for i, s in enumerate(score_per_card): if s[0] == best_score: possible_plays.append((s[1], hand[i])) if possible_plays: best_possible_plays = [x for x in possible_plays if x[0] not in (5, 21)] if best_possible_plays: return best_possible_plays[0][1] else: return possible_plays[0][1] return None
def next_pegging_card(self, stack, hand, turn_card): stack_score = sum([x.value for x in stack]) score_per_card = [] for card in hand: if stack_score + card.value <= 31: score_per_card.append(score.score_pegging_stack(stack + [card])[0]) else: score_per_card.append(None) if all(x is None for x in score_per_card): return None best_score = max(x for x in score_per_card if x is not None) for i, s in enumerate(score_per_card): if s == best_score: return hand[i] return None
def test_score_pegging_stack_run_of_7(self): stack = Card.from_str_list('AD, 2D, 3S, 4H, 5H, 6H, 7C') sc, desc = score.score_pegging_stack(stack) self.assertEqual(7, sc)
def test_score_pegging_stack_run_of_4(self): stack = Card.from_str_list('2D, 3S, 4H, 5H') sc, desc = score.score_pegging_stack(stack) self.assertEqual(4, sc)
def test_score_pegging_stack_run_of_3(self): stack = Card.from_str_list('AC, JS, 2D, 3S, 4H') sc, desc = score.score_pegging_stack(stack) self.assertEqual(3, sc)
def test_score_pegging_stack_4_of_a_kind(self): stack = Card.from_str_list('AC, 2S, 2D, 2S, 2H') sc, desc = score.score_pegging_stack(stack) self.assertEqual(12, sc)
def score_pegging_stack(stack): return score.score_pegging_stack(stack)