Exemple #1
0
def main():
    # name variables
    deck, value = Deck(), Value()
    shuffled_deck = deck.shuffle_deck()
    spacer = '--------------------------'

    print (spacer)
    print ("BLACKJACK!!!")
    print (spacer)
    bank = eval(input('how much do you want to play with: '))
    print (spacer)
    P1 = Player('P1', bank)
    dealer = Player('dealer')
    print ('You now have %s to bet' %(P1.bank))
    print (spacer)


    answer = ['Y', 'y', 'n', 'N'] # input bank
    keep_playing = True
    a = 'Y'
    # does user want to play again
    while keep_playing == True:
        if a not in answer: # did not select Y/N
            print ('that answer is not an option...')
            a = input('do you want to keep playing? (Y/N) ') # ask once more
            continue
        else:
            if a == 'N' or a == 'n':
                keep_playing = False
                break # cut to game
        # start game
        if P1.bank == 0:
            P1.bank = int(input('you ran out of money, enter more money: '))
        bet = int(input('enter your bet: '))
        print (P1.add_bet(bet))
        print ()
        # pass cards to dealer & player(s)
        P1.hand.append(deck.deal_card())
        dealer.hand.append(deck.deal_card())
        P1.hand.append(deck.deal_card())
        dealer.hand.append(deck.deal_card())
        # show your hand & dealer's last hand
        print ('your current hand:')
        print (spacer)
        print (P1.show_hand())
        print ('value of the hand is: %s' % (value.actual_value(P1.hand)))
        print (spacer)
        print ('dealer\'s hand: %s'% (dealer.hand[-1]))
        print (spacer)
        print ('card count: %s' % (c(deck.history)))
        bj = False
        if value.actual_value(P1.hand) != 21:
            choose = input('Do you want to hit or stand? (H/S): ')
        else:
            bj = True
        # plauer's turn
        while ((choose == 'H') and not bj) or ((choose == 'h') and not bj):
            P1.hand.append(deck.deal_card())
            print (spacer)
            print ('you drew a %s. Hand value: %s' % (P1.hand[-1], value.actual_value(P1.hand)))
            print (spacer)
            print ('Current Hand:')
            print (P1.show_hand())
            if value.actual_value(P1.hand) == 0:
                print ('You busted')
                break
            print ('card count: %s' % (c(deck.history)))
            choose = input('Do you want to hit or stand? (H/S): ')
        # Dealer's turn
        print ('Dealer\'s turn')
        print (spacer)

        print ('dealer\'s hand: ')
        print (dealer.show_hand())
        print ('dealer\'s current value is: %s' % (value.actual_value(dealer.hand) ))
        while value.actual_value(dealer.hand) < 17 and value.actual_value(P1.hand) != 0: # soft 17
            dealer.hand.append(deck.deal_card())
            print ('dealer drew a %s' % (dealer.hand[-1]))
            print (spacer)
            # did dealer break?
            if value.hand_value(dealer.hand) != 0:
                print ('dealer value: %s' % (value.actual_value(dealer.hand)))
            if value.hand_value(dealer.hand) == 0:
                print ('dealer busted')
                break
        # reveal winner and give earnings
        print ('Your hand value: %s || Dealer\'s hand value: %s' % (value.actual_value(P1.hand), value.actual_value(dealer.hand)))
        if value.actual_value(P1.hand) > value.actual_value(dealer.hand):
            P1.win() # add winnings to bank
            if value.hand_value(dealer.hand) == 0:
                print ('Dealer busted')
            print ('You win!')
            print ('you now have %s dollars total' %(P1.bank))
        elif value.hand_value(P1.hand) == value.hand_value(dealer.hand):
            print ('Tie.')
        else:
            P1.lose() # subtract bet from P1 bank
            print ('Dealer wins')
            print ('you now have %s dollars total' %(P1.bank))
        print ('end of the round')
        print ()
        # clear old hands
        dealer.new_hand()
        P1.new_hand()
        a = input('do you want to keep playing? (Y/N) ')
        bj = False
        if len(deck.history) < 26:
            deck._deck = deck.shuffle_deck()
