+ 1

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.

4th Nov 2020, 1:35 PM
NG_
NG_ - avatar
7 Respostas
+ 8
Wow such a short question. It'll take hours to read this question ,days to understand this question and minutes to solve this question.😂 I was just joking.. NG_ Pls post ur attempt(code) so that we can help with that..
4th Nov 2020, 1:39 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 8
NG_ Pls let us know when the editing is over.
4th Nov 2020, 1:49 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 4
The logic is not correct. You shouldn't call acefunction in for loop. You should calculate all cards which are not ace first. Then, loop over each Ace (Can be more than one), test different combination, and decide based on the possibilities.
4th Nov 2020, 3:12 PM
Gordon
Gordon - avatar
0
Is this a some type of holiday homework?? NG_
4th Nov 2020, 1:40 PM
Smiley[Offline]
0
Hello- I was trying to edit my post and you all replied so fast.
4th Nov 2020, 1:44 PM
NG_
NG_ - avatar
0
Editing is over & I have posted my code as well.
4th Nov 2020, 2:36 PM
NG_
NG_ - avatar
0
def next_move(myString): sum = 0 ace_in = False for chara in myString: #print("chara=", chara) if chara in ['J', 'K', 'Q', '0']: sum += 10 elif chara == "A": ace_in = True else: sum += int(chara) print("sum = ",sum) if ace_in: if sum <= 10: sum += 11 elif sum > 10 : sum += 1 if sum >= 17 and sum <= 21: return "Stay" elif sum < 17: return "Hit" else: return "Bust" ***** I updated my code as above**** Is there any othee optimal solution..
4th Nov 2020, 7:59 PM
NG_
NG_ - avatar