Beispiel #1
0
def datacoll(num_games):
        
    wins = 0
    pushes = 0
    loses = 0
    blackjack = Blackjack()
        
    Data_coll = []   #initialize the data set for my bayes method  
    for _ in range(num_games):
        finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["start"]) #I can name these output variables differently and create my output data differently
        hit_count = 0
        game_data = [Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),result,hit_count,_+1]
        Data_coll.append(game_data)
        while not finished:
            action = random.choice([Blackjack.ACTION["hit"], Blackjack.ACTION["hold"]])
            finished, result, dealer_cards, player_cards = blackjack.play_game(action)
            if action == Blackjack.ACTION["hit"]:
                hit_count += 1
                Data_coll.append([Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),result,hit_count,_+1])
            else:
                Data_coll.append([Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),result,hit_count,_+1])
        
        wins += 1 if result == Blackjack.RESULT["win"] else 0
        pushes += 1 if result == Blackjack.RESULT["push"] else 0
        loses += 1 if result == Blackjack.RESULT["lose"] else 0
    return (Data_coll)
 def cards_to_state(dealer_cards, player_cards, revealed_cards, only_one_dealer_card=True):
     dealer_cards_interested_in = [dealer_cards[0]] if only_one_dealer_card else dealer_cards
     player_value = Blackjack.get_card_value(player_cards)
     dealer_value = Blackjack.get_card_value(dealer_cards_interested_in)
     assert player_value >= 4
     assert dealer_value >= 2
     return player_value - 1, dealer_value - 1
def main():
    env = Blackjack()
    policy = init_policy(env)
    v = init_state_map(env)
    visits_map = init_state_map(env)
    for _ in xrange(20000):
        episode = generate_episode(env, policy)
        on_policy_state_evaluation(episode, v, visits_map)
    env.visualize_state_value(v)
def main():
    env = Blackjack()
    target_policy = init_policy(env)
    behavior_policy = init_equiprobable_random_policy(env)
    q = init_state_action_map(env)
    c = init_state_action_map(env)
    for _ in xrange(20000):
        episode = generate_episode(env, behavior_policy)
        off_policy_evaluation(episode, q, c, target_policy, behavior_policy)
    env.visualize_action_value(q)
    def cards_to_state(dealer_cards, player_cards, revealed_cards, only_one_dealer_card=True):
        dealer_cards_interested_in = [dealer_cards[0]] if only_one_dealer_card else dealer_cards
        counts = count_cards(revealed_cards)

        s = np.zeros([1, 15])
        s[0, 0] = Blackjack.get_card_value(player_cards)
        s[0, 1] = Blackjack.get_card_value(dealer_cards_interested_in)
        for number in range(13):
            s[0, 2 + number] = counts[number]

        return s
    def test_dealer_deals_cards(self):
        blackjack = Blackjack([Player()])

        blackjack._dealer_deals_cards()

        player_hand = blackjack._holders[0].get_hand()
        dealer_hand = blackjack._holders[1].get_hand()

        self.assertEqual(2, len(player_hand))
        self.assertEqual(2, len(dealer_hand))
        self.assertEqual(48, len(blackjack._deck._cards))
def Off_policy_MC_Control(env,episode_nums,discount_factor=1.0):

    env = Blackjack()
    Q = defaultdict(lambda:np.zeros(env.nA))
    target_policy = defaultdict(float)

    return_count=defaultdict(float)


    for i_episode in range(1,1+episode_nums):
        env._reset()
        state = env.observation()
        episode=[]
        prob_b=[]
        if i_episode % 1000 == 0:
            print("\rEpisode {}/{}.".format(i_episode, episode_nums))
            sys.stdout.flush()
        for i in range(100):

            A = sample_policy(Q,state,env.nA)
            probs = A
            action = np.random.choice(np.arange(env.nA),p=probs)

            next_state,reward,done = env._step(action)
            episode.append((state,action,reward))
            prob_b.append(probs[action])
            if done:
                break
            else:
                state = next_state

        seperate_episode = set([(tuple(x[0]), x[1]) for x in episode])

        G =0.0
        W =1
        prob_b=prob_b[::-1]
        for idx,eps in enumerate(episode[::-1]):
            state,action,reward  = eps
            pair=(state,action)
            G = discount_factor*G+reward
            return_count[pair]+=W
            Q[state][action]+=W*1.0/return_count[pair]*(G-Q[state][action])
            target_policy[state] = np.argmax(Q[state])
            if target_policy[state]!=action:
                break
            W = W*1.0/prob_b[idx]

    return Q
