Black Jack Game COde
Different cards: A, 2, 3, 4, 5, 6, 7, 8, 9, 0 (for 10), J, Q, K. Note that the number 10 is represented by the single character 0.The goal of Blackjack is to get as close to 21 points as possible without going higher. Each of the thirteen cards above has a point total attached: the numerals are worth their given value (2 points for 2, 7 points for 7, etc.). J, Q, and K are worth 10 points. A is worth either 1 or 11 points, whichever is better for the player. These are the rules we'll use for our Blackjack-playing AI.#The rules are:# - The dealer must Hit if their total is below 17. # - The dealer must Stay as soon as their total is 17 or higher. # - An Ace (A) should be counted as 11 if it puts the dealer between 17 and 21 points. If it puts them over 21, though, it should be counted as 1. #For example, imagine the dealer's first cards are A and 3. Their point total is either 4 or 14, both below 17, so they Hit. The next card is a 9. If we count the A as 11, then their total is now 23 (11 + 3 + 9), and so we count the A as 1. Their total is 13, and so they Hit again. The next card is a 7, so their total is 20, so they Stay. # - "Hit" if the dealer should take another card. # - "Stay" if the dealer should not take another card. # - "Bust" if the sum is already over 21. def acefunction(sum): if sum >= 17 and sum < 21: return 11 elif sum < 17 or sum > 21: return 1 def next_move(myString): sum = 0 for chara in myString: #print("chara=", chara) if chara in ['J', 'K', 'Q', '0']: sum += 10 elif chara == "A": sum += acefunction(sum) else: sum += int(chara) if sum > 21: return "Bust" elif sum >= 17: return "Stay" elif sum < 17: return "Hit" This code works but I am not sure if this is the optimal solution. COuld anyone advise of alternate solution.