I made blackjack with python!

at first

Watch udemy's Video about Object Orienting I thought it might be object-oriented, so I made blackjack instead of trying my skills!

Prerequisites

--There is no concept such as a deck. --A is always treated as 1. ――No 5 cards, both bursts are won by the dealer.

code

import random

CARDS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
CARDS_DIC = {"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}


class Hit(list): #Player or dealer hit
    
    def __init__(self, list):
        self.list = list
        
    def player_hit(self):
        self.list.append(random.choice(CARDS))
        print(self.list[-1] + "I pulled")
        show_hand(self.list)
        
    def dealer_hit(self):
        
        while True:
            if sum_cards(self.list) >= 17 and sum_cards(self.list) <= 21:
                return False
            elif sum_cards(self.list) < 17:
                self.list.append(random.choice(CARDS))
            else:
                return False
        
    
class Interface(list): #Branch
    
     def __init__(self, list):
        self.list = list
    
     def hit_or_stand(self): 
        while True:
            answer = input("hit or stand?:").lower()
            if answer == "hit":
                Hit(self.list).player_hit()
            elif answer == "stand":
                return False
            else:
                print("Please enter hit or stand")
            if sum_cards(self.list) > 21:
                print("Burst")
                return False
            
def classification(player_list, dealer_list): #Judgment of victory or defeat
    
    sum_player = sum_cards(player_list)
    sum_dealer = sum_cards(dealer_list)
    
    if 21 >= sum_player > sum_dealer or sum_dealer > 21 >= sum_player:
        print(f"you: {sum_player}")
        print(f"dealer: {sum_dealer}")
        print("You win")
        
    elif 21 >= sum_dealer > sum_player or sum_player > 21 >= sum_dealer or sum_player and sum_dealer > 21:
        print(f"you: {sum_player}")
        print(f"dealer: {sum_dealer}")
        print("You lose")
        
    else:
        print(f"you: {sum_player}")
        print(f"dealer: {sum_dealer}")
        print("It's a draw")
        
def sum_cards(lists): #Aggregate cards
    
    sum_card = 0
    for list in lists:
        sum_card += CARDS_DIC[list]
        
    return sum_card

def show_hand(list): #Show the cards in your hand and the total of the cards
    player_hand_info = "When".join(list)
    print( "Your hand is" + player_hand_info + "is")
    print("total" + str(sum_cards(list)) + "is")
    
def main():
    # 1,Player draws a card(2 sheets)
    player_hand = random.choices(CARDS, k=2)
    player_interface = Interface(player_hand)
    
    # 2,Dealer draws card(2 sheets)
    dealer_hand = random.choices(CARDS, k=2)
    dealer_hit = Hit(dealer_hand)
    
    print("Game Start")
    print()
    
    # 3,Player chooses hit or stand(Until burst or stand)
    print("It's your turn")
    show_hand(player_hand)
    player_interface.hit_or_stand()
    print()
    
    #4,Dealer chooses hit or stand(Until the total is 17 or more)
    print("It's the dealer's turn")
    dealer_hit.dealer_hit()
    print()
    
    # 5,Judge and finish
    print("Judgment")
    classification(player_hand, dealer_hand)#Main processing layer
    
if __name__ == "__main__": 
    main()

Processing result

Game Start

It's your turn
Your hand is K and 9
19 in total
hit or stand?:hit
I drew 7
Your hand is K, 9 and 7
26 in total
Burst

It's the dealer's turn

Judgment
you: 26
dealer: 18
You lose

Reflections

During development, I didn't understand the class, I feel that the result is strange code. (Compared to the code written by others, my code is in a half-finished state with no person class, only Hit class and Interface class)

I want to study object-oriented programming again and try again.

It's difficult to think object-oriented is difficult!

Recommended Posts

I made blackjack with python!
I made blackjack with Python.
I made a simple blackjack with Python
I made wordcloud with Python.
I made a fortune with Python.
I made a daemon with Python
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I tried fp-growth with python
I tried scraping with Python
I made a python text
I tried gRPC with Python
I made COVID19_simulator with JupyterLab
I tried scraping with python
I made Word2Vec with Pytorch
Othello made with python (GUI-like)
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
SNS Python basics made with Flask
I made a Line-bot using Python!
I tried web scraping with python.
I made my own Python library
Numer0n with items made in Python
I sent an SMS with Python
Othello game development made with Python
I liked the tweet with python. ..
I played with PyQt5 and Python3
I want to debug with Python
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
When I made CaboCha usable with python3, I got stuck (Windows 10)
I made a simple typing game with tkinter in Python
I made a package to filter time series with python
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
I made a simple book application with python + Flask ~ Introduction ~
[I made it with Python] XML data batch output tool
I made a puzzle game (like) with Tkinter in Python
Life game with Python [I made it] (on the terminal & Tkinter)
FizzBuzz with Python3
I made a simple circuit with Python (AND, OR, NOR, etc.)
Scraping with Python
I made a library to easily read config files with Python
Statistics with python
I made Othello to teach Python3 to children (4)
I made a payroll program in Python!
Scraping with Python
HTTP split download guy made with Python
Python with Go