Beispiel #8
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    print("Hello World!")
    deck = Deck()
    deck.shuffle()

    initial_money = UserInputUtils.get_positive_int_with_default(
        600, "How much should be initial money?")
    croupier = APlayer(100000)

    players = [APlayer(initial_money)]

    blackjack = Blackjack(croupier, players, 100)
    winners = blackjack.new_game()
    for winner in winners:
        print("A winner is: \n{}".format(winner))
    def test_finish_round(self):
        player = Player("Jill", 100)
        # (case name, expected prints, player cards, dealer cards)
        cases = [
            (
                "player_loss",
                ["loss"],
                [Card(Card.HEART, Card.TEN), Card(Card.HEART, Card.NINE)],
                [Card(Card.SPADE, Card.TEN), Card(Card.DIAMOND, Card.TEN)]
            ),
            (
                "player_tie",
                ["tie"],
                [Card(Card.HEART, Card.TEN), Card(Card.CLUB, Card.TEN)],
                [Card(Card.SPADE, Card.TEN), Card(Card.DIAMOND, Card.TEN)]
            ),
            (
                "player_win",
                ["win"],
                [Card(Card.SPADE, Card.TEN), Card(Card.DIAMOND, Card.TEN)],
                [Card(Card.HEART, Card.TEN), Card(Card.HEART, Card.NINE)]
            ),
            (
                "player_blackjack_dealer_blackjack",
                ["tie"],
                [Card(Card.HEART, Card.TEN), Card(Card.HEART, Card.ACE)],
                [Card(Card.SPADE, Card.JACK), Card(Card.SPADE, Card.ACE)]
            ),
            (
                "player_blackjack_dealer_non_blackjack",
                ["Blackjack"],
                [Card(Card.HEART, Card.TEN), Card(Card.HEART, Card.ACE)],
                [Card(Card.HEART, Card.TEN), Card(Card.HEART, Card.NINE)]
            ),
        ]
        for case in cases:
            expected_prints = list(reversed(case[1]))
            def test_print(statement):
                self.assertTrue(expected_prints.pop() in statement)
            blackjack = Blackjack([player], test_print)
            blackjack._holders[0].set_hand(case[2])
            blackjack._holders[1].set_hand(case[3])

            blackjack._finish_round()
class BlackjackEnv():
    def __init__(self):
        self._seed = None
        self._blackjack = None

        self.reset()

    def seed(self, seed=None):
        self._seed = seed

    def reset(self):
        self._blackjack = Blackjack(self._seed)

    def deal(self):
        _, _, dealer_cards, player_cards, revealed_cards = self._blackjack.play_game(Blackjack.ACTION["start"])
        return dealer_cards, player_cards, revealed_cards

    def step(self, action):
        assert action in Blackjack.ACTION.values()

        action = Blackjack.ACTION["hold"] if action == 0 else Blackjack.ACTION["hit"]

        finished, result, dealer_cards, player_cards, revealed_cards = self._blackjack.play_game(action)
        reward = BlackjackEnv._calculate_reward(finished, result, player_cards)

        return [dealer_cards, player_cards, revealed_cards], reward, finished, [result]

    @staticmethod
    def _calculate_reward(finished, result, player_cards):
        if finished and result == Blackjack.RESULT["lose"]:
            # Player lost
            return -100
        elif finished and result == Blackjack.RESULT["win"]:
            # Player won
            return 100
        elif finished and result == Blackjack.RESULT["push"]:
            # Push
            return -25
        elif not finished:
            # Still in the middle of a hand
            return 0
