def simulate(individual, betAmount): # status = -1 = dealer win ; 0 = push (equal valued decks) ; 1 = player win. dDeck = bjs.dealerInitial() # Give dealer two cards. botChoice = 1 bDeck = [] bjs.hit(bDeck) # While botChoice = 1 (hit) while botChoice == 1: # Give a card bjs.hit(bDeck) # Check for soft aces. bDeck = bjs.checkAce(bDeck) # Check if bust if bjs.bust(bDeck): # If player busts status = -1 return status, betAmount # Check for 21 if bjs.checkBlackjack(dDeck): break # Make a new choice with the new deck. botChoice = makeDecision(individual, bDeck, dDeck) # If the player has a blackjack if bjs.checkBlackjack(bDeck): # If the dealer also has a blackjack. if bjs.checkBlackjack(dDeck): status = 0 return status, betAmount # If the dealer doesn't have blackjack. else: status = 1 betAmount *= 3 return status, betAmount # If the dealer has a blackjack elif bjs.checkBlackjack(dDeck): status = -1 return status, betAmount # If stand if botChoice == 0: bjs.dealerAfter(dDeck) dw, pw = bjs.compare(bDeck, dDeck) if dw == 1 and pw == 0: status = -1 return status, betAmount elif dw == 0 and pw == 1: status = 1 return status, betAmount elif dw == 0 and pw == 0: status = 0 return status, betAmount # If double down elif botChoice == 2: # Double the betting money. betAmount *= 2 # Give bot a single random card from the deck. bDeck = bjs.hit(bDeck) # Check if bust. if bjs.bust(bDeck): status = -1 return status, betAmount # Compare to dealer dw, pw = bjs.compare(bDeck, dDeck) if dw == 1 and pw == 0: status = -1 return status, betAmount elif dw == 0 and pw == 1: status = 1 return status, betAmount elif dw == 0 and pw == 0: status = 0 return status, betAmount # If split elif botChoice == 3: # Create two separate decks. bDeck1 = [bDeck[0]] # Deck 1 bDeck2 = [bDeck[1]] # Deck 2 bDecks = [bDeck1, bDeck2] # For each deck, player win, equal or dealer win. deckStatus = [ 0, 0 ] # -1 = dealer win ; 0 = push (equal valued decks) ; 1 = player win. # Play with both hands deckNumber = -1 # Double the bet betAmount *= 2 for deck in bDecks: deckNumber += 1 botChoice = 1 # While botChoice = 1 (hit) while botChoice == 1: # Give a card bjs.hit(deck) # Check for soft aces. deck = bjs.checkAce(deck) # Check if bust if bjs.bust(deck): deckStatus[deckNumber] = -1 break # Check for 21 if bjs.checkBlackjack(deck): break # Make a new choice with the new deck. botChoice = makeDecision(individual, deck, dDeck) # If the player has a blackjack if bjs.checkBlackjack(deck): # If the dealer also has a blackjack. if bjs.checkBlackjack(dDeck): deckStatus[deckNumber] = 0 # If the dealer doesn't have blackjack. else: deckStatus[deckNumber] = 1 # If the dealer has a blackjack elif bjs.checkBlackjack(dDeck): deckStatus[deckNumber] = -1 # If stand if botChoice == 0: bjs.dealerAfter(dDeck) dw, pw = bjs.compare(deck, dDeck) if dw == 1 and pw == 0: deckStatus[deckNumber] = -1 elif dw == 0 and pw == 1: deckStatus[deckNumber] = 1 elif dw == 0 and pw == 0: deckStatus[deckNumber] = 0 # If double down elif botChoice == 2: # Double the betting money. betAmount *= 2 # Give bot a single random card from the deck. deck = bjs.hit(deck) # Check if bust. if bjs.bust(deck): deckStatus[deckNumber] = -1 # Compare to dealer dw, pw = bjs.compare(deck, dDeck) if dw == 1 and pw == 0: deckStatus[deckNumber] = -1 elif dw == 0 and pw == 1: deckStatus[deckNumber] = 1 elif dw == 0 and pw == 0: deckStatus[deckNumber] = 0 return deckStatus, betAmount
def player(bet, dDeck): print("====================================================") print("Dealer:", dDeck[0], "\n") pDeck = [] pDecision = "hit" drawCard(pDeck) # While decision is to hit. while pDecision == "hit": drawCard(pDeck) # Check for aces pDeck = bjs.checkAce(pDeck) if bjs.checkBlackjack(pDeck): bet *= 3 return pDeck, bet print("You:", end=' ') for card in pDeck: print(card, end=' ') print("") print("Value:", bjs.value(pDeck), "\n") pDecision = getDecision(pDeck) if pDecision == "double down": bet *= 2 drawCard(pDeck) print("You:", end=' ') for card in pDeck: print(card, end=' ') print("") print("Value:", bjs.value(pDeck), "\n") pDecision = "stand" if pDecision == "stand" or bjs.bust(pDeck): print("You:", end=' ') for card in pDeck: print(card, end=' ') print("") print("Value:", bjs.value(pDeck), "\n") return pDeck, bet elif pDecision == "split": bet *= 2 pDeck1 = [pDeck[0]] pDeck2 = [pDeck[1]] pDecks = [pDeck1, pDeck2] deckCounter = -1 for deck in pDecks: deckCounter += 1 pDecision = "hit" while pDecision == "hit": drawCard(deck) deck = bjs.checkAce(deck) print("You:", end=' ') for card in deck: print(card, end=' ') print("") print("Value:", bjs.value(deck)) pDecision = getDecision(deck) if pDecision == "double down": bet *= 2 drawCard(pDeck) pDecision = "stand" if pDecision == "stand" or bjs.bust(pDeck): break return pDecks, bet
def test_bust_False( self): # Value lower than 22 should NOT result in a bust deck = ["10", "7", "4"] self.assertEqual(bs.value(deck), 21) self.assertFalse(bs.bust(deck))
def botPlay(botStrat, dDeck): print("====================================================") print("Dealer:", dDeck[0], "\n") botChoice = "hit" bDeck = [] drawCard(bDeck) # While bot decides to hit while botChoice == "hit": drawCard(bDeck) bDeck = bjs.checkAce(bDeck) if bjs.checkBlackjack(bDeck): return bDeck print("Bot:", end=' ') for card in bDeck: print(card, end=' ') print("") print("Value:", bjs.value(bDeck), "\n") botDecision = ga.makeDecision(botStrat, bDeck, dDeck) if botDecision == 0: botChoice = "stand" print("Bot stands\n") elif botDecision == 1: botChoice = "hit" print("Bot hits\n") elif botDecision == 2: botChoice = "double down" print("Bot doubles down\n") elif botDecision == 3: botChoice = "split" print("Bot splits\n") if botChoice == "double down": drawCard(bDeck) botChoice = "stand" print("Bot:", end=' ') for card in bDeck: print(card, end=' ') print("") print("Value:", bjs.value(bDeck), "\n") if botChoice == "stand" or bjs.bust(bDeck): print("Bot:", end=' ') for card in bDeck: print(card, end=' ') print("") print("Value:", bjs.value(bDeck), "\n") return bDeck elif botChoice == "split": bDeck1 = [bDeck[0]] bDeck2 = [bDeck[1]] bDecks = [bDeck1, bDeck2] deckCounter = -1 for deck in bDecks: deckCounter += 1 botChoice = "hit" while botChoice == "hit": drawCard(deck) deck = bjs.checkAce(deck) print("Bot:", end=' ') for card in deck: print(card, end=' ') print("") print("Value:", bjs.value(deck), "\n") botDecision = ga.makeDecision(botStrat, deck, dDeck) if botDecision == 0: botChoice = "stand" print("Bot stands\n") elif botDecision == 1: botChoice = "hit" print("Bot hits\n") elif botDecision == 2: botChoice = "double down" print("Bot doubles down\n") elif botDecision == 3: botChoice = "split" print("Bot splits\n") if botChoice == "double down": drawCard(bDeck) botChoice = "stand" if botChoice == "stand" or bjs.bust(bDeck): break return bDecks
def test_bust_True(self): # Value higher than 21 should result in a bust deck = ["10", "7", "5"] self.assertEqual(bs.value(deck), 22) self.assertTrue(bs.bust(deck))