def test_deck_should_be_throw_exception_no_more_cards(self): deck = Deck([]) with pytest.raises(Exception) as ex: deck.put_card() self.assertEqual(str(ex.value), 'No more cards. =(')
def test_new_deck(self): deck = Deck() deck.create() assert len(deck.cards) == 52 assert str( deck ) == "2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As "
def test_shuffles(): Bicycle = Deck() Casino = Deck() Casino.cut() print("Hindu shuffle:") print(Casino)
def test_deck_should_be_3_cards(self): cards = [ Mock(), Mock(), Mock(), ] deck = Deck(cards) self.assertEqual(len(deck.get_cards()), 3)
class BlackJack21(object): def __init__(self, player: Player): _cards = [ Ace(), Two(), Three(), Four(), Five(), Six(), Seven(), Eight(), Nine(), Ten(), K(), Q(), J(), ] self._deck = Deck(_cards) self._player = player self._tapped_cards = [] self._score = 0 def start(self): _loading() print('==== Embaralhando ====') self._deck.shuffle() time.sleep(1.5) _clear() print('==== Mesa pronta, podemos começar ====') while self._deck.has_more_cards(): input('---- Pressione ENTER para virar uma carta jogador {} ----'. format(self._player)) tapped_card = self._deck.put_card() self._tapped_cards.append(tapped_card) print('---- Carta virada ----', tapped_card) print(' Calculando ...') self._calculate_score() if self._is_done(): break print('=== Pontuação {} ===='.format(self._score)) self._finish() def _is_done(self): if self._score <= 21: return False return True def _calculate_score(self): self._score = sum( map(lambda card: card.get_value(), self._tapped_cards)) def _finish(self): _clear() print('==== Acabou o jogo ====') print('==== Calculando ====') _loading() _clear() print('O Resultado é: {}'.format(self._score))
def test_shuffle_strength(): ctr = 0 for i in range(30000): test_deck = Deck() test_deck.riffle() test_deck.riffle() ctr += Deck.shuffle_score(test_deck) ctr /= float(30000) print(ctr)
def __init__(self, num_decks): self.deck = Deck() std_deck = list(self.deck.cards) for i in range(1, num_decks): self.deck.cards += std_deck self.player = Player() self.dealer = Player()
def round(self): self.round_counter += 1 if self.round_counter > 1: input("Press Enter to start next round...") print("Round {} started".format(self.round_counter)) if not self.deck.get_cards(): self.deck = Deck() self.deck.shuffle() self.player.hit(self.deck.deal()) self.dealer.hit(self.deck.deal()) self.print_state() while self.player.playing: if self.player.calc_hand() > 21: print("You have too much :(") self.player.playing = False break if self.player.calc_hand() == 21: print("You have 21!") self.player.playing = False break choice = input("Want to [hit] or [stop]? ") while choice not in {"hit", "stop"}: choice = input("Wrong command. Want to [hit] or [stop]? ") if choice == "hit": self.player.hit(self.deck.deal()) if choice == "stop": self.player.playing = False self.print_state() if not self.player.calc_hand() > 21: while self.dealer.calc_hand() < 17: self.dealer.hit(self.deck.deal()) self.print_state() self.dealer.playing = False winner = self.get_winner() if winner == self.player: self.player.wins += 1 print("Player won!") elif winner == self.dealer: self.dealer.wins += 1 print("Dealer won") else: self.player.wins += 1 self.dealer.wins += 1 print("Push") print("Current win rate is: {:.1%}\n".format(self.player.wins / self.round_counter)) self.reset()
def test_perfect_riffles(): for j in range(53): x = j Montevideo = Deck() for i in range(x): Montevideo.riffle(perfect=True) print("%s perfect riffles:" % x) print(Deck.shuffle_score(Montevideo)) print("\n")
def test_deck_removes_cards_from_its_collection(self): card1 = Card(rank='Ace', suit='Spades') card2 = Card(rank='4', suit='Diamonds') cards = [card1, card2] deck = Deck() deck.add_cards(cards) assert deck.remove_cards(1) == [card1] assert deck.cards == [card2]
def test_deck_should_be_put_a_card(self): cards = [ Mock(), Mock(), Mock(), ] deck = Deck(cards) deck.put_card() self.assertEqual(len(deck.get_cards()), 2)
def test_deck_should_be_shuffle_cards(self, random_mock): cards = [ Mock(), Mock(), Mock(), ] random_mock.shuffle = MagicMock() deck = Deck(cards) deck.shuffle() random_mock.shuffle.assert_called_once_with(cards)
def test_dealt_deck(self): deck = Deck() deck.create() deck.shuffle() assert len(deck.cards) == 52 before_deal = str(deck) deck.deal_card() assert len(deck.cards) == 51 deck.deal_card() assert len(deck.cards) == 50 after_deal = str(deck) assert before_deal[ 6:] == after_deal # The 'top' cards of the deck are being dealt out just like in live play.
def __init__(self, player: Player): _cards = [ Ace(), Two(), Three(), Four(), Five(), Six(), Seven(), Eight(), Nine(), Ten(), K(), Q(), J(), ] self._deck = Deck(_cards) self._player = player self._tapped_cards = [] self._score = 0
def play_hand(self, feedback_file_name, action): date_time = datetime.datetime.now().strftime("%m-%d-%y %H:%M:%S") deck = Deck() deck.create() deck.shuffle() hand = Hand() hand.get_hand(deck) hand.order_hand() hand_type = hand.hand_type() position = Position() position.current_position(self.num_players) position_min = position.min_open() r = Range(self.hand_range) correct_decision, hand_percent, total_cards = r.correct_decision( hand_type, position_min) min_open_hand = r.min_open_card(position_min) decision = Decision(action).decision() if self.show_feedback: if decision == correct_decision: feedback = "Correct" # report feedback self.score += 1 else: feedback = "Incorrect" # report feedback feedback_file_name.save_hand(self.hand_num, date_time, self.hand_range, feedback, position, position_min, min_open_hand, hand, hand_type, hand_percent, decision, correct_decision, self.score) self.hand_num += 1 else: self.hand_num += 1 return self.hand_num
def test_desk_shuffles_cards(self, mock_shuffle): deck = Deck() cards = [ Card(rank='7', suit='Diamonds'), Card(rank='3', suit='Spades') ] deck.add_cards(cards) deck.shuffle() assert mock_shuffle.called_once_with(cards)
def test_shuffle_deck(self): unshuffled_deck = "2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As " deck = Deck() deck.create() assert str(deck) == unshuffled_deck deck.shuffle() assert str( deck ) != unshuffled_deck # There is a VERY SMALL chance the shuffled deck will still be in the same order as above after the shuffle; but I wouldn't bet on it. assert len(deck.cards) == 52
def test_hand_has_2_cards(self): from app.deck import Deck deck = Deck() deck.create() deck.shuffle() before_deal = str(deck) hand = Hand() hand.get_hand(deck) card1, card2 = hand.hole_cards assert str(card1) == before_deal[:2] assert str(card2) == before_deal[3:5] assert str(deck) == before_deal[6:]
def test_new_hand(self): """The two starting cards for each player consist of their pre-flop hand. Also, called the hole cards.""" from app.deck import Deck deck = Deck() deck.create() deck.shuffle() before_deal = str(deck) hand = Hand() hand.get_hand(deck) assert str( hand ) == before_deal[: 6] # See the test_deck module for an explanation of the number of characters.
def test_dealt_card(self): deck = Deck() deck.create() deck.shuffle() assert len(deck.cards) == 52 before_deal = str(deck) top_card = deck.deal_card() assert len(deck.cards) == 51 assert len( str(top_card) ) == 2 # The top card trims off the trailing whitspace (Technically, doesn't really trim. The space was added to make it easier in the str method for development and manual testing). assert str( top_card ) == before_deal[: 2] # The first 3 characters of the random deck match the first 2 dealt for the top card + the space. assert before_deal[3:] == str( deck ) # The rest of the deck is still in its random shuffled order.
def shuffle_comparison(): start_time = datetime.datetime.now() print("Trivial:") print(Deck.variation_distance(shuffler_trivial)) print("Single:") print(Deck.variation_distance(shuffler_riffle)) print("Double:") print(Deck.variation_distance(shuffler_double_riffle)) for i in range(3, 9): print("%d:" % i) print( Deck.variation_distance(lambda deck: shuffler_n_riffle(deck, n=i))) print("Python.shuffle:") print(Deck.variation_distance(shuffler_shuffle)) print("Casino:") print(Deck.variation_distance(shuffler_casino)) end_time = datetime.datetime.now() diff_time = end_time - start_time print("Time it took: %d" % diff_time.seconds)
class Game: round_counter = 0 def __init__(self, num_decks): self.deck = Deck() std_deck = list(self.deck.cards) for i in range(1, num_decks): self.deck.cards += std_deck self.player = Player() self.dealer = Player() def round(self): self.round_counter += 1 if self.round_counter > 1: input("Press Enter to start next round...") print("Round {} started".format(self.round_counter)) if not self.deck.get_cards(): self.deck = Deck() self.deck.shuffle() self.player.hit(self.deck.deal()) self.dealer.hit(self.deck.deal()) self.print_state() while self.player.playing: if self.player.calc_hand() > 21: print("You have too much :(") self.player.playing = False break if self.player.calc_hand() == 21: print("You have 21!") self.player.playing = False break choice = input("Want to [hit] or [stop]? ") while choice not in {"hit", "stop"}: choice = input("Wrong command. Want to [hit] or [stop]? ") if choice == "hit": self.player.hit(self.deck.deal()) if choice == "stop": self.player.playing = False self.print_state() if not self.player.calc_hand() > 21: while self.dealer.calc_hand() < 17: self.dealer.hit(self.deck.deal()) self.print_state() self.dealer.playing = False winner = self.get_winner() if winner == self.player: self.player.wins += 1 print("Player won!") elif winner == self.dealer: self.dealer.wins += 1 print("Dealer won") else: self.player.wins += 1 self.dealer.wins += 1 print("Push") print("Current win rate is: {:.1%}\n".format(self.player.wins / self.round_counter)) self.reset() def reset(self): self.player.playing = True self.dealer.playing = True self.player.reset_hand() self.dealer.reset_hand() def print_state(self): print("Player's hand is:") self.player.print_hand() print("Dealer's hand is:") self.dealer.print_hand() print("Actual score is: {}:{}\n".format(self.player.calc_hand(), self.dealer.calc_hand())) def get_winner(self): if (self.player.calc_hand() > self.dealer.calc_hand() or self.dealer.calc_hand() > 21)\ and not self.player.calc_hand() > 21: return self.player elif self.player.calc_hand() < self.dealer.calc_hand( ) or self.player.calc_hand() > 21: return self.dealer else: return None
def test_one_one(seed, expected_shuffled_deck): routine(Deck(), seed, Deck.DistributionStrategy.OneOne, list(expected_shuffled_deck[i::4] for i in range(4)))
def test_one_three(seed, expected_shuffled_deck): routine(Deck(), seed, Deck.DistributionStrategy.OneThree, list(expected_shuffled_deck[i:i + 13] for i in range(0, 52, 13)))
def play_hand(self, feedback_file_name): date_time = datetime.datetime.now().strftime("%m-%d-%y %H:%M:%S") print("Hand number: " + str(self.hand_num)) deck = Deck() deck.create() deck.shuffle() hand = Hand() hand.get_hand(deck) print(hand) hand.order_hand() hand_type = hand.hand_type() position = Position() position.current_position(self.num_players) print(position) position_min = position.min_open() r = Range(self.hand_range) correct_decision, hand_percent, total_cards = r.correct_decision( hand_type, position_min) # Leaving the following several print statements for reference in case someone else isn't that familiar with hand range calculations. # print(correct_decision) # print(hand_percent) # print(total_cards) # To confirm the hand percentage is correct: total_cards / 1326 = hand_percent min_open_hand = r.min_open_card(position_min) # print(min_open_hand) action = int(input("What is your decision? (4=Open, 6=Fold): ") ) # For faster keyboard input decision = Decision(action).decision() if decision != "stop": if self.show_feedback: if decision == correct_decision: # screen feedback print( "Good job! You should", correct_decision, "because you want to play the top {0:.2f}".format( position_min * 100) + "% of your range; which ends at " + str(min_open_hand) + ".\n" + str(hand) + "is in the top {0:.2f}".format(hand_percent * 100) + "% of starting hands for the " + self.hand_range + " range.") feedback = "Correct" # report feedback self.score += 1 else: print( "Sorry, you should", correct_decision, "because you want to play the top {0:.2f}".format( position_min * 100) + "% of your range; which ends at " + str(min_open_hand) + ".\n" + str(hand) + "is in the top {0:.2f}".format(hand_percent * 100) + "% of starting hands for the " + self.hand_range + " range.") feedback = "Incorrect" # report feedback feedback_file_name.save_hand(self.hand_num, date_time, self.hand_range, feedback, position, position_min, min_open_hand, hand, hand_type, hand_percent, decision, correct_decision, self.score) self.hand_num += 1 print("Score: " + str(self.score) + "\n") else: self.hand_num += 1 print() else: print("Thanks for playing.") return action
from app.card import Card from app.deck import Deck from app.hand import Hand from app.player import Player from app.game import Game # creates a deck of 52 cards deck = Deck() cards = Card.create_deck_with_52_cards() deck.add_cards(cards) # mock game with two players hand1 = Hand(cards=[]) hand2 = Hand(cards=[]) player1 = Player(name='M', hand=hand1) player2 = Player(name='Z', hand=hand2) players = [player1, player2] # game.play() shuffles deck and deals two cards to x num of players game = Game(deck=deck, players=players) game.play() # # # creates initial three card draw # # table_hand = [] # # table_count = 0 # # for card in deck.cards: # # if table_count < 3: # # table_hand.append(card) # # deck.remove_cards(card) # # table_count += 1
def test_deck(): Bicycles = Deck() assert (Bicycles.validate()) some_card = Bicycles.deal() assert (not Bicycles.validate()) Bicycles.collect(some_card) assert (Bicycles.validate()) Bicycles.collect(some_card) assert (not Bicycles.validate()) print("Test deck passed!")
def test_five_four_four(seed, expected_shuffled_deck): routine( Deck(), seed, Deck.DistributionStrategy.FiveFourFour, list(expected_shuffled_deck[i * 5:(i + 1) * 5] + expected_shuffled_deck[20 + i * 4:24 + i * 4] + expected_shuffled_deck[36 + i * 4:40 + i * 4] for i in range(4)))
def test_cards(): Bicycles = Deck() for card in Bicycles.cards: print(card.id())
def __init__(self): self.deck = Deck() self.players = []