python 21 시 미니 게임 실현

8628 단어 python21 시 게임
python 으로 21 시 작은 게임 을 실현 합 니 다.참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

from random import shuffle
import random

import numpy as np

from sys import exit

#       
playing_cards = {
    "  A": 1, "  2": 2, "  3": 3, "  4": 4, "  5": 5, "  6": 6, "  7": 7, "  8": 8, "  9": 9, "  10": 10,
    "  J": 10, "  Q": 10, "  K": 10,
    "  A": 1, "  2": 2, "  3": 3, "  4": 4, "  5": 5, "  6": 6, "  7": 7, "  8": 8, "  9": 9, "  10": 10,
    "  J": 10, "  Q": 10, "  K": 10,
    "  A": 1, "  2": 2, "  3": 3, "  4": 4, "  5": 5, "  6": 6, "  7": 7, "  8": 8, "  9": 9, "  10": 10,
    "  J": 10, "  Q": 10, "  K": 10,
    "  A": 1, "  2": 2, "  3": 3, "  4": 4, "  5": 5, "  6": 6, "  7": 7, "  8": 8, "  9": 9, "  10": 10,
    "  J": 10, "  Q": 10, "  K": 10
}
#     
poker_name = list(playing_cards.keys())

#       
poker_count = 1
poker_list = poker_count*poker_name

#            A,       A    1  11
four_a = {'  A', '  A', '  A', '  A'}

#    
total_score = np.array([0, 0])

#      
game_round = 1


def random_cards(poker_name_list):
    """
          :          
    """
    shuffle(poker_name_list)


def score_count(hand_poker):
    """
            
    :param hand_poker:         
    :return:        poker_score
    """
    #       ,       
    poker_score = 0
    #   :     A   ,    
    have_a = False

    #         
    for k in hand_poker:
        poker_score += playing_cards[k]

    #           A,   A          
    for i in hand_poker:
        if i in four_a:
            have_a = True
            break
        else:
            continue

    if have_a:
        if poker_score + 10 <= 21:
            poker_score = poker_score + 10

    return poker_score


def who_win(your_score, pc_score):
    """
           
    :param your_score:     
    :param pc_score:     
    :return:      
    """
    if your_score > 21 and pc_score > 21:
        print('  ')
        return np.array([0, 0])
    elif your_score > 21 and pc_score <= 21:
        print('   ,    ')
        return np.array([0, 1])
    elif your_score <= 21 and pc_score > 21:
        print('  !!     ')
        return np.array([1, 0])
    elif your_score <= 21 and pc_score <= 21:
        if your_score > pc_score:
            print('  !!     ')
            return np.array([1, 0])
        elif your_score < pc_score:
            print('   ,    ')
            return np.array([0, 1])
        else:
            print('  !!')
            return np.array([0, 0])


def if_get_next_poker():
    """
          
    """
    if_continue = input("         ?(Y/N)>>>>:")
    if if_continue.upper() == "Y":
        return get_one_poker()

    elif if_continue.upper() == "N":
        print('      ')
        return False
    else:
        print("    ,     ")
        return if_get_next_poker()


def get_one_poker():
    """
        :   poker_list       
    :return:
    """
    return poker_list.pop(random.randint(0, len(poker_list)-1))


def continue_or_quit():
    """
           ,           
    """
    if_next_round = input("         (Y/N)>>>>:")
    if if_next_round.upper() == 'Y':
        #              
        if len(poker_list) <= 15:
            print('   ,      ,       ,    。')
            exit(1)
        else:
            return True
    elif if_next_round.upper() == "N":
        print("     。    !!")
        exit(1)
    else:
        print("    ,     ")
        return continue_or_quit()


def start_game_init_two_poker(poker_database):
    """
         ,            
    :param poker_database:   
    :return:             
    """
    return [poker_database.pop(random.randint(0, len(poker_list)-1)),
            poker_database.pop(random.randint(0, len(poker_list)-1))]


def every_round(porker_list):
    """
            
    :param porker_list:  
    :return:      
    """
    #       ,        
    your_hand_poker = []
    #      ,        
    pc_hand_poker = []
    #     ,         
    you_init_poker = start_game_init_two_poker(porker_list)
    pc_init_poker = start_game_init_two_poker(porker_list)
    #          
    print(f"        :{you_init_poker[0]} {you_init_poker[1]}")
    print(f"           :{pc_init_poker[0]}")
    #                
    your_hand_poker.extend(you_init_poker)
    pc_hand_poker.extend(pc_init_poker)
    #          
    your_score = score_count(your_hand_poker)
    pc_score = score_count(pc_hand_poker)
    #         ,      21 ,             
    if your_score == 21 or pc_score == 21:
        print("     21  。")
        return who_win(your_score, pc_score)
    #     ,        ,      。
    else:
        while True:
            get_new_poker = if_get_next_poker()

            #     
            if get_new_poker != False:
                #                    
                your_hand_poker.append(get_new_poker)
                print(f"       {your_hand_poker}")
                your_score = score_count(your_hand_poker)
                if your_score > 21:
                    print("        21 ")
                    print(f"       {pc_hand_poker}")
                    return who_win(your_score, pc_score)
                else:
                    continue
            #       ,       
            elif get_new_poker == False:
                #        :          
                # while pc_score < your_score:
                #     pc_new_poker = get_one_poker()
                #     pc_hand_poker.append(pc_new_poker)
                #     #              
                #     pc_score = score_count(pc_hand_poker)
                #        :              [1:18] ,     
                while pc_score in range(1, 19):
                    pc_new_poker = get_one_poker()
                    pc_hand_poker.append(pc_new_poker)
                    #          
                    pc_score = score_count(pc_hand_poker)
                print(f"       {pc_hand_poker}")
                return who_win(your_score, pc_score)
            else:
                continue


"""
       
"""
while True:
    print("      ,    !!!")
    input("  【enter】    >>>")
    print(f"    {game_round}   ")

    #   
    random_cards(poker_list)

    #     
    score = every_round(poker_list)

    #     
    total_score = np.add(total_score, score)

    print(f'      ,    :{total_score[0]}:{total_score[1]}')
    game_round += 1
    continue_or_quit()

running result

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기