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)
Beispiel #2
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()
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 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))
Beispiel #6
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 #7
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()
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()
 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 #13
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 #14
0
def main():
    Blackjack()
Beispiel #15
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 #16
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 #17
0
def main():

    blackjackgame = Blackjack()
    blackjackgame.play()
Beispiel #18
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
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)
Beispiel #20
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 #21
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?: ")
    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:
 def reset(self):
     self._blackjack = Blackjack(self._seed)
def main():
    env = Blackjack()
    policy = init_deterministic_policy(env)
    q = policy_iteration(env, policy)
    env.visualize_action_value(q)
Beispiel #25
0
def main():
    env = Blackjack()
    target_policy = init_policy(env)
    behavior_policy = init_equiprobable_random_policy(env)
    q = policy_iteration(env, target_policy, behavior_policy)
    env.visualize_action_value(q)