import random
from replit import clear
def deal_card():
"""랜덤으로 카드 뽑기"""
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card = random.choice(cards)
return card
def calculate_score(cards):
"""카드 합계산"""
if sum(cards) == 21 and len(cards) == 2: #두장의 카드합이 21이면 게임이 끝나는 조건
return 0
if 11 in cards and sum(cards) > 21:
cards.remove(11)
cards.append(1)
return sum(cards)
def compare(user_score,com_score):
if user_score == com_score:
print("동점입니다.")
elif com_score == 0:
print("당신이 졌습니다.")
elif user_score == 0:
print("당신이 이겼습니다.")
elif user_score > 21:
print("당신이 졌습니다.")
elif com_score > 21:
print("당신이 이겼습니다.")
elif user_score > com_score:
print("당신이 이겼습니다.")
else:
print("당신이 졌습니다.")
def play_game():
user_cards = []
com_cards = []
is_game = False
for _ in range(2):
user_cards.append(deal_card())
com_cards.append(deal_card())
while not is_game:
user_score = calculate_score(user_cards)
com_score = calculate_score(com_cards)
print(f'당신의 카드는 {user_cards}, 점수는 {user_score}')
print(f'컴퓨터의 첫번째 카드는 {com_cards[0]}')
if user_score == 0 or com_score == 0 or user_score > 21:
is_game = True
else:
user_aw = input("게임을 끝내시겠습니까? 'y' 아니면 카드를 받으시겠습니까? 'n'")
if user_aw == 'y':
is_game = True
else:
user_cards.append(deal_card())
#사용자 턴이 끝나면 컴퓨터도 카드를 다시 받을지 결정한다.
while com_score < 17 or com_score == 0:
com_cards.append(deal_card())
com_score = calculate_score(com_cards)
print(f'당신의 카드는 {user_cards}, 점수는 {user_score}')
print(f'컴퓨터 카드는 {com_cards}, 점수는 {com_score}')
compare(user_score, com_score)
user_aw = input("시작하시겠습니까? 'y' or 'n'")
if user_aw == 'y':
clear()
play_game()
후기
솔직히 나에게는 조금 어려웠다. 그러나
블랙잭이라는 게임을 알게 되었고 함수를 짜는 능력도 향상된거 같다.
프로잭트로 제대로 블랙잭이라는 게임을 만들고 싶다.
반응형
'언어 > 파이썬' 카테고리의 다른 글
[부트캠프] 간단한 계산 프로그램 만들기 - 10일 (0) | 2023.05.22 |
---|---|
[부트캠프]Dictionary 딕셔너리 -9일차 (1) | 2023.05.17 |
[부트캠프] shift를 이용한 암호화 복호화 코드 8일차 (1) | 2023.05.09 |
[부트캠프] for 과 while 반복문 - 6일차 (0) | 2023.05.05 |
[부트캠프] for문으로 비밀번호 만들기 (0) | 2023.05.03 |