def test_split_6(self): """Split sixes if dealer's card is two through six.""" expected1 = True expected2 = False hand = cards.Hand([ cards.Card(6, 2), cards.Card(6, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(6, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual1 = willsplit.will_split_recommended(player, hand, g) self.assertEqual(expected1, actual1) hand = cards.Hand([ cards.Card(6, 2), cards.Card(6, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(7, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual2 = willsplit.will_split_recommended(player, hand, g) self.assertEqual(expected2, actual2)
def test_has_decision_methods(self): """The players created by make_player() should have the decision methods for Players, such as will_hit and will_insure. """ # As long as no exception is raised, this test passes. phand = cards.Hand([ cards.Card(11, 3), cards.Card(1, 2), ]) dhand = cards.Hand([ cards.Card(11, 3), cards.Card(1, 2), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, None, None, 2) player = players.make_player() methods = [ player.will_hit, player.will_split, player.will_double_down, ] for method in methods: _ = method(phand, g) methods = [ player.will_buyin, player.will_insure, ] for method in methods: _ = method(g)
def one_player(): ui = LogUI() play = True deck = cards.Deck.build(6) deck.shuffle() deck.random_cut() dealer = players.Dealer(name='Dealer') player = players.AutoPlayer(name='Player', chips=200) g = game.Engine(deck, dealer, (player, ), ui=ui, buyin=2) loop = game.main(g) play = next(loop) while play: play = loop.send(play)
def test_double_down_on_10(self): """If player's hand is 10 and the dealer's card is a 9 or less, the player should double down. """ expected = False phand = cards.Hand([ cards.Card(4, 0), cards.Card(5, 0), ]) player = players.Player((phand, ), 'Terry') dhand = cards.Hand([ cards.Card(1, 0), cards.Card(8, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willdoubledown.will_double_down_recommended(player, phand, g) self.assertEqual(expected, actual) expected = False phand = cards.Hand([ cards.Card(4, 0), cards.Card(5, 0), ]) player = players.Player((phand, ), 'Terry') dhand = cards.Hand([ cards.Card(7, 0), cards.Card(8, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willdoubledown.will_double_down_recommended(player, phand, g) self.assertEqual(expected, actual)
def test_split_2_3_or_7(self): """Split twos, threes, and sevens if the dealer's card is seven or less. """ expected1 = True expected2 = False hand = cards.Hand([ cards.Card(2, 2), cards.Card(2, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(7, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual1 = willsplit.will_split_recommended(player, hand, g) self.assertEqual(expected1, actual1) hand = cards.Hand([ cards.Card(2, 2), cards.Card(2, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(8, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual2 = willsplit.will_split_recommended(player, hand, g) self.assertEqual(expected2, actual2)
def four_player(): p1 = players.AutoPlayer(name='John', chips=200) p2 = players.BetterPlayer(name='Michael', chips=200) p3 = players.NeverPlayer(name='Graham', chips=200) p4 = players.RandomPlayer(name='Terry', chips=200) ui = TableUI(seats=5) play = True deck = cards.Deck.build(6) deck.shuffle() deck.random_cut() dealer = players.Dealer(name='Dealer') g = game.Engine(deck, dealer, (p1, p2, p3, p4), ui=ui, buyin=2) loop = game.main(g) play = next(loop) while play: play = loop.send(play)
def two_player(): p1 = players.AutoPlayer(name='John', chips=200) p2 = players.BetterPlayer(name='Michael', chips=200) ui = LogUI() play = True deck = cards.Deck.build(6) deck.shuffle() deck.random_cut() dealer = players.Dealer(name='Dealer') g = game.Engine(deck, dealer, ( p1, p2, ), ui=ui, buyin=2) loop = game.main(g) play = next(loop) while play: play = loop.send(play)
def test_double_down_if_11(self): """If the player's hand is 11, player should double down.""" expected = True phand = cards.Hand([ cards.Card(5, 0), cards.Card(6, 0), ]) player = players.Player((phand, ), 'Terry') dhand = cards.Hand([ cards.Card(11, 0), cards.Card(8, 3), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willdoubledown.will_double_down_recommended(player, phand, g) self.assertEqual(expected, actual)
def test_stand(self): """will_hit_never() should never return True.""" expected = False phand = cards.Hand([ cards.Card(10, 1), cards.Card(11, 2), ]) player = players.Player((phand, ), 'John') dhand = cards.Hand([ cards.Card(5, 1), cards.Card(2, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, 20) actual = willhit.will_hit_never(None, phand, g) self.assertEqual(expected, actual)
def test_stand(self): """If the situation doesn't match the above criteria, stand.""" expected = False phand = cards.Hand([ cards.Card(10, 1), cards.Card(11, 2), ]) player = players.Player((phand, ), 'John') dhand = cards.Hand([ cards.Card(5, 1), cards.Card(2, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, 20) actual = willhit.will_hit_recommended(None, phand, g) self.assertEqual(expected, actual)
def test_split_4_5_or_8(self): """Never split fours, fives, or eights.""" expected = False hand = cards.Hand([ cards.Card(10, 2), cards.Card(10, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(7, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willsplit.will_split_recommended(player, hand, g) self.assertEqual(expected, actual)
def test_never_double_down(self): """will_double_down_never should always return False.""" expected = False phand = cards.Hand([ cards.Card(4, 0), cards.Card(6, 0), ]) player = players.Player((phand, ), 'Terry') dhand = cards.Hand([ cards.Card(11, 0), cards.Card(8, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willdoubledown.will_double_down_never(player, phand, g) self.assertEqual(expected, actual)
def test_split_ace_or_8(self): """Always split aces or eights.""" expected = True hand = cards.Hand([ cards.Card(1, 2), cards.Card(1, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(7, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willsplit.will_split_recommended(player, hand, g) self.assertEqual(expected, actual)
def test_never_split(self): """will_split_never() should return False.""" expected = False hand = cards.Hand([ cards.Card(10, 2), cards.Card(10, 1), ]) player = players.Player((hand, ), 'Graham') dhand = cards.Hand([ cards.Card(7, 3), cards.Card(10, 0, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, None) actual = willsplit.will_split_never(player, hand, g) self.assertEqual(expected, actual)
def test_do_not_hit_soft_19(self): """If the player's hand is a soft 19, don't hit.""" expected = False phand = cards.Hand([ cards.Card(1, 1), cards.Card(6, 2), cards.Card(2, 2), ]) player = players.Player((phand, ), 'John') dhand = cards.Hand([ cards.Card(2, 1), cards.Card(2, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, 20) actual = willhit.will_hit_recommended(None, phand, g) self.assertEqual(expected, actual)
def test_hit_soft_if_not_18(self): """If the player's hand contains an ace and is less than 19, the player should hit. """ expected = True phand = cards.Hand([ cards.Card(1, 1), cards.Card(7, 2), ]) player = players.Player((phand, ), 'John') dhand = cards.Hand([ cards.Card(2, 1), cards.Card(2, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, 20) actual = willhit.will_hit_recommended(None, phand, g) self.assertEqual(expected, actual)
def test_dealer_card_poor_hit_if_not_12(self): """If the dealer's up card is 2-3, the player should hit until their hand's total is 17 or greater. """ expected = True phand = cards.Hand([ cards.Card(4, 1), cards.Card(7, 2), ]) player = players.Player((phand, ), 'John') dhand = cards.Hand([ cards.Card(5, 1), cards.Card(2, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, 20) actual = willhit.will_hit_recommended(None, phand, g) self.assertEqual(expected, actual)
def test_stand_bust(self): """If the hand is bust, stand.""" expected = False phand = cards.Hand(( cards.Card(8, 2), cards.Card(6, 1), cards.Card(2, 1), cards.Card(7, 2), )) player = players.Player((phand, ), 'John') dhand = cards.Hand([ cards.Card(5, 1), cards.Card(2, 3, cards.DOWN), ]) dealer = players.Dealer((dhand, ), 'Dealer') g = game.Engine(None, dealer, (player, ), None, 20) actual = willhit.will_hit_recommended(None, phand, g) self.assertEqual(expected, actual)
def dui(): try: ui = TableUI(seats=6) deck = cards.Deck.build(6) deck.shuffle() deck.random_cut() dealer = players.Dealer(name='Dealer') playerlist = [] for index in range(4): playerlist.append(players.make_player(bet=20)) playerlist.append(players.UserPlayer(name='You', chips=200)) g = game.Engine(deck, dealer, playerlist, ui=ui, buyin=20) loop = game.main(g) play = next(loop) while play: play = loop.send(play) except Exception as ex: with open('exception.log', 'w') as fh: fh.write(str(ex.args)) tb_str = ''.join(tb.format_tb(ex.__traceback__)) fh.write(tb_str) ui.end() raise ex
def __init__(self, number_of_decks=8): self.players = [] self.number_of_decks = number_of_decks self.shoe = cards.Shoe(number_of_decks) self.dealer = players.Dealer()