티스토리 뷰

def clear_screen():
    import os
    if os.name in ('nt', 'dos'):  # If Machine is running on Windows, use cls
        os.system('cls')
    else:
        os.system('clear')
def print_ongoing_card(your_card, computer_card):
    print(f"\tYour cards: {your_card},  current score: {sum(your_card)}")
    print(f"\tComputer's first card: {computer_card[0]}")
def your_turn(your_card, cards):
    your_more_card = random.choice(cards)
    if your_more_card == 11 and your_more_card+sum(your_card) > 21:
        your_more_card=1
    your_card.append(your_more_card)
def computer_turn(computer_card, cards):
    computers_more_card = random.choice(cards)
    if computers_more_card == 11 and computers_more_card+sum(computer_card) > 21:
        computers_more_card=1
    computer_card.append(computers_more_card)
def final_check(your_card, computer_card):
    print(f"\tYour final hand: {your_card},  final score: {sum(your_card)}")
    print(f"\tComputer's final hand: {computer_card}, final score: {sum(computer_card)}")
    if sum(your_card)<=21 and sum(computer_card)<=21:
        if sum(your_card) > sum(computer_card):
            print("You win ^^")
        elif sum(your_card) < sum(computer_card):
            print("You lose T_T")
        else :
            print("Draw")
    elif sum(your_card)<=21 and sum(computer_card)>21:
        print("Opponent go over. You Win ^^")
    elif sum(your_card)>21 :
        print("You went over. You lose T_T")
    main()
def main():
    if input("Do you want to play a game of Blackjack? Type 'y' or 'n'").lower() == 'y':
        clear_screen()
        print(logo)
        your_card = [random.choice(cards) for i in range(2)]
        computer_card = [random.choice(cards) for i in range(2)]
        print_ongoing_card(your_card, computer_card)
        if sum(your_card) == 21 or sum(computer_card) == 21:
            if sum(your_card) == 21 :
                print("You got the blackjack! You win !! ^^")
            else :
                print("Opponent got the blackjack! You lose T_T")
            return 0
        repeat = True
        while repeat == True :
            if input("Type 'y' to get another card, type 'n' to pass.: ") == "y":
                your_turn(your_card, cards)
                computer_turn(computer_card, cards)
                print_ongoing_card(your_card,computer_card)
                if sum(your_card) >= 21:
                    repeat = False
            else:
                repeat = False
                while sum(computer_card) < 17:
                    computer_turn(computer_card, cards)
        final_check(your_card, computer_card)
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
import random
from art import logo
main()

'Test > Python(20220101~)' 카테고리의 다른 글

Day13_How to Debugging  (0) 2022.01.18
Days12_Guess Number  (0) 2022.01.16
Days11_Black Jack, 21  (0) 2022.01.16
Python 화면 지우기  (0) 2022.01.15
Days10_계산기 최종  (0) 2022.01.15
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함