#Mission>> Black Jack game
<Rules> ※難易度調整のため一部改変
- 山札は無限大 (同じカードを重複して引くことがある)
- ジョーカーはなし
- "A"は1or11,Jack/Queen/Kingは10としてカウントする
- cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
- 山札からカードを引いてもデッキから削除されることなく、常に同じカードが山札に存在する
BJ.py
#from art import logo
import random
from replit import clear
def deal_card():
"""returns a random card from a deck""" #deal_card()のDocstring
cards = [11,2,3,4,5,6,7,8,9,10,10,10,10]
card = random.choice(cards)
return card
def cal_score(cards):
"""check whether BlackJack and Ace card then returns total score"""
if sum(cards) == 21 and len(cards) == 2:
return 0 #score=0をBlackJackとする
if 11 in cards and sum(cards) > 21: #"A"(=11)を引いた際にover21なら1に変換
cards.remove(11)
cards.append(1)
return sum(cards)
def compare(user_score,dealer_score):
"""compare score and returns a message"""
if user_score == dealer_score:
return "DRAW!(^^)!"
elif dealer_score == 0:
return "Lose, opponent has BLACKJACK"
elif user_score == 0:
return "You win with a BLACKJACK."
elif user_score > 21:
return "You are over 21...You lose!!"
elif dealer_score > 21:
return "You Win!! Opponent is over 21."
elif user_score > dealer_score:
return "You win!!"
else:
return "You lose..."
def play_game():
"""controls game"""
#print(logo)
user_cards = []
dealer_cards = []
is_game_over = False #gameから抜ける
for i in range(2):
user_cards.append(deal_card())
dealer_cards.append(deal_card())
while not is_game_over:
#for the user loop
user_score = cal_score(user_cards)
dealer_score = cal_score(dealer_cards)
if len(user_cards) < 3:
if user_score == 0: #このままだとBlackJackの時にCurrent scoreに0が入ってしまうので、
user_score = 21 #21に変更しておく
print(f"\nNOW...\nYour cards: {user_cards}\t\tCurrent score: {user_score}\nDealer's first card:{dealer_cards[0]}")
else:
print(f"\n>> You added {user_cards[-1]}\n\nNOW...\nYour cards: {user_cards}\t\tCurrent score: {user_score}\nDealer's first card: {dealer_cards[0]}")
if user_score == 21 or dealer_score == 0 or user_score > 21:
is_game_over = True
else:
again = input("HIT?? [y/n] ")
if not again == "n":
user_cards.append(deal_card())
else:
is_game_over = True
while dealer_score != 21 and dealer_score < 17:
#for dealer loop
dealer_cards.append(deal_card())
dealer_score = cal_score(dealer_cards)
if dealer_score == 0: #ここも、BlackJackの際にCurrent scoreに0ではなく21を入れる
dealer_score = 21
print(f"\nDealer final hand:{dealer_cards}\t\tDealer's final score:{dealer_score}\n\nRESULT...")
result = compare(user_score,dealer_score)
line = "+"*len(result)
print(f"++++{line}\n| {result} |\n{line}++++")
while not input("\nDou you want to play a BLACKJACK?? [y/n] ") == "n":
clear()
play_game()
else:
print("Bye...")
出力結果
NOW...
Your cards: [10, 10] Current score: 20
Dealer's first card:10
HIT?? [y/n] y
>> You added 8
NOW...
Your cards: [10, 10, 8] Current score: 28
Dealer's first card: 10
Dealer final hand:[10, 6, 8] Dealer's final score:24
RESULT...
++++++++++++++++++++++++++++++++
| You are over 21...You lose!! |
++++++++++++++++++++++++++++++++
Dou you want to play a BLACKJACK?? [y/n]