Exemple #2
0
def doit(epochs, showFrequency):
    # Create backProp
    backprop1 = backProp(2, 4, 2, 0.05)
    backprop2 = backProp(2, 10, 2, 0.07)

    # BP1
    num_right = 0  # Number of guesses right
    num_wrong = 0  # Number of guesses wrong

    # BP2
    num_wrong2 = 0  # Number of guesses right
    num_right2 = 0  # Number of guesses wrong

    # PRINT INITIAL STATE
    print(
        "\n______________________INITIAL STATE OF NETWORK________________________"
    )
    print("INITIAL STATE FOR 2 CARDS\n")
    # Print initial state of backProp
    print_initial_state(backprop1)
    print()  # Adds a blank line

    print("INITIAL STATE FOR 3 CARDS\n")
    # Print initial state of backProp
    print_initial_state(backprop2)
    print()  # Adds a blank line

    # Loop through the epochs
    for i in range(1, epochs + 1):
        player_cards = []  # list of Player cards
        player_total = None  # player total value
        dealer_cards = []  # list of Dealer cards
        dealer_total = None  # dealer total value

        guess1 = None  # a guess
        confidence = None  # confidence in guess
        percent = None  # percentage right

        # Two inputs: Player's total, Dealer's total
        inputs1 = []  # BP1

        # Initialize deck
        deck = Deck()  # BP1 and BP2 share this deck

        # Shuffle deck
        deck.shuffle_deck()

        # Player draws 2 cards
        playerC1 = deck.deck.pop(0)  # 51 cards left in deck
        playerC2 = deck.deck.pop(0)  # 50 cards left in deck
        # Add to list of Player cards
        player_cards.append(playerC1)
        player_cards.append(playerC2)
        # Adding up the total points for the player
        player_total = playerC1.value + playerC2.value
        # Converting the total (2 to 21) to a value between 0 to 1
        inputs1.append((player_total - 2) / 19.0)

        # Dealer draws 1 card
        dealerC1 = deck.deck.pop(0)  # 49 cards left in deck
        # Add to list of Dealer cards
        dealer_cards.append(dealerC1)
        # Adding up the total points for the dealer
        dealer_total = dealerC1.value
        # Converting the total (1 to 10) to a value between 0 to 1
        inputs1.append((dealer_total - 1) / 9.0)

        # P's 1st card; P's 2nd card; D's 1st card; # of times
        desired_output1 = runSimulation1(deck, playerC1, playerC2, dealerC1,
                                         50, i)  # Returns 0 - draw or 1 - hold

        # Get a guess and the confidence
        (guess1, confidence) = predictBP(backprop1, inputs1)

        # Update right/wrong counter
        if guess1 == desired_output1:
            num_right += 1
        else:
            num_wrong += 1

        # For printing; did we hold or draw?
        desired_line = "draw" if (desired_output1 == 0) else "hold"
        guess_line = "draw" if (guess1 == 0) else "hold"

        # Comes after runSimulation because we use the desired_output to calculate other stuff
        # Print first 10 epochs & then every value of showFrequency thereafter
        if (i <= 5 or ((i % showFrequency) == 0)):
            percent = (100.0 * num_right) / (num_right + num_wrong)
            print(
                "%d.  (%s %s - % s) -> %s with conf=%.5f desired=%s right=%.2f%s\t BP1"
                % (i, playerC1.name, playerC2.name, dealerC1.name, guess_line,
                   confidence, desired_line, percent, "%"))

            # This prints the results of predictBP if epochs is <=5 or the last one
            '''
            if(i <= 5 or (i == epochs)):
                print()
                print_prediction(backprop1, inputs1)
                print()
            '''

        # Adjust the weights for BP1
        adjustWeights(backprop1, inputs1, desired_output1)

        # If the guess is to draw, invoke BP2
        if guess1 is 0:

            # Two inputs: Player's total, Dealer's total
            inputs2 = []  # Special for BP2

            # Player draws 1 more card, the 3rd card
            playerC3 = deck.deck.pop(0)  # at least 4 less cards left in deck
            # Add to list of Player cards
            player_cards.append(playerC3)
            # Adding up the total points for the player
            player_total = playerC1.value + playerC2.value + playerC3.value

            if (player_total < 21):

                # Converting the total (2 to 21) to a value between 0 to 1
                inputs2.append((player_total - 1) / 19.0)

                # Converting the total (1 to 10) to a value between 0 to 1
                inputs2.append((dealer_total - 1) / 9.0)

                # P's 1st card; P's 2nd card; D's 1st card; # of times
                # Special for BP2
                desired_output2 = runSimulation2(
                    deck, playerC1, playerC2, playerC3, dealerC1, 50,
                    i)  # Returns 0 - draw or 1 - hold

                # Get a guess and the confidence
                (guess2, confidence) = predictBP(backprop2, inputs2)

                # Update right/wrong counter for BP2
                if guess2 == desired_output2:
                    num_right2 += 1
                else:
                    num_wrong2 += 1

                # For printing; did we hold or draw?
                desired_line = "draw" if (desired_output2 == 0) else "hold"
                guess_line = "draw" if (guess2 == 0) else "hold"

                # Comes after runSimulation because we use the desired_output to calculate other stuff
                # Print first 10 epochs & then every value of showFrequency thereafter
                # Special for BP2, three cards displayed now
                if (i <= 5 or ((i % showFrequency) == 0)):
                    percent2 = (100.0 * num_right2) / (num_right2 + num_wrong2)
                    print(
                        "%d.  (%s %s %s - % s) -> %s with conf=%.5f desired=%s right=%.2f%s\t BP2\n"
                        % (i, playerC1.name, playerC2.name, playerC3.name,
                           dealerC1.name, guess_line, confidence, desired_line,
                           percent2, "%"))

                    # This prints the results of predictBP if epochs is <=5 or the last one
                    '''
                    if (i <= 5 or (i == epochs)):
                        print()
                        print_prediction(backprop2, inputs2)
                        print()
                    '''

                # Adjust the weights for BP1
                adjustWeights(backprop2, inputs2, desired_output2)

    # PRINT FINAL NETWORK #
    print(
        "______________________FINAL STATE OF THE NETWORK________________________"
    )
    print("FINAL STATE FOR 2 CARDS\n")
    # Print initial state of backProp
    print_initial_state(backprop1)
    print()  # Adds a blank line

    print("FINAL STATE FOR 3 CARDS\n")
    # Print initial state of backProp
    print_initial_state(backprop2)
    print()  # Adds a blank line
Exemple #3
0
    for i in backprop.get_biasTop():
        print("%8s " % (str(i)), end="")


##############START################

deck = Deck()

#print(deck)
#deck.shuffle_deck()

#shuffledDeck = deck.get_deck()

print("Five random cards")
for i in range(0, 10):
    deck.shuffle_deck()
    print("%d) %s" % (i + 1, deck.get_deck()[0]))

print("")

#print(decktemp[1])
#print(deck.get_deck()[1].get_name())
#print("%c" % (deck.get_deck()[0].get_name()[0]))

playerC1 = deck.get_deck()[0].get_name()
playerC2 = deck.get_deck()[1].get_name()
dealerC1 = deck.get_deck()[2].get_name()
print(playerC1, playerC2, dealerC1)

runSimulation()