Exemple #1
0
def test_init(verbose):
    """Test initializer of blackjack objects.
    Extra info printed if `verbose` is True. """

    tester_name = inspect.stack()[0][3]
    print("Running " + tester_name + "()")

    # Small test
    c1 = c11.Card(0, 12)
    c2 = c11.Card(1, 10)
    c3 = c11.Card(2, 9)
    c4 = c11.Card(0, 1)
    start_deck = [c1, c2, c3, c4]
    if verbose:
        print("testing start deck of " + c11.cardlist_str(start_deck))

    game = blackjack.Blackjack(start_deck)
    ca.assert_equals(set([c1, c2]), set(game.playerHand))  # Sets ignore order
    ca.assert_equals([c3], game.dealerHand)
    ca.assert_equals([c4], start_deck)  # Check that cards were removed
    ca.assert_equals(start_deck, game.deck)

    # Bigger test.
    start_deck = c11.full_deck()
    if verbose:
        print("testing start deck of " + c11.cardlist_str(start_deck))
    game = blackjack.Blackjack(start_deck)
    ca.assert_true(
        set([c11.Card(0, 1), c11.Card(0, 2)]) == set(game.playerHand))
    ca.assert_equals([c11.Card(0, 3)], game.dealerHand)
    # Check card removal
    ca.assert_equals(c11.full_deck()[3:], start_deck)
    ca.assert_equals(start_deck, game.deck)

    print_done_message(verbose, tester_name)
def evaluate_policy(total_episodes = 50000):
    '''Evalutes the policy by playing episodes of blackjack'''
    # Initialize variable to hold all states with returns and times visited
    all_states_visited = {}
    
    # Perform a high number of episodes
    for episode in range(total_episodes):
        # Initialize game
        env = blackjack.Blackjack(verbose=False)
        new_state, reward = env.make_step(action="reset")
        end_state = np.array([-1, -1, -1])
        episode_visited_states = []

        # Play one episode
        while not np.array_equal(new_state, end_state):
            episode_visited_states.append((new_state[0], new_state[1], new_state[2]))
            player_sum = new_state[0]
            action = policy(player_sum)
            new_state, reward = env.make_step(action)

        # Store states and returns for episode
        for state in episode_visited_states:
            if state in all_states_visited: 
                old_reward, old_visits = all_states_visited[state]
                new_reward = old_reward + reward
                new_visits = old_visits + 1
                all_states_visited[state] = (new_reward, new_visits)
            else:
                all_states_visited[state] = (reward, 1)
        
    # Update V values
    for state, value in all_states_visited.items():
        total, visited = value
        average_return = total / visited
        v[state] = average_return
Exemple #3
0
def make_dummy_game():
    """Returns a dummy blackjack game."""
    return blackjack.Blackjack(
        [c11.Card(alt='QC'),
         c11.Card(alt='TD'),
         c11.Card(alt='9H')])
Exemple #4
0
for suit in ['S', 'C', 'H', 'D']:  # spades, clubs, hearts, diamonds
    for num in range(2, 11):
        card_images[f"{num}{suit}"] = pygame.image.load(
            f'./card_images/{num}{suit}.png')
    for face in ['J', 'Q', 'K', 'A']:  # jack, queen, king, ace
        card_images[f"{face}{suit}"] = pygame.image.load(
            f'./card_images/{face}{suit}.png')

button_deal = button((0, 255, 255), 800, 600, 50, 50, 'Deal')
button_hit = button((0, 255, 0), 200, 600, 50, 50, 'Hit')
button_stand = button((255, 0, 0), 275, 600, 50, 50, 'Stand')
button_play_again = button((200, 200, 200), 800, 500, 100, 50, 'Play Again')

game_over = False
while not game_over:
    game = blackjack.Blackjack()
    game.shuffle()

    has_dealt = False
    players_turn = True
    running = True
    done = False

    while running:
        screen.blit(background, (0, 0))

        if not has_dealt:
            button_deal.draw(screen, (0, 0, 0))

        if has_dealt:
            draw_player_cards(screen, game)
Exemple #5
0
#!/usr/bin/env python3
"""
Blackjack test.
"""

import blackjack

playerChoice = input("How many players would you like? (Max 6)\n-->")

if playerChoice.isnumeric():
    if int(playerChoice) < 6 and int(playerChoice) > 0:
        myBlackJack = blackjack.Blackjack(int(playerChoice))
        print("// -------------------------------")
        myBlackJack.gameloop()
    else:
        print("Number must be: below 6 and above 0")
else:
    print("Amount of players must be numeric.")
Exemple #6
0
# Window size
width: int = 1280
height: int = 720
white = (255, 255, 255)
black = (0, 0, 0)
brown = (80, 50, 25)

pygame.init()

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Casino")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 40)
font2 = pygame.font.Font(None, 80)
font3 = pygame.font.Font(None, 60)
b1 = bk.Blackjack(2)
P_bust = False
P_bust2 = False


class Button:
    def __init__(self, x1, y1, w, h, func, name, func_param=None, image=None):
        self.x1 = x1
        self.w = w
        self.y1 = y1
        self.h = h
        self.image = image
        self.func = func
        self.func_param = func_param
        self.button_name = name
Exemple #7
0
def clear():
    if platform.system() == "Windows":
        os.system("cls")
    else:
        os.system("clear")


print('''
♠♣BLACKJACK♥♦ v. 1.0
By Jake Henderson
''')
while True:
    try:
        score = int(input("Enter starting score for players: "))
        game = blackjack.Blackjack(score)
        break
    except ValueError as e:
        print("Please enter a valid number.")

while True:
    player_name = input("Enter player name: ")
    if not player_name:
        if len(game.get_players()) == 1:
            print("Please enter at least one player.")
        else:
            break
    else:
        if player_name[-3:] == " -r":
            game.add_player(player_name[:-3], score, True)
        else:
Exemple #8
0
def main():
    game = b.Blackjack(2, 6, 50)
    score = DataFrame(game.play_game()) 
    score.to_csv('FinalScores.csv')

    return
Exemple #9
0
#!/usr/bin/env python3
"""
Blackjack test.
"""

import blackjack

myBlackJack = blackjack.Blackjack(3)
print("// -------------------------------")
myBlackJack.gameloop()