def test_getrank(self): ''' test get_rank method ''' rank = 5 c5s = Card(rank, 's') self.assertEqual(rank, c5s.get_rank(), "rank 5 from card5s")
def test_getsuit(self): ''' test get_suit method ''' suit = 'h' c5h = Card(5, suit) self.assertEqual(suit, c5h.get_suit(), "rank h from card5h")
def test_constructor(self): ''' Shoe() Shoe(8) ''' # test good construction try: shoe = Shoe(8) except Exception as ex: self.fail("ex(%s)" % str(ex)) self.assertIsNotNone(shoe, "not none Shoe(8)") # test bad construction expected = "number_decks(8) invalid value(8)" try: shoe = Shoe("8") self.fail("expected a failure because of string param") except Exception as ex: actual = str(ex) self.assertEqual(expected, actual, 'msg Shoe("8")(%s)' % actual) # default constructor shoe = Shoe() self.assertIsNotNone(shoe, "default blank shoe exists") # custom shoe shoe = Shoe([Card(43), Card(44), Card(45)]) self.assertIsNotNone(shoe, "small custom 3 card shoe exists")
def test_ordinal(self): ''' test ordinal method ''' c5s = Card(5, 's') # spades are 4th suit (3*13) = +39 # five rank is 5-1 within spades range self.assertEqual((39 + 5 - 1), c5s.get_ordinal(), "card5s ordinal")
def test_ge(self): ''' __ge__ ''' card1 = Card(5, 's') card2 = Card(7, 'h') try: result = card1 >= card2 self.fail("should not continue") except NotImplementedError as ex: self.assertEqual("GE does not have meaning, so not permitted", str(ex))
def test_eq(self): ''' test __eq__ method ''' card1 = Card(5, 's') card2 = Card(7, 'h') card3 = Card(5, 's') class CardChild1(Card): pass card4 = CardChild1(5, 's') card5 = CardChild1(7, 'h') card6 = Card(5, 's') # test 3 fundamental tests of same class EQ self.assertTrue(card1 == card1, "reflexive") self.assertTrue(card1 is card1, "same instance") self.assertFalse(card1 == card2, "false A==B") self.assertFalse(card2 == card1, "false B==A") self.assertFalse(card1 is card2, "not same instance 1") self.assertFalse(card2 is card1, "not same instance 2") self.assertTrue(card1 == card3, "2 diff but same value") self.assertTrue(card3 == card1, "opposite of 1==3") self.assertTrue(card1 == card6, "transative test1") self.assertTrue(card6 == card1, "T test2") self.assertTrue(card3 == card6, "T test3") self.assertTrue(card6 == card3, "T test4") self.assertFalse(card1 is card3, "not same 3") self.assertFalse(card1 is card6, "not same 6") # basic same class tests done # test Card class against a Card subclass self.assertTrue(card1 == card4, "child class same value") self.assertTrue(card4 == card1, "opp child class same") self.assertFalse(card1 == card5, "child class diff value") self.assertFalse(card5 == card1, "opp child class diff") self.assertFalse(card1 is card4, "not same 4") self.assertFalse(card1 is card5, "not same 5") # set tests set_test = set([card1, card3]) actual = len(set_test) self.assertEqual(1, actual, "card1 and 3 EQ actual(%d)" % actual) set_test2 = set([card1, card3, card6]) # card6 is from child class actual = len(set_test2) self.assertEqual(1, actual, "card1,3,6 actual(%d)" % actual) set_test3 = set([card1, card3, card6, card5]) actual = len(set_test3) self.assertEqual(2, actual, "card5 is unique")
def test_hash(self): ''' __hash__ ''' card1 = Card(5, 's') card2 = Card(7, 'h') card3 = Card(5, 's') h1 = card1.__hash__() # print("h1(%s)" % str(h1)) self.assertTrue(h1 == card3.__hash__(), "card 1 and 3 same value") self.assertFalse(h1 == card2.__hash__(), "card 1 and 2 diff")
def test_get_card(self): ''' get_card(int) ''' player = Hand() self.assertIsNone(player.get_card(0), 'empty hand') card6s = Card(6, 's') card7h = Card(7, 'h') cardAd = Card(1, 'd') player.add(card6s) player.add(card7h) player.add(cardAd) returned_card = player.get_card(2) self.assertEqual(1, returned_card.get_rank(), 'get back Ad')
def test_need_hit(self): ''' need_hit() need_hit(player) ''' player = Hand() banker = Hand() card6s = Card(6, 's') card7h = Card(7, 'h') cardAd = Card(1, 'd') # normal player hit uses other=None player.add(card6s) player.add(card7h) self.assertTrue(player.need_hit(None), 'player 6+7 needs a hit') player.empty() player.add(card6s) player.add(cardAd) self.assertFalse(player.need_hit(None), 'player 6+A no hit') # normal banker must pass other=player player.empty() banker.empty() player.add(card6s) player.add(cardAd) banker.add(card6s) banker.add(card6s) self.assertTrue(banker.need_hit(player), 'banker 6+6 v player 6+A; hit') player.empty() banker.empty() player.add(card6s) player.add(card7h) player.add(cardAd) banker.add(card7h) banker.add(card7h) self.assertFalse(banker.need_hit(player), 'banker 4 v. P6+7+1; no hit') player.empty() banker.empty() player.add(card6s) player.add(card7h) player.add(cardAd) banker.add(card7h) banker.add(card6s) self.assertTrue(banker.need_hit(player), 'banker 3 v. P6+7+1; hit')
def test_add(self): ''' add(card) ''' player = Hand() card6s = Card(6, 's') self.assertEqual(0, player.value(), "no cards value 0") self.assertEqual("[]", str(player), "no cards") player.add(card6s) self.assertEqual(6, player.value(), "1 six = 6 value") self.assertEqual("[6s]", str(player), "add 6s") player.add(card6s) self.assertEqual(2, player.value(), "2 sixes = 2 value") self.assertEqual("[6s,6s]", str(player), "2 cards") player.add(card6s) self.assertEqual(8, player.value(), "3 sixes = 8 value") self.assertEqual("[6s,6s,6s]", str(player), "3 cards") # add an illegal 4th card expected = "too many cards in hand, can not add more" try: player.add(card6s) self.fail("should have failed adding 4th card") except ValueError as ex: self.assertEqual(expected, str(ex), "adding 4th card")
def test_ne(self): '''! test __ne__ method ''' card1 = Card(5, 's') card2 = Card(7, 'h') card3 = Card(5, 's') class CardChild1(Card): pass card4 = CardChild1(5, 's') card5 = CardChild1(7, 'h') card6 = Card(5, 's') # test 3 fundamental tests of same class EQ self.assertFalse(card1 != card1, "reflexive") self.assertTrue(card1 is card1, "same instance") self.assertTrue(card1 != card2, "false A==B") self.assertTrue(card2 != card1, "false B==A") self.assertFalse(card1 is card2, "not same instance 1") self.assertFalse(card2 is card1, "not same instance 2") self.assertFalse(card1 != card3, "2 diff but same value") self.assertFalse(card3 != card1, "opposite of 1==3") self.assertFalse(card1 != card6, "transative test1") self.assertFalse(card6 != card1, "T test2") self.assertFalse(card3 != card6, "T test3") self.assertFalse(card6 != card3, "T test4") self.assertFalse(card1 is card3, "not same 3") self.assertFalse(card1 is card6, "not same 6") # basic same class tests done # test Card class against a Card subclass self.assertFalse(card1 != card4, "child class same value") self.assertFalse(card4 != card1, "opp child class same") self.assertTrue(card1 != card5, "child class diff value") self.assertTrue(card5 != card1, "opp child class diff") self.assertFalse(card1 is card4, "not same 4") self.assertFalse(card1 is card5, "not same 5")
def test_deal(self): '''! test method deal() ''' shoe = Shoe(8) card1 = shoe.deal() # no shuffle so Ac should start us out self.assertEqual("Ac", str(card1), "no shuffle Ac first card") # create a short custom shoe to test running out of cards shoe2 = Shoe([ Card(43), Card(44), Card(45), ]) # only 3 cards in this shoe2 card1 = shoe2.deal() self.assertIsNotNone(card1, "first of shoe2") card1 = shoe2.deal() self.assertIsNotNone(card1, "second of shoe2") card1 = shoe2.deal() self.assertIsNotNone(card1, "third of shoe2") card1 = shoe2.deal() self.assertIsNone(card1, "fourth of shoe2 (empty)")
def test_inherit(self): ''' inheritted things from object: c = playingcards.Card(5,'s') dir(c) ['__delattr__', '__getattribute__', '__setattr__', '__new__', '__dict__', '__dir__', '__format__', '__init_subclass__', '__subclasshook__', '__reduce__', '__reduce_ex__', '__weakref__'] @todo inherit tests ''' # check namespace values c5s = Card(5, 's') class_actual = str(c5s.__class__) self.assertEqual("<class 'pybaccarat.playingcards.Card'>", class_actual, ".class(%s)" % class_actual) module_actual = str(c5s.__module__) self.assertEqual("pybaccarat.playingcards", module_actual, ".module(%s)" % module_actual) docstring = c5s.__doc__ self.assertTrue(isinstance(docstring, str), "docstring is string") self.assertTrue(0 < len(docstring), "some string of >0 exists")
def test_constructor(self): # simple good value test c5s = Card(5, 's') self.assertIsNotNone(c5s, "normal 5s")
def play(self, display=True, show_burn_cards=False, cut_card=-14): '''! Play one game of Baccarat. ''' # start of new shoe procedure self.count_d7 = 0 self.count_p8 = 0 self.__shoe.reset() #self.__shoe.shuffle() self.__shoe.set_cut_card(cut_card) tie_track = Ties() boards = [Scoreboard(0), Scoreboard(1), Scoreboard(2), Scoreboard(3)] rc1 = 0 rc2 = 0 # burn procedure burn = self.__shoe.deal() if display: display_burn = "burn(%s)" % str(burn) burned_cards = [burn] burn = burn.get_rank() if burn > 9: burn = 10 for _ in range(burn): burned_card = self.__shoe.deal() if show_burn_cards: display_burn += " " + str(burned_card) burned_cards.append(burned_card) else: display_burn += " XX" # prepare before playing entire shoe hand_number = 0 last_hand = False bpt = {'B': 0, 'P': 0, 'T': 0} win = 'X' special_JustBoards = False if display: print(display_burn) if self.system_play is not None: special_JustBoards = self.system_play.new_shoe( burned_cards, boards) self.system_play.set_tie_object(tie_track) self.system_play.set_bpt_object(bpt) if special_JustBoards: special_card9s = Card(9, 's') special_cardJh = Card(11, 'h') # while not last_hand: # start of a hand hand_number += 1 self.__player.empty() self.__banker.empty() last_hand = self.__shoe.cut_card_seen() system_hand_output = "" if self.system_play is not None: # print(79 * "=") print(boards[0].print_lines()) print(boards[2].print_lines()) print(str(tie_track)) print(self.system_play.end_shoe()) # system_hand_output = self.system_play.hand_pre() if special_JustBoards: print("***\n*** special_JustBoards\n***") if system_hand_output == "B" or \ system_hand_output == "P" or \ system_hand_output == "T": next_card = self.__shoe._Shoe__next_card print("*** force a %s" % system_hand_output) special_1 = special_cardJh special_2 = special_cardJh if system_hand_output != "B": special_1 = special_card9s if system_hand_output != "P": special_2 = special_card9s self.__shoe._Shoe__cards[next_card + 0] = special_cardJh self.__shoe._Shoe__cards[next_card + 1] = special_cardJh self.__shoe._Shoe__cards[next_card + 2] = special_1 #p2 self.__shoe._Shoe__cards[next_card + 3] = special_2 #b2 elif system_hand_output == "X": print("*** backup") hand_number -= 1 if hand_number < 0: hand_number = 0 print("***win(%s)" % win) if win == 'X': print("can not clear") else: bpt[win] -= 1 if win == 'T': tie_track.remove_last() win = 'X' boards[0].remove_last() h_array = boards[0].get_array() if 1 < len(h_array): #last_col = len(h_array h_a_index = len(h_array) - 1 boards[0].h_array[h_a_index][1] -= 1 for j in range(1, 4): boards[j].remove_last() print("%s" % str(boards[j].get_array())) print("%s" % str(boards[j].get_horiz_count())) continue else: print("*** what is this??? (%s)" % system_hand_output) (win, diff, bonus) = self.play_hand() bpt[win] += 1 tie_track.mark(win) boards[0].mark(win) for j in range(1, 4): if win != "T": boards[j].mark(boards[j].get_cs_mark( boards[0].get_array())) # shoe hand results if self.system_play is not None: system_hand_output += self.system_play.hand_post( win + diff, self.__player, self.__banker) #running counts rc1, rc2 = self.side_count(self.__player, rc1, rc2) rc1, rc2 = self.side_count(self.__banker, rc1, rc2) if display: board0horiz_count = boards[0].get_horiz_count() # peekB = "" if True: board0arr = boards[0].get_array() arrayB = boards[0].get_peek_B_array(board0arr) peekB += boards[1].get_cs_mark(arrayB) peekB += boards[2].get_cs_mark(arrayB) peekB += boards[3].get_cs_mark(arrayB) # flag1 = " " flag2 = " " if (25 + hand_number // 11) < rc1: flag1 = ">" if (32 - 4 * (hand_number // 11)) < rc2: flag2 = "<" print(("%02d P%d%-10s B%d%-10s %s%s %s BPT=%02d-%02d-%02d" + \ " %02d/%02d %s%s%02d,%02d%s %s") % (hand_number, self.__player.value(), str(self.__player), self.__banker.value(), str(self.__banker), win, diff, bonus, bpt['B'], bpt['P'], bpt['T'], board0horiz_count[0], board0horiz_count[1], peekB, flag1, rc1, rc2, flag2, system_hand_output)) # notify the user if self.__shoe.cut_card_seen() and not last_hand: if display: print("last hand of this shoe") # # # end of shoe # if display: print("%-30s D7(%d) P8(%d)" % \ (str(tie_track), self.count_d7, self.count_p8)) print(boards[0].print_lines()) print(boards[2].print_lines()) if self.system_play is not None: print(self.system_play.end_shoe())
def test_constructor(self): ''' test card construction ''' # simple good value test c5s = Card(5, 's') self.assertIsNotNone(c5s, "normal 5s") # bad rank value with self.assertRaises(ValueError): cbad = Card(0, 's') # rank 0 with self.assertRaises(ValueError): cbad = Card(0, 'S') # upper suit with self.assertRaises(ValueError): cbad = Card('s', 5) # swap suit,rank with self.assertRaises(ValueError): cbad = Card(5, 5) # 2 ranks with self.assertRaises(ValueError): cbad = Card('s', 's') # 2 suits with self.assertRaises(ValueError): cbad = Card(5, '') # suit length 0 with self.assertRaises(ValueError): cbad = Card(None, None) # Null cbad = None try: cbad = Card('s', 5) self.fail("s5 should have thrown a ValueError but did not") except ValueError as ex: actual = str(ex) expected = "invalid syntax new_ordinal(s) suit(5)" self.assertEqual(expected, actual, "check msg actual(%s)" % actual) # bad suit value with self.assertRaises(ValueError): cbad = Card(5, 'Clubs') # interate through all legal 52 cards count = 0 for suit in "cdhs": for rank in range(1, 14): count += 1 card = Card(rank, suit) self.assertIsNotNone(card, "52 legeal cards") card = None self.assertEqual(52, count, "make sure we did all 52") # try some illegal ranks illegal = [-1, 0, 14] suit = 's' for rank in illegal: expected = "new_ordinal(%d) not in rank range 1..13" % rank try: cbad = Card(rank, suit) self.fail("bad should have thrown a ValueError but did not") except ValueError as ex: self.assertEqual(expected, str(ex), "check msg") # try the new 3rd constructor syntax c3good = Card("As") self.assertEqual("As", str(c3good), "normal As") c3good = Card("aS") self.assertEqual("As", str(c3good), "odd cases As") # try bad syntax try: c3bad = Card("bad") actual = "" except Exception as ex: actual = str(ex) self.assertEqual("new_ordinal(bad) not legel length 2 string", actual, "msg(%s)" % actual) try: c3bad = Card("cT") actual = "" except Exception as ex: actual = str(ex) self.assertEqual("illegal rank part of new_ordinal(cT)", actual, "msg(%s)" % actual) try: c3bad = Card("TT") actual = "" except Exception as ex: actual = str(ex) self.assertEqual("new_suit(t) not a legal value('cdhs')", actual, "msg(%s)" % actual)
def test_tostring(self): ''' test __str__ method ''' c5s = Card(5, 's') self.assertEqual('5s', str(c5s), "card5s toString")