Beispiel #11
0
    def blackjackChanceTesting(CI, testIters):
        CCAI_Hand = Hand("CC_AI")
        players = {"CC_AI": CCAI_Hand, "dealer": Dealer_Hand("dealer")}
        blackjack = Blackjack(players)
        CCAI_Interface = Counting_Interface(blackjack, CI, CCAI_Hand)
        # Get the game state then calc chances
        for x in range(testIters):
            print()

            blackjack.display_game()
            gameState = CCAI_Interface.getGameState()

            hand = gameState[0]
            handValue = gameState[1]
            dealer_hand = gameState[2]
            dealerValue = gameState[3]
            AI_Winning = hand == dealer_hand

            CI.decrement_cards(hand, dealer_hand)
            CI.displayCardRecord()
            chances = CI.calcChances(hand, handValue, dealer_hand, dealerValue,
                                     AI_Winning)
            for key in chances.keys():
                print(key, chances[key])
            blackjack.reset()
class BlackjackTest(unittest.TestCase):
    def setUp(self):
        self.game = Blackjack()

    def test_setNumOfDecks(self):
        self.game.setNumOfDecks(3)
        self.assertEquals(3, self.game.getNumOfDecks())

    def test_setNumOfPlayers(self):
        self.game.setNumOfPlayers(3)
        self.assertEquals(3, self.game.getNumOfPlayers())

    def test_removePlayer(self):
        self.assertEquals(False, self.game.removePlayer('XYZ'))
        self.assertEquals(True, self.game.removePlayer('Player1'))
Beispiel #13
0
    def game_start(self):
        print("Welcome to the Game Center!")
        print("Please select a game and type its hotkey to begin:")
        print("Blackjack: <b>")
        game_choice = input()

        while game_choice not in self.gamelist:
            print("This game does not exist, please input a valid hotkey:")
            game_choice = input()

        self.player_list = self.spawn_players(game_choice)

        if game_choice == 'b':
            Blackjack(self.player_list, BlackjackPlayer())
            self.play_again()
 def test_get_player_hit(self):
     player = Player("Jill", 100)
     # (case name, expected prints, expected inputs)
     cases = [
         ("ask_twice_to_stand", ["has score", "has score"], [("Hit or stand", "b"), ("Hit or stand", "n")]),
     ]
     for case in cases:
         expected_prints = list(reversed(case[1]))
         expected_inputs = list(reversed(case[2]))
         def test_print(statement):
             self.assertTrue(expected_prints.pop() in statement)
         def test_input(prompt):
             expected_input = expected_inputs.pop()
             self.assertTrue(expected_input[0] in prompt)
             return expected_input[1]
         blackjack = Blackjack([player], test_print, test_input)
         blackjack._dealer_deals_cards()
         
         blackjack._get_player_hit(player)
    def test_player_makes_bet(self):
        player = Player("Jill", 100)
        # (case name, bet, expected prints, expected inputs)
        cases = [
            (
                "legal_bet",
                [10],
                ["Dealer has", "has hand", "has pot"],
                [("give bet", 10)]
            ),
            (
                "noninteger_bet",
                [10.10, 10],
                ["Dealer has", "has hand", "has pot", "Illegal bet", "Dealer has", "has hand", "has pot"],
                [("give bet", 10.10), ("give bet", 10)]
            ),
            (
                "below_zero_bet",
                [-10, 10],
                ["Dealer has", "has hand", "has pot", "Illegal bet", "Dealer has", "has hand", "has pot"],
                [("give bet", -10), ("give bet", 10)]
            ),
            (
                "over_pot_bet",
                [110, 10],
                ["Dealer has", "has hand", "has pot", "Illegal bet", "Dealer has", "has hand", "has pot"],
                [("give bet", 110), ("give bet", 10)]
            ),
        ]
        for case in cases:
            expected_prints = list(reversed(case[2]))
            expected_inputs = list(reversed(case[3]))
            def test_print(statement):
                self.assertTrue(expected_prints.pop() in statement)
            def test_input(prompt):
                expected_input = expected_inputs.pop()
                self.assertTrue(expected_input[0] in prompt)
                return expected_input[1]
            blackjack = Blackjack([player], test_print, test_input)
            blackjack._dealer_deals_cards()

            blackjack._player_makes_bet(player)
