def test_Deck_4_25_2(self): deck = Deck(4, 25, 2) deck.num_decks = 2 deck.num_players = 4 pyramid_poker_hands = deck.deal() self.assertEqual(len(pyramid_poker_hands), 4) self.assertEqual(len(pyramid_poker_hands[0]), 25) self.assertEqual(len(deck.deck), 106)
def create_card_images(): """create all card images as a dictionary card_name:image_object""" # toplevel = tk.Tk() image_dir = "Cards_gif/" deck = Deck(6, 25, 3) card_image_dict = {} for card in list(deck.deck): # all images have filenames the match the card_list names + extension .gif card_only = card[0:2] card_image_path = image_dir + card_only + ".gif" # Uses the right path for Card.gif irrespective of where script is run path_parent = os.path.dirname(os.getcwd()) os.chdir(path_parent + "/src/") card_image_dict[card] = tk.PhotoImage(file=card_image_path) logging.debug("{} {}".format(card, card_image_dict[card])) card_image_dict["Deck3"] = tk.PhotoImage(file=image_dir + "Deck3" + ".gif") return card_image_dict
from src.Deck import * from src.LegalPokerHands import * pyramid_poker_list = Deck(6, 25, 3).deal()[0] # pyramid_poker_list =['S2-', 'D2-', 'D3*', 'H5+', 'D4+', 'S4*', 'H8+', 'S9+', 'C7-', 'H7*', 'D9*', 'S7-', 'H5*', 'D7-', 'HA*', 'D6*', 'SA-', 'H4*', 'D3+', 'CT+', 'ST*', 'C7*', 'H4-', 'HQ-', 'C5-'] # pyramid_poker_list =['CT-', 'H3*', 'S9+', 'D7-', 'D4-', 'CJ+', 'H5-', 'S8+', 'CK-', 'HT-', 'SQ+', 'D9+', 'H2+', 'H4*', 'H8-', 'SA-', 'H5+', 'H9+', 'SQ*', 'SJ-', 'SQ-', 'C4-', 'C2-', 'DQ*', 'H6-'] # pyramid_poker_list = \ # ['DA+', 'DA*', 'CA-', 'HK+', 'HQ+', 'DQ+', 'HJ-', 'HJ*', 'ST*', 'HT*', 'DT-', 'CT-', 'S9+', 'H9*', 'H8*', 'D8-', 'C8*', 'D7*', 'D7-', 'D6*', 'H5-', 'D5-', 'C5-', 'H4*', 'C3*'] pyramid_poker_list = sorted(pyramid_poker_list, key=rank_sort, reverse=True) print(pyramid_poker_list) my_hand = LegalPokerHands(pyramid_poker_list) hand6 = list(my_hand.hand6) hand5 = list(my_hand.hand5) hand4 = list(my_hand.hand4) hand3 = list(my_hand.hand3) hand2 = list(my_hand.hand2) hand1 = list(my_hand.hand1) hand6_count = len(hand6) hand5_count = len(hand5) hand4_count = len(hand4) hand3_count = len(hand3) hand2_count = len(hand2) hand1_count = len(hand1) # print ("hand6") # for i in range(hand6_count): # print (hand6[i])
def ActualGame(): p1_score = 0 p2_score = 0 p3_score = 0 p4_score = 0 rounds = 13 def display(player): for i in range(len(player)): print(player[i].name) Players = int(input("How many players do you want to play with?")) print("Okay, we will play with " + str(Players) + " players.") for i in range(13): p1_sets_made = 0 p2_sets_made = 0 p3_sets_made = 0 p4_sets_made = 0 deck = Deck() deck.shuffle() # Check if total number of players playing is 3. if int(Players) == 3: # Split deck into 3 seperate players. player1 = deck.cards[0:rounds] player2 = deck.cards[rounds:(rounds * 2)] player3 = deck.cards[(rounds * 2):(rounds * 3)] # Check if total number of players playing is 4. elif int(Players) == 4: # Split deck into 4 seperate players. player1 = deck.cards[0:rounds] player2 = deck.cards[rounds:(rounds * 2)] player3 = deck.cards[(rounds * 2):(rounds * 3)] player4 = deck.cards[(rounds * 3):(rounds * 4)] if Players == 3: trump = ['spades', 'hearts', 'clubs', 'diamonds'] deck.trump_suit = trump[i % 4] print("The trump suit is " + deck.trump_suit + ".") print("everyone except player 1 look away, you have 5 seconds") time.sleep(5) print('Player 1, this is your hand.') display(player1) player1_sets_called = int( input("how many sets are you going to make?\n>") ) #implement click event thing for i in range(50): print(" ") print("everyone except player 2 look away, you have 5 seconds") print("player 1 called " + str(player1_sets_called) + ". How many will you make?") time.sleep(5) print('Player 2, this is your hand.') display(player2) player2_sets_called = int( input("how many sets are you going to make?\n>") ) #implement click event thing for i in range(50): print(" ") print("everyone except player 3 look away, you have 5 seconds") print("player 1 called " + str(player1_sets_called) + ".") print("player 2 called " + str(player2_sets_called) + ". How many will you make?") time.sleep(5) print('Player 3, this is your hand.') display(player3) player3_sets_called = int( input("how many sets are you going to make?\n>") ) #implement click event thing for i in range(50): print(" ") print(" everyone look away while player 1 plays his card, ") time.sleep(3) print('Player 1, this is your hand.') display(player1) player1_play_card = input("what card do you want to play?") if player1_play_card in player1: print("\n" * 50) print(player1_play_card) time.sleep(3) print(" everyone look away while player 2 plays his card, ") time.sleep(3) print('Player 1 played ' + str(player1_play_card) + ".") print('Player 2, this is your hand.') display(player2) player2_play_card = input("what card do you want to play?") if player2_play_card in player2: print("\n" * 50) print(player2_play_card) time.sleep(3) print(" everyone look away while player 3 plays his card, ") time.sleep(3) print('Player 1 played ' + str(player1_play_card) + ".") print('Player 2 played ' + str(player2_play_card) + ".") print('Player 3, this is your hand.') display(player3) player3_play_card = input("what card do you want to play?") if player3_play_card in player3: print("\n" * 50) print(player3_play_card) time.sleep(3) rounds = rounds - 1
def test_Deck_6_25_3(self): deck = Deck(6, 25, 3) pyramid_poker_hands = deck.deal() self.assertEqual(len(pyramid_poker_hands), 6) self.assertEqual(len(pyramid_poker_hands[0]), 25) self.assertEqual(len(deck.deck), 159)
def test_Deck_7_25_1(self): deck = Deck(7, 25, 3) pyramid_poker_hands = deck.deal() self.assertEqual(len(pyramid_poker_hands), 7) self.assertEqual(len(pyramid_poker_hands[0]), 25) self.assertEqual(len(deck.deck), 212)
xstart = xloc index = 0 for cardlist in self.cardlists: xloc = round(xloc, 2) # cardlist[0] starts at 0,0 cardlist[1] starts at 4,0, cardlist[2] 8,0 for card in cardlist: # print (card, xloc, yloc) if index <= 5: w2.create_image(xloc, yloc, image=image_dict[card], anchor="nw", tags=("card")) xloc = round((xloc + self.CARD_WIDTH), 2) if xloc - xstart >= 3 * X_GAP_factor * self.CARD_WIDTH: xloc = xstart yloc = round((yloc + self.CARD_HEIGHT), 2) index = index + 1 # now we need to make sure x is +4, and y is 0 xloc = xstart + 4 * self.CARD_WIDTH + X_GAP_factor xstart = round(xloc, 2) yloc = self.INITIAL_Y w2.pack() w2.mainloop() test = test_create_card_images() # create object test.cardlists = Deck( 6, 25, 3).deal() # set attribute test_folder.cardlist to first hand test.display_cardlists()
from src.Deck import * from src.BestHand25Wild import * total_elapse = 0 for i in range (1): card_list = Deck(6, 25, 3).deal()[0] card_list2 = list(card_list[0:25]) # deal number_of_cards # card_list2 = ['SA*', 'SA-', 'HA+', 'HA*', 'CA+', 'CA-', 'CK+', 'HQ+', 'DQ-', 'DQ+', 'DQ*', 'CQ-', 'DJ-', 'HT-', 'S9+', 'H8+', # 'S7-', 'H7+', 'H7-', 'D7+', 'D7-', 'H6+', 'H5+', 'D3*', 'S2*'] # card_list2 = ['WW-', 'SA-', 'SQ-', 'SJ-', 'S9+', 'S7-', 'S6-', 'S3-', 'HJ+', 'HT*', 'H8+', 'H6*', 'H3+', 'DK+', 'DK*', 'D6*', 'D5-', 'D4*', 'D2-', 'CA*', 'CA-', 'CQ-', 'CQ+', 'CJ+', 'C2+'] # ['WW*', 'DA+', 'DA*', 'SK*', 'HK*', 'DK+', 'DQ-', 'SJ-', 'ST+', 'ST*', 'DT*', 'H9+', 'S8-', 'H8*', 'H8+', 'D8-', # card_list2 = ['SJ-', 'DA-', 'SK-', 'HK-', 'SQ+', 'CQ-', 'DJ-', 'HT+', 'HT*', 'DT+', 'ST*', 'S9+', 'S8+', # 'H6', 'S6*', 'D6-', 'C8', 'C8-', 'D5+', 'D5+', 'H5+', 'H5+', 'S5+','S5-'] # 'C8+', 'S7-', 'H6+', 'S5+', 'C4*', 'C3+', 'S2-', 'H2-', 'C2*'] # Example to pick the best kickers for 4 four of a kinds - 4A, 4Q, 4J # card_list2 = ['S4-', 'SA-', 'SQ-', 'SJ-', 'SJ+', 'SQ-', 'SA-', 'ST-', 'HJ+', 'HT*', 'H8+', 'H6*', 'H3+', 'DK+', 'DK*', 'D6*', 'D5-', 'D4*', 'D2-', 'CA*', 'CA-', 'CQ-', 'CQ+', 'CJ+', 'C2+'] # Example to pick the best kickers for 4 four of a kinds - 4J, 4 5's, 4 2's # card_list2 = ['S4-', 'S5-', 'S2-', 'SJ-', 'SJ+', 'S2-', 'S5-', 'ST-', 'HJ+', 'HT*', 'H8+', 'H6*', 'H3+', 'DK+', 'DK*', 'D6*', 'D5-', 'D4*', 'D2-', 'C5*', 'CA-', 'CQ-', 'CQ+', 'CJ+', 'C2+'] # Example to pick the best kickers for 4 four of a kinds - 4 J's, 4 5's, 4 2's - change CA to C6 - then added a WW # card_list2 = ['S4-', 'S5-', 'S2-', 'SJ-', 'SJ+', 'S2-', 'S5-', 'ST-', 'HJ+', 'HT*', 'H8+', 'WW*', 'H3+', 'DK+', 'DK*', # 'D6*', 'D5-', 'D4*', 'D2-', 'C5*', 'C6-', 'CQ-', 'CQ+', 'CJ+', 'C2+'] card_list2 = ["SA+", "DA+", "DA-", "CA+", "C8+", "HJ+", "CJ-", "CJ+", "SJ+", "H9+", "CQ+", "DT+", "HT+", "HT-", "ST+", "D9+", "S5+", "H5+", "D5+", "C5+", "D7+", "D7-", "S6+", "HK+", "C6+"] card_list2 = sorted(card_list2, key=suit_rank_sort, reverse=True)
from src.Deck import * deck = Deck(6, 25, 3) pyramid_poker_list = deck.deal()[0] print (pyramid_poker_list) pyramid_poker_string = '.'.join(pyramid_poker_list) print (pyramid_poker_string) print (pyramid_poker_string.split("."))
from src.Analysis import * from src.Deck import * from varname import nameof pyramid_poker_list = Deck(6, 25, 3).deal()[0] # pyramid_poker_list =['S2-', 'D2-', 'D3*', 'H5+', 'D4+', 'S4*', 'H8+', 'S9+', 'C7-', 'H7*', 'D9*', 'S7-', 'H5*', 'D7-', 'HA*', 'D6*', 'SA-', 'H4*', 'D3+', 'CT+', 'ST*', 'C7*', 'H4-', 'HQ-', 'C5-'] # pyramid_poker_list =['CT-', 'H3*', 'S9+', 'D7-', 'D4-', 'CJ+', 'H5-', 'S8+', 'CK-', 'HT-', 'SQ+', 'D9+', 'H2+', 'H4*', 'H8-', 'SA-', 'H5+', 'H9+', 'SQ*', 'SJ-', 'SQ-', 'C4-', 'C2-', 'DQ*', 'H6-'] # pyramid_poker_list = \ # ['DA+', 'DA*', 'CA-', 'HK+', 'HQ+', 'DQ+', 'HJ-', 'HJ*', 'ST*', 'HT*', 'DT-', 'CT-', 'S9+', 'H9*', 'H8*', 'D8-', 'C8*', 'D7*', 'D7-', 'D6*', 'H5-', 'D5-', 'C5-', 'H4*', 'C3*'] print(pyramid_poker_list.sort(key=rank_sort)) analysis = Analysis(pyramid_poker_list) def print_var(variable): print(nameof(variable), variable) for i in range(0, 51): print('{:<2} {:<18} {:<5} {}'.format(i, analysis.desc[i], len(analysis.suit_rank_array[i]), analysis.suit_rank_array[i]))
hand_separation = 1.04 X_GAP_factor = .6 print (self.cardlist) self.CARD_WIDTH = 72 * X_GAP_factor self.CARD_HEIGHT= 95 self.CANVAS_W = 10 * self.CARD_WIDTH self.CANVAS_H = 7 * self.CARD_HEIGHT self.INITIAL_X = 0 self.INITIAL_Y = 0 self.card_image_dict = create_card_images() w2 = tk.Canvas(window1, height=7 * 100, width=15 * 70, bg="light blue", relief="raised") i = 0 j = 0 for card in self.cardlist: w2.create_image(70 * i + 35, 100 * j + 50, image=self.card_image_dict[card]) i += 1 if i > 12: i = 0 j = j + 1 w2.pack() w2.mainloop() test = test_create_card_images() # create object test.cardlist = Deck(1,52,1).deck test.display_cardlist()
from copy import deepcopy from src import Card from src import Deck from src import Evaluator from src import hand_range_to_cards evaluator = Evaluator() original_deck = Deck() hands = hand_range_to_cards('random') i = 0 for hand in hands: i += 1 deck = deepcopy(original_deck) deck.draw_card(hand[0]) deck.draw_card(hand[1]) board = [deck.draw_rank('A'), deck.draw_rank('6'), deck.draw_rank('2')] print evaluator.evaluate(hand, board), Card.print_pretty_cards(board + hand) print i
def monte_carlo_control(episodes): EPISODES = episodes deck = Deck.Deck() GAMES = [] MC = RL.Monte_Carlo_Control() policy = RL.Epsilon_Greedy() for k in range(EPISODES): player = Agent.Player(deck) dealer = Agent.Dealer(deck) game = State.Game() player.draw_black() dealer.draw_black() state = State.State(player, dealer) game.add_state(state) # state.print_state() # Player 's turn while policy.action(state, k + 1, MC) == Agent.HIT: player.draw() game.add_action(Agent.HIT) if player.score > 21 or player.score < 1: # Lose game.add_reward(-1) game.terminated = True MC.update(game) break game.add_reward(0) state = state.next_state(player, dealer) game.add_state(state) if not game.terminated: game.add_action(Agent.STICK) # Dealer 's turn only if game is not over if not game.terminated: while dealer.score < 17: dealer.draw() if dealer.score > 21 or dealer.score < 1: # Win game.add_reward(1) game.terminated = True MC.update(game) break # Compare the scores if not terminated if not game.terminated: if player.score > dealer.score: # print("Player scores more Win !") game.add_reward(1) elif player.score < dealer.score: # print("Dealer scores more Lose !") game.add_reward(-1) else: # print("Draw !") game.add_reward(0) game.terminated = True MC.update(game) GAMES.append(game) # player.print_holds() # print() # dealer.print_holds() # print("This game is over !") print("Training Over !") return MC.Q
def sarsa_lambda_control(episodes, lamb): EPISODES = episodes deck = Deck.Deck() sarsa = RL.Sarsa_Lambda(lamb, 1) policy = RL.Epsilon_Greedy() for k in range(EPISODES): terminated = False player = Agent.Player(deck) dealer = Agent.Dealer(deck) sarsa.init_eligibility_trace() player.draw_black() dealer.draw_black() state = State.State(player, dealer) # state.print_state() action = policy.action(state, k + 1, sarsa) while action == Agent.HIT: player.draw() sarsa.add_trace_N(state, Agent.HIT) if player.score > 21 or player.score < 1: # Lose reward = -1 delta = reward + 0 - sarsa.Q_S_A(state, Agent.HIT) sarsa.update(delta) terminated = True # print("Player BUST Lose !") break state_next = state.next_state(player, dealer) action_next = policy.action(state_next, k + 1, sarsa) delta = 0 + sarsa.gamma * sarsa.Q_S_A( state_next, action_next) - sarsa.Q_S_A(state, Agent.HIT) sarsa.update(delta) state = state_next action = action_next if not terminated: sarsa.add_trace_N(state, Agent.STICK) while dealer.score < 17: dealer.draw() if dealer.score > 21 or dealer.score < 1: # Win reward = 1 delta = reward + 0 - sarsa.Q_S_A(state, Agent.STICK) sarsa.update(delta) terminated = True # print("Dealer BUST Win !") break if not terminated: if player.score > dealer.score: # print("Player scores more Win !") reward = 1 delta = reward + 0 - sarsa.Q_S_A(state, Agent.STICK) sarsa.update(delta) terminated = True elif player.score < dealer.score: # print("Dealer scores more Lose !") reward = -1 delta = reward + 0 - sarsa.Q_S_A(state, Agent.STICK) sarsa.update(delta) terminated = True else: # print("Draw !") reward = 0 delta = reward + 0 - sarsa.Q_S_A(state, Agent.STICK) sarsa.update(delta) terminated = True # player.print_holds() # print() # dealer.print_holds() # print("This game is over !") print("Training Over !") return sarsa.Q