블랙잭 게임2

👩‍💻NEO_매트릭스·2021년 11월 23일
0

Python

목록 보기
2/2
post-thumbnail

main.py

##################### Hints #####################
import random
from art import logo
deal_card() 함수를 만들어 카드 리스트중 랜덤으로 하나 리턴한다.

def deal_card():
"""함수를 정의한다."""
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card = random.choice(cards)
return card

calculate_score() 카드들의 배열을 입력하면 더한 값을 리턴하는 함수 작성 sum() 함수 사용
블랙잭일때(ace + 10) 실제 값이 아니라 0 을 리턴한다.

점수가 21점이 넘을때 에이스(11)가 있으면 1로 바꾼다. append() 와 remove() 사용
def calculate_score(cards):
"""입력한 카드들의 더한값을 리턴"""
if sum(?) == 21 and len(?) == 2:
return ?
if 11 in cards and sum(?) > 21:
cards.remove(?)
cards.append(?)
return sum(cards)

compare() 함수는 유저와 컴의 점수를 비교하여 승리 패배 Draw를 가린다.
def compare(user_score, computer_score):
if ? == ?:
return "Draw 🙃 비겼음 !"
elif computer_score == ?:
return "패배~ 컴이 블랙잭! 😱"
elif user_score == ?:
return "승리~ 블랙잭! 😎"
elif user_score ? 21:
return "패배~ 21 넘김 😭"
elif computer_score ? 21:
return "승리~ 컴이 21 넘김 😁"
elif user_score ? computer_score:
return "승리~ 😃"
else:
return "패배~ 😤"

def playgame():
블랙잭 로고를 출력
print(logo)
유저, 컴 카드들의 배열을 선언, 게임 끝을 알려주는 변수 선언
user_cards = []
computer_cards = []
is_game_over = False
유저, 컴 모두 2장의 카드를 받아 위의 배열에 입력한다.

이때 append() 와 deal_card() 함수 사용
for
in range(2):
user_cards.append(deal_card())
computer_cards.append(deal_card())

블랙잭? 21점 초과? 한장 더?
while not is_game_over:

    user_score = ?(user_cards)
    computer_score = ?(computer_cards)
    print(f"  당신의 카드: {user_cards}, 현재 점수: {user_score}")
    print(f"  컴의 첫카드: {computer_cards[0]}")

    if user_score ? 0 or computer_score ? 0 or user_score ? 21:
        is_game_over = True  # 블랙잭이 나오거나 유저의 점수가 21보다 크면 종료
    else:
        # 카드를 한장 더 받으면 게임을 계속 진행하고 더이상 카드를 받지 않으면 반복문을 종료한다.
        user_should_deal = input(
            "카드를 한장 더 받겠습니까?  (y/n) : ")
        if user_should_deal ? "y":
            user_cards.append(?)  # 카드 한장 더 
        else:
            is_game_over = True

# 컴퓨터는 블랙잭이 아니며 17점 미만에서는 계속 카드를 뽑는 전략을 사용
while computer_score ? 0 and computer_score ? 17:
    computer_cards.append(deal_card())
    computer_score = calculate_score(?)

print(f" 당신의 덱은: {user_cards}, 최종 점수: {user_score}")
print(f" 컴퓨터 덱은: {computer_cards}, 최종 점수: {computer_score}")
print(compare(user_score, computer_score))

0개의 댓글