Esempio n. 1
0
def q_learning_decision(player_cards, dealer_cards):
    if not Qsa:
        # read all files
        with open(SUMMARY_FNAME, 'rb') as r_csvfile:
            reader = csv.reader(r_csvfile)
            for row in reader:
                s1, s2, a = row
                Qsa[(eval(s1),eval(s2))] = eval(a)
    s1, s2 = get_sum(player_cards), get_sum(dealer_cards)
    return Qsa[(s1, s2)]
Esempio n. 2
0
def q_learning_decision(player_cards, dealer_cards):
    if not Qsa:
        # read all files
        with open(SUMMARY_FNAME, 'rb') as r_csvfile:
            reader = csv.reader(r_csvfile)
            for row in reader:
                s1, s2, a = row
                Qsa[(eval(s1), eval(s2))] = eval(a)
    s1, s2 = get_sum(player_cards), get_sum(dealer_cards)
    return Qsa[(s1, s2)]
Esempio n. 3
0
def play_round(way_to_decision):
    # Initialize a game
    player_cards, dealer_cards = [], []
    while True:
        new_card = get_card()
        if new_card[0] == RED:
            continue
        else:
            break
    player_cards.append(new_card)
    while True:
        new_card = get_card()
        if new_card[0] == RED:
            continue
        else:
            break 
    dealer_cards.append(new_card)
    #
    rv_player = None
    while True:
        if DISPLAY:
            print "Player's turn"
            display_current(player_cards, dealer_cards)
        player_choice = way_to_decision(player_cards, dealer_cards)
        if player_choice == HIT:
            nc = get_card()
            if DISPLAY:
                print 'New card is (%s, %d)' %('Red' if nc[0]==RED else 'Black', nc[1])
            player_cards.append(nc)
            s = get_sum(player_cards) 
            if s < 1  or s > 21:
                rv_player = BUST
                break
            if s == 21:
                return WIN
        else:
            assert player_choice == STICK, player_choice 
            rv_player = STICK
            break
    #
    if rv_player != BUST:
        # the player did not go bust
        rv_dealer = None
        while True:
            if DISPLAY:
                print "Dealer's turn"
                display_current(player_cards, dealer_cards)
            s = get_sum(dealer_cards)
            if s < 1  or s > 21:
                rv_dealer = BUST
                break
            elif s >= 17:
                rv_dealer = STICK
                break
            if s == 21:
                return LOSE
            else:
                # HIT
                nc = get_card()
                if DISPLAY:
                    print 'New card is (%s, %d)' %('Red' if nc[0]==RED else 'Black', nc[1])
                dealer_cards.append(nc)
        if rv_dealer == BUST:
            return WIN
    player_sum, dealer_sum = get_sum(player_cards), get_sum(dealer_cards)
    if player_sum == dealer_sum:
        return DRAW
    return WIN if player_sum > dealer_sum else LOSE
Esempio n. 4
0
def play_round(way_to_decision):
    # Initialize a game
    player_cards, dealer_cards = [], []
    while True:
        new_card = get_card()
        if new_card[0] == RED:
            continue
        else:
            break
    player_cards.append(new_card)
    while True:
        new_card = get_card()
        if new_card[0] == RED:
            continue
        else:
            break
    dealer_cards.append(new_card)
    #
    rv_player = None
    while True:
        if DISPLAY:
            print "Player's turn"
            display_current(player_cards, dealer_cards)
        player_choice = way_to_decision(player_cards, dealer_cards)
        if player_choice == HIT:
            nc = get_card()
            if DISPLAY:
                print 'New card is (%s, %d)' % ('Red' if nc[0] == RED else
                                                'Black', nc[1])
            player_cards.append(nc)
            s = get_sum(player_cards)
            if s < 1 or s > 21:
                rv_player = BUST
                break
            if s == 21:
                return WIN
        else:
            assert player_choice == STICK, player_choice
            rv_player = STICK
            break
    #
    if rv_player != BUST:
        # the player did not go bust
        rv_dealer = None
        while True:
            if DISPLAY:
                print "Dealer's turn"
                display_current(player_cards, dealer_cards)
            s = get_sum(dealer_cards)
            if s < 1 or s > 21:
                rv_dealer = BUST
                break
            elif s >= 17:
                rv_dealer = STICK
                break
            if s == 21:
                return LOSE
            else:
                # HIT
                nc = get_card()
                if DISPLAY:
                    print 'New card is (%s, %d)' % ('Red' if nc[0] == RED else
                                                    'Black', nc[1])
                dealer_cards.append(nc)
        if rv_dealer == BUST:
            return WIN
    player_sum, dealer_sum = get_sum(player_cards), get_sum(dealer_cards)
    if player_sum == dealer_sum:
        return DRAW
    return WIN if player_sum > dealer_sum else LOSE