def MC_Control_with_epsilon_greedy(env,episode_nums,discount_factor=1.0, epsilon=0.1):

    env = Blackjack()
    Q = defaultdict(lambda:np.zeros(env.nA))
    return_sum=defaultdict(float)
    return_count=defaultdict(float)

    for i_episode in range(1,1+episode_nums):
        env._reset()
        state = env.observation()
        episode=[]
        if i_episode % 1000 == 0:
            print("\rEpisode {}/{}.".format(i_episode, episode_nums))
            sys.stdout.flush()
        for i in range(100):

            A = epsilon_greedy_policy(Q,state,env.nA,epsilon)

            probs = A
            action = np.random.choice(np.arange(env.nA),p=probs)

            next_state,reward,done = env._step(action)
            episode.append((state,action,reward))
            if done:
                break
            else:
                state = next_state

        seperate_episode = set([(tuple(x[0]), x[1]) for x in episode])

        for state,action in seperate_episode:
            for idx,e in enumerate(episode):
                if e[0]==state and e[1]==action:
                    first_visit_idx = idx
                    break
            pair = (state,action)
            G = sum([e[2]*(discount_factor**i) for i,e in enumerate(episode[first_visit_idx:])])
            return_sum[pair]+=G
            return_count[pair]+=1.0
            Q[state][action]=return_sum[pair]*1.0/return_count[pair]
    return Q
Beispiel #17
0
def main():
    Blackjack()
Beispiel #18
0
def blackjack_manager():
    """Simple game of blackjack"""
    blackjack = Blackjack()
    blackjack.new_game()
    finished_game = False
    while finished_game == False:
        blackjack.print_turn()
        move = click.prompt('Hit(1) or stand?(2)', type=str)
        if move == '1':
            blackjack.player_hit()
        if move == '2':
            blackjack.player_stand()
        if blackjack.winner:
            blackjack.print_turn()
            finished_game = True
Beispiel #19
0
import random
import sys

from Blackjack import Blackjack

num_games = int(float(sys.argv[1]))

wins = 0
pushes = 0
loses = 0

blackjack = Blackjack()
for _ in range(num_games):
    finished, result, dealer_cards, player_cards = blackjack.play_game(
        Blackjack.ACTION["start"])
    while not finished:
        action = random.choice(
            [Blackjack.ACTION["hit"], Blackjack.ACTION["hold"]])
        finished, result, dealer_cards, player_cards = blackjack.play_game(
            action)

    wins += 1 if result == Blackjack.RESULT["win"] else 0
    pushes += 1 if result == Blackjack.RESULT["push"] else 0
    loses += 1 if result == Blackjack.RESULT["lose"] else 0

print("wins:\t{}".format(wins))
print("pushes\t{}".format(pushes))
print("loses:\t{}".format(loses))
Beispiel #20
0
#Main.py
#This is the file used to start the game of Blackjack and debug different classes
from Blackjack import Blackjack
from Card import Card
from Deck import Deck
from Player import Player
p = Player("Dean")
b = Blackjack(p)
inp = "yes"
while "yes" in inp.lower():
	b.play()
	inp = raw_input("Do you want to keep playing?: ")
def main():
    
    blackjackgame = Blackjack()
    blackjackgame.play()
