from poker import * #---------------------- TESTS ---------------------- def test_card_1(): # Card class card1 = Card('9', 'Spades') card2 = Card('Q', 'Hearts') card3 = Card('9', 'Hearts') card4 = Card('K', 'Diamonds') assert str(card1) == '9-Spades' assert str(card2) == 'Q-Hearts' print(card1, card2, card3, card4) def test_deck_1(): # Deck class deck = Deck() print('--------- Before Shuffle --------') print(deck) deck.shuffle() print('--------- After Shuffle --------') print(deck) def test_hand_1(): # Hand class deck = Deck() deck.shuffle() cards = deck.draw_cards(3) h = Hand(cards) newcards = deck.draw_cards(2) h.add(newcards) print(h) def get_random_hand(deck): "Get random hand of 5 cards from deck of cards" cards = deck.draw_cards(5) h = Hand(cards) return h def test_hand_2(): # Hand class, hand type for i in range(4000): deck = Deck() deck.shuffle() h = get_random_hand(deck) type, a, b = h.type() if type == 'flush': print("Have flush!") print(h) print("") elif type == 'four of a kind': print("Have four of a kind! rank=%s fifth_card = %s" % (a,str(b))) print(h) print("") elif type == 'three of a kind': c4,c5 = b print("Have three of a kind! rank=%s two_card = %s, %s" % (a, str(c4), str(c5))) print(h) print("") def test_player_1(): # Player class p = Player('Alice', 400, strategy=strategy1) # defined in poker.py deck = Deck() deck.shuffle() h = get_random_hand(deck) p.hand = h stage = 3 bets = (1, 1, 0, 0, 2) # six players (with Alice) print(p.hand.type()) print(p.strategy(stage, p.hand, bets)) def test_game_1(): a = Player('Alice', 100, strategy1) b = Player('Bob', 200, strategy1) c = Player('Clod', 100, strategy1) d = Player('Dian', 250, strategy1) print("Players:", a.name, b.name, c.name, d.name) players = [a, b, c, d] g = Game(players) g.stage1() g.stage2() for p in players: print(p.name, p.hand) def test_game_2(): a = Player('Alice', 100, strategy1) b = Player('Bob', 200, strategy1) c = Player('Clod', 100, strategy1) d = Player('Dian', 250, strategy1) print("Players:", a.name, b.name, c.name, d.name) players = [a, b, c, d] g = Game(players) g.stage1() g.stage2() g.stage3() g.stage4() for p in players: print(p.name, p.hand, p.budget) def test_game_3(): a = Player('Alice', 100, strategy1) b = Player('Bob', 200, strategy1) c = Player('Clod', 100, strategy1) d = Player('Dian', 250, strategy1) print("Players:", a.name, b.name, c.name, d.name) players = [a, b, c, d] g = Game(players) g.run() print(g.log) def test_game_4(): a = Player('Alice', 5000, strategy1) b = Player('Bob', 5000, strategy2) c = Player('Clod', 5000, strategy3) for i in range(1000): g = Game([a, b, c]) g.run() print(a.name, a.budget) print(b.name, b.budget) print(c.name, c.budget) def test_game_5(num_games=1000, budget=500): import matplotlib.pyplot as plt a = Player('Alice', budget, strategy1) b = Player('Bob', budget, strategy2) c = Player('Clod', budget, strategy3) a_init_budget = a.budget b_init_budget = b.budget c_init_budget = c.budget a_budgets = [] b_budgets = [] c_budgets = [] for i in range(num_games): g = Game([a, b, c]) g.run() a_budgets.append(a.budget) b_budgets.append(b.budget) c_budgets.append(c.budget) plt.subplot(311) plt.plot(range(num_games), a_budgets) plt.grid(True) plt.title("Player=%s, %s, budget=%d" % (a.name, a.strategy.__name__, a_init_budget)) plt.xlabel('Games') plt.ylabel('Budget') plt.subplot(312) plt.plot(range(num_games), b_budgets) plt.grid(True) plt.title("Player=%s, %s, budget=%d" % (b.name, 'strategy2', b_init_budget)) plt.xlabel('Games') plt.ylabel('Budget') plt.subplot(313) plt.plot(range(num_games), c_budgets) plt.grid(True) plt.title("Player=%s, %s, budget=%d" % (c.name, c.strategy.__name__, c_init_budget)) plt.xlabel('Games') plt.ylabel('Budget') plt.tight_layout() plt.show() if __name__ == '__main__': #test_card() #test_deck1() #test_hand_2() test_player_1()