Quantcast
Channel: Blackjack casino game - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 3

Blackjack casino game

$
0
0

Blackjack (twenty-one) is a casino game played with cards. The goal of the game to draw cards that total as close to 21 points as possible without going over. All face cards count as 10 points, aces count as 1 or 11, and all other cards count their numeric value.

The game is played against a dealer. The player tries to get closer to 21 (without going over) than the dealer. If the dealer busts (goes over 21) the player automatically wins (provided the player had not already busted). The dealer must always take cards according to a fixed set of rules. The dealer takes cards until he or she achieves a total of at least 17. If the dealer's hand contains an ace, it will be counted as 11 when that results in a total between 17 and 21 inclusive; otherwise, the ace is counted as 1.

Write a program that simulates multiple games of blackjack and estimates the probability that the dealer will bust. Hints: treat the deck of cards as infinite. You do not need to keep track of the cards in the hand, just the total so far (treating an ace as 1) and a bool variable hasace that tells whether or not the hand contains an ace. A hand containing an ace should have 10 points added to the total exactly when doing so would produce a stopping total(something between 17 and 21 inclusive).

It runs, but I'm just looking to improve it if possible.

def printIntro():    print("This program simulates a bunch of blackjack games.")def getInputs():    while True:        n = eval(input("How many games should be simulated? "))        if n <= 0:            print("You must enter a number greater than zero!")        else:            break    return ndef simOneGame():    score = 0    blacklist = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]    while not gameOver(score):        blackscore = blacklist[randrange(len(blacklist))]        score = score + blackscore        if score >= 17:              break        if blackscore == 11 and (score >= 6 and score <= 10):             score = score + 11        else:            score = score + 1    return scoredef simNGames(n):    holds = 0    busts = 0    for i in range(n):        score = simOneGame()        if score >= 22:            busts = busts + 1        else:            holds = holds + 1    return holds, bustsdef gameOver(score):    return score >= 22 or score >= 17def printSummary(holds, busts):    n = holds + busts    print("Number of holds and busts: Holds: {0:0} Busts: {1:0}".format(holds, busts))    print("Percentage of holds and busts: Holds: {0:0.1%}".format(holds / n), end = "")    print("Busts: {0:0.1%}".format(busts / n))def main():    printIntro()    n = getInputs()    score = simOneGame()    holds, busts = simNGames(n)    printSummary(holds, busts)if __name__ == "__main__":    main()

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images