Beispiel #22
0
    for i in range(len(Gamemove)):
        Gamemove[i].pop(0)
    Data =  np.array(Gamemove)
   
    # Initialize our classifier
    mnb = MultinomialNB()

    # Train our classifier
    model = mnb.fit(Data, outcome)
    
    # Playing With NB.Multinomial Classifier
    num_games_test = 1000
    wins = 0
    pushes = 0
    loses = 0
    blackjack = Blackjack()     
    
    for _ in range(num_games_test):
        finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["start"]) #I can name these output variables differently and create my output data differently
        while not finished:
            outc_W_hit =  [Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),1]
            outc_Wout_hit =  [Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),0]
            preds_W_hit = mnb.predict_proba([outc_W_hit])   #order of this output is [-1  0  1] thanks to mnb.classes_ attribute output
            preds_Wout_hit = mnb.predict_proba([outc_Wout_hit])
            if preds_W_hit[0][2] > preds_Wout_hit[0][2]:
                finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["hit"])
            else:
                finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["hold"])
        
        wins += 1 if result == Blackjack.RESULT["win"] else 0
        pushes += 1 if result == Blackjack.RESULT["push"] else 0
Beispiel #23
0
def main():

    blackjackgame = Blackjack()
    blackjackgame.play()
Beispiel #24
0
    def start(self):
        bj = (Blackjack("armond", 500.00))

        bj.renew_deck()
        is_play_again = (True)
        amount = (0)

        while (is_play_again == (True)):
            self.refresh_screen(bj)
            self.is_play_again = (True)

            if (bj.balance < (5)):
                print("\nNo more funds. Thanks for playing!")
                is_play_again = (False)
                break

            self.amount = (input("\n\nWager Amount? (0 to quit): $"))
            self.amount = (self.amount.strip())

            while (match("^([0-9]{1,5})(\.{0,1})([0-9]{0,2})$",
                         str(self.amount)) == (None)):
                self.amount = (input("\nWager Amount? (0 to quit): $"))
                self.amount = (self.amount.strip())

            self.amount = (float(self.amount))

            if (self.amount <= (0.0)):
                is_play_again = (False)
                break

            if (self.amount > (bj.balance)):
                print("\nCannot wager beyond your available balance!")
                sleep(2)
                continue

            bj.balance -= (self.amount)

            self.refresh_screen(bj)

            if (bj.deck_card_count() <= (12)):
                bj.renew_deck()

            bj.reset_hands()

            bj.deal_cards(who="all", amount=2)

            is_done = (False)
            while (is_done == (False)):
                self.refresh_screen(bj)
                action = (bj.perform_action(who=("human")))
                if (action == ('S')):
                    is_done = (True)
                    break
                elif (action == ('H')):
                    bj.deal_cards(who=("human"), amount=(1))
                    count = (bj.calculate_hand("human"))
                    if (count > (21)):
                        action = ('S')
                        is_done = (True)
                        break

            self.refresh_screen(bj)

            if (bj.calculate_hand("human") <= (21)):
                is_done = (False)
                while (is_done == (False)):
                    self.refresh_screen(bj)
                    action = (bj.perform_action(who=("computer")))
                    if (action == ('S')):
                        is_done = (True)
                        break
                    elif (action == ('H')):
                        bj.deal_cards(who=("computer"), amount=(1))
                        count = (bj.calculate_hand("computer"))
                        if (count > (21)):
                            action = ('S')
                            is_done = (True)
                            break

            self.refresh_screen(bj)

            winner = (self.determine_winner(bj))

            print()
            if (self.winner == ("human")):
                print("\nCongratulations. You won ${0:.2f}!".format(
                    self.amount))
                bj.balance += (self.amount * (2))
                bj.my_is_player_lose = (False)
                bj.my_is_player_tie = (False)
            elif (self.winner == ("computer")):
                print("\nSorry, you lose!")
                bj.my_is_player_lose = (True)
                bj.my_is_player_tie = (False)
            elif (self.winner == ("tie")):
                print("Tie game!")
                bj.balance += (self.amount)
                bj.my_is_player_tie = (True)
                bj.my_is_player_lose = (False)
            else:
                raise Exception("Error: Invalid winning scenario!")
            print()

            sleep(3)

            self.refresh_screen(bj)

        print("\nFinal Player Balance: ${0:.2f}\n".format(bj.balance))

        return
Beispiel #25
0
            else:
                state = next_state

        seperate_episode = set([tuple(eps[0]) for eps in episode])

        for s_eps in seperate_episode:
            #find the first visit state

            for i, x in enumerate(episode):
                if x[0] == s_eps:
                    first_visit_pos = i
            G = sum([
                e[2] * discount**idx
                for idx, e in enumerate(episode[first_visit_pos:])
            ])

            return_sum[s_eps] += G
            return_count[s_eps] += 1.0
            V[s_eps] = return_sum[s_eps] * 1.0 / return_count[s_eps]

    return V


env = Blackjack()

V_10k = mc_prediction(sample_policy, env, num_episodes=10000)
plotting.plot_value_function(V_10k, title="10,000 Steps")

V_500k = mc_prediction(sample_policy, env, num_episodes=500000)
plotting.plot_value_function(V_500k, title="500,000 Steps")
Beispiel #26
0
#Main.py
#This is the file used to start the game of Blackjack and debug different classes
from Blackjack import Blackjack
from Card import Card
from Deck import Deck
from Player import Player

p = Player("Dean")
b = Blackjack(p)
inp = "yes"
while "yes" in inp.lower():
    b.play()
    inp = raw_input("Do you want to keep playing?: ")
Beispiel #27
0
from Deck import Deck
from Player import Player
from Blackjack import Blackjack

########################################################################################################################
#Standard Deck
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
rankings = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]  # Ace(Low) -> King
deckStandard = Deck(suits, rankings)

# deckStandard.shuffle()
# deckStandard.show()

p1 = Player("JamFries")
p2 = Player("Guest")

b1 = Blackjack(deckStandard)
b1.startGame([p1, p2])
Beispiel #28
0
    def play_game(self):
        cc = Card_Counter()
        self.blackjack = Blackjack(self.hands)  # local instance of blackjack
        game_ids = []
        move_q = cQ(100)
        cc_q = cQ(100)
        game_q = cQ(100)
        game_id = self.db_wrapper.get_next_game_id()
        continue_playing = True
        while continue_playing:
            while self.blackjack.continue_game:
                turn_num = self.blackjack.turnNumber
                ID_current_player = self.blackjack.get_current_player()
                all_hands = self.blackjack.get_all_hands()
                current_hand = self.hands[ID_current_player]
                hand_val_before = current_hand.get_value()
                next_best_hand = self.get_next_best_hand(
                    ID_current_player, all_hands)

                next_move = self.get_move(
                    ID_current_player)  # pass in all player's hands

                if next_move == Moves.HIT:
                    self.blackjack.hit()
                elif next_move == Moves.STAND:
                    self.blackjack.stand()

                # calculate the required information to the databases and push to the query queues
                hand_val_after = current_hand.get_value()
                move_info = (ID_current_player, game_id, turn_num, next_move,
                             next_best_hand, hand_val_before, hand_val_after)
                move_q.push(move_info)

                chances = cc.calcChances(handValue=hand_val_before,
                                         winning_value=next_best_hand)
                cc_info = (game_id, turn_num, chances["bust"],
                           chances["blackjack"],
                           chances["exceedWinningPlayer"],
                           chances["alreadyExceedingWinningPlayer"], next_move)
                cc_q.push(cc_info)

                if move_q.isFull():
                    self.empty_queue_push(move_q, "move")
                if cc_q.isFull():
                    self.empty_queue_push(cc_q, "cc")

            # PROCESS END OF GAME
            # get the winners, increment their wins, update the agents
            self.blackjack.end_game()
            # push winners to db
            winners = self.blackjack.winners
            winning_hands = []
            for winner_id in winners:
                winning_hands.append(self.hands[winner_id])
            self.display_winners(winning_hands)
            game_info = (game_id, winners, winning_hands,
                         self.blackjack.turnNumber, self.hands)
            game_q.push(game_info)

            if game_q.isFull():
                self.empty_queue_push(game_q, "game")

            # update agents and card counter then reset and increment game_id
            cc.decrement_cards(self.blackjack.new_cards)
            self.blackjack.reset()
            game_ids.append(game_id)
            game_id += 1
            continue_playing = self.get_continue_playing()

        self.empty_queue_push(move_q, "move")
        self.empty_queue_push(game_q, "game")
        self.empty_queue_push(cc_q, "cc")
        return game_ids
Beispiel #29
0
class Console_Env():
    def __init__(self, users):
        self.db_wrapper = CT_Wrapper("DB/Blackjack.sqlite")
        self.usernames = users
        self.hands = self.construct_hands()

    # builds the hands for each user
    def construct_hands(self):
        hands = {}
        for name in self.usernames:
            hands[name] = Hand(name)
        return hands

    # executes teh environemnt
    def play_game(self):
        cc = Card_Counter()
        self.blackjack = Blackjack(self.hands)  # local instance of blackjack
        game_ids = []
        move_q = cQ(100)
        cc_q = cQ(100)
        game_q = cQ(100)
        game_id = self.db_wrapper.get_next_game_id()
        continue_playing = True
        while continue_playing:
            while self.blackjack.continue_game:
                turn_num = self.blackjack.turnNumber
                ID_current_player = self.blackjack.get_current_player()
                all_hands = self.blackjack.get_all_hands()
                current_hand = self.hands[ID_current_player]
                hand_val_before = current_hand.get_value()
                next_best_hand = self.get_next_best_hand(
                    ID_current_player, all_hands)

                next_move = self.get_move(
                    ID_current_player)  # pass in all player's hands

                if next_move == Moves.HIT:
                    self.blackjack.hit()
                elif next_move == Moves.STAND:
                    self.blackjack.stand()

                # calculate the required information to the databases and push to the query queues
                hand_val_after = current_hand.get_value()
                move_info = (ID_current_player, game_id, turn_num, next_move,
                             next_best_hand, hand_val_before, hand_val_after)
                move_q.push(move_info)

                chances = cc.calcChances(handValue=hand_val_before,
                                         winning_value=next_best_hand)
                cc_info = (game_id, turn_num, chances["bust"],
                           chances["blackjack"],
                           chances["exceedWinningPlayer"],
                           chances["alreadyExceedingWinningPlayer"], next_move)
                cc_q.push(cc_info)

                if move_q.isFull():
                    self.empty_queue_push(move_q, "move")
                if cc_q.isFull():
                    self.empty_queue_push(cc_q, "cc")

            # PROCESS END OF GAME
            # get the winners, increment their wins, update the agents
            self.blackjack.end_game()
            # push winners to db
            winners = self.blackjack.winners
            winning_hands = []
            for winner_id in winners:
                winning_hands.append(self.hands[winner_id])
            self.display_winners(winning_hands)
            game_info = (game_id, winners, winning_hands,
                         self.blackjack.turnNumber, self.hands)
            game_q.push(game_info)

            if game_q.isFull():
                self.empty_queue_push(game_q, "game")

            # update agents and card counter then reset and increment game_id
            cc.decrement_cards(self.blackjack.new_cards)
            self.blackjack.reset()
            game_ids.append(game_id)
            game_id += 1
            continue_playing = self.get_continue_playing()

        self.empty_queue_push(move_q, "move")
        self.empty_queue_push(game_q, "game")
        self.empty_queue_push(cc_q, "cc")
        return game_ids

    # gets input from the console to get user's current move
    def get_move(self, currPlayerID):
        self.blackjack.display_game()
        inp = ""
        move = None
        while inp != "h" and inp != "s":
            inp = input("\n" + currPlayerID +
                        ": would you like to hit (h) or (s) ")
            if inp != "h" and inp != "s":
                print("please input h or s")
        print()
        if inp == "h":
            move = Moves.HIT
        elif inp == "s":
            move = Moves.STAND
        return move

    # pass in agent id
    # returns the hand value of the next best agent
    # will return 0 if all other agents are bust
    def get_next_best_hand(self, agent_id, all_hands):
        best_value = 0
        for hand in all_hands:
            if hand.id == agent_id:
                continue
            hand_val = hand.get_value()
            if hand_val > best_value:
                best_value = hand_val
        return best_value

    # method for emptying a db queue and pushing all the queries
    # pass in the queue and a string showing the type of queue "move" or "game"
    def empty_queue_push(self, queue, q_type):
        print("Emptying q: " + q_type)
        if q_type == "move":
            # push all moves to db
            while not queue.isEmpty():
                move_info = queue.pop()
                self.db_wrapper.push_move(agent_id=move_info[0],
                                          game_id=move_info[1],
                                          turn_num=move_info[2],
                                          move=move_info[3],
                                          next_best_val=move_info[4],
                                          hand_val_before=move_info[5],
                                          hand_val_after=move_info[6])
        elif q_type == "game":
            while not queue.isEmpty():
                game_info = queue.pop()
                self.db_wrapper.push_game(game_id=game_info[0],
                                          winners=game_info[1],
                                          winning_hands=game_info[2],
                                          num_of_turns=game_info[3],
                                          agents=game_info[4],
                                          table="users")
        elif q_type == "cc":
            while not queue.isEmpty():
                cc_info = queue.pop()
                self.db_wrapper.push_cc(
                    game_id=cc_info[0],
                    turn_num=cc_info[1],
                    bust=cc_info[2],
                    blackjack=cc_info[3],
                    exceedWinningPlayer=cc_info[4],
                    alreadyExceedingWinningPlayer=cc_info[5],
                    move=cc_info[6])

    # returns true if the user enters that they want to continue playing
    def get_continue_playing(self):
        inp = ""
        res = None
        while inp != "y" and inp != "n":
            inp = input("Would you like play another? (y or n) ")
            if inp != "y" and inp != "n":
                print("please enter y or n")
        if inp == "y":
            res = True
        elif inp == "n":
            res = False
        return res

    # outputs the winners
    def display_winners(self, winning_hands):
        winning_val = winning_hands[0].get_value()
        print("Winners!!", winning_val)
        for hand in winning_hands:
            print(hand.id)
        print()
    print(f"[*] Waiting for connections...")

    while server.in_game is False:
        conn, addr = server.s.accept()
        if conn not in [player._connection for player in server.players]:
            data = conn.recv(1024)
            player = Player(data.decode())
            player.set_connection(conn)
            server.players.append(player)
            server.broadcast(f'[*] {player} has joined the game')
            # Thread(target=client_thread, args=(player,)).start()
            if len(server.players) == player_count:
                server.in_game = True
                print('[*] Game start')

    game = Blackjack(server.players)

    game.deal()

    while server.in_game is True:

        for player in server.players:
            # JSON format to send data => {"text": text, "active": True/False}
            try:
                player._connection.sendall(
                    json.dumps({
                        "text": player.show_cards(),
                        "active": player == game.active_player()
                    }).encode())
            except IndexError:
                if game.winner is None:
        else :
            state = blackjack.stick ()
            #print (state)
            states.append ([1 if state['usable_ace'] > 0 else 0, state['player_total']-1, state['dealer_total']-1])
            
            if state['dealer_total'] > 21 or state['dealer_total'] < state['player_total'] :
                rewards.append (1)
            elif state['dealer_total'] == state['player_total'] :
                rewards.append (0)
            else :
                rewards.append (-1)
            break

    return states, actions, rewards

blackjack = Blackjack ()

average_policy = np.zeros ((2, 21, 11), dtype=np.float)

for k in range (1001) :
    policy = np.ones ((2, 21, 11))
    policy[:, 18:21, :] = 0

    q_values = np.zeros ((2, 21, 11, 2))

    for i in range (5000) :
        initial_state = blackjack.new_state ()
        states, actions, rewards = generate_episode (initial_state, policy)

        G = 0
        for j in range (len(actions)-1, -1, -1) :
def main():
    env = Blackjack()
    policy = init_deterministic_policy(env)
    q = policy_iteration(env, policy)
    env.visualize_action_value(q)
 def reset(self):
     self._blackjack = Blackjack(self._seed)
 def setUp(self):
     self.game = Blackjack()
def main():
    env = Blackjack()
    epsilon = 0.4
    policy = init_epsilon_greedy_policy(env, epsilon)
    q = policy_iteration(env, policy, epsilon)
    env.visualize_action_value(q)