I tried to implement a card game of playing cards in Python

Introduction

As an introduction to Python, I would like to challenge Graduation exams from programming beginners should develop'Blackjack'. The requirements are used as they are. There are some unfamiliar points, so it's good to do this here! I would like you to comment that there is something like that!

What I did before implementation

For learning Python syntax etc., I referred to the following.

I wanted to implement something in Python before machine learning, so I did an introduction to Python

About implementation

Before the game of blackjack, I thought that I needed the function of the deck of cards, which is the basis of card games, so I will start from there.

Card class

Get the card information by giving the instance generated from the Card class a mark and a numeric argument.

card.py


#!/usr/bin/env python
# coding: UTF-8


class Card:
    """
Output card marks and numbers
    Attributes
    ----------
    card_mark : int
Card mark (♠ ︎❤︎ ♦ ︎♣ ︎)
    card_value : int
Card numbers
    """

    #mark
    MARKS = ("♠︎-", "❤︎-", "♦︎-", "♣️-")
    #Numbers
    VALUES = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)

    def __init__(self, card_mark, card_value):
        """
        Parameters
        ----------
        card_mark : int
Card mark (♠ ︎❤︎ ♦ ︎♣ ︎)
        card_value : int
Card numbers
        """
        self.mark = card_mark
        self.value = card_value

    #Override and change the output of the instance
    def __repr__(self):
        return self.MARKS[self.mark] + str(self.VALUES[self.value])

If you execute the above Card class alone, it will be as follows. The output content of the instance is changed using the repr method.

#Create an instance from the Card class (mark and number as arguments)
#Since the output content is changed by the repr method, it is output as follows
#Normal →<__main__.Card object at 0x10c949310>
#Add repr method → ♦ ︎-4

# ♦︎-4
card = Card(2, 4)
print(card)

Deck class

Use the Card class to generate a random list of 52 cards per deck.

deck.py


#!/usr/bin/env python
# coding: UTF-8

#Card class
import card
#Shuffle list elements
from random import shuffle


class Deck:
    """
Create 52 playing cards (1 deck)
    Attributes
    ----------
    deck_set_no : int
Number of decks
    """

    def __init__(self, deck_set_count=1):
        """
        Parameters
        ----------
        deck_set_count : int
Number of decks
        """

        self.cards = []
        for index in range(deck_set_count):
            #4 marks
            for mark in range(len(card.Card.MARKS)):
                #Numbers (2 to 13+1 of 14)
                for value in range(len(card.Card.VALUES)):
                    #Add to cards
                    self.cards.append(card.Card(mark, value))
        #Shuffle the deck
        shuffle(self.cards)

    def get_card(self, card_num=1):
        """
Get element and remove it from list
        Parameters
        ----------
        card_num : int
Number of cards to get
        """
  
        self.card_list = []
        if len(self.cards) == 0:
            #Do nothing when there are 0 cards
            return

        #self.card from the end in cards_Get num minutes and delete from list
        for index in range(card_num):
            self.card_list.append(self.cards.pop())
        return self.card_list

When you execute the Deck class, it will be as follows.

#Generate two decks
deck = Deck(2)
# [♦︎-12, ♠︎-3, ♦︎-13, ❤︎-9, ♦︎-3, ♦︎-8, ♠︎-11, ♦︎-13, ♣︎-9, ♦︎-11,
#  ♦︎-12, ♠︎-2, ♠︎-8, ❤︎-10, ♦︎-4, ♦︎-9, ♣︎-10, ♣︎-4, ♦︎-9, ♠︎-6,
#  ♠︎-3, ♣︎-12, ♠︎-12, ❤︎-13, ♣︎-11, ♣︎-8, ♦︎-10, ♦︎-5, ♦︎-7, ❤︎-12,
#  ♣︎-8, ♦︎-10, ♣︎-10, ♦︎-6, ♠︎-9, ♦︎-5, ❤︎-12, ♠︎-12, ♠︎-10, ♣︎-1,
#  ♣︎-3, ❤︎-1, ❤︎-9, ❤︎-3, ❤︎-6, ♠︎-8, ♦︎-1, ♠︎-13, ❤︎-3, ♦︎-2, ♣︎-1,
#  ♣︎-12, ♠︎-7, ❤︎-8, ♣︎-5, ♠︎-9, ♣︎-4, ♠︎-10, ❤︎-11, ❤︎-4, ♣︎-11,
#  ♠︎-4, ♣︎-7, ❤︎-7, ❤︎-6, ♣︎-3, ♣︎-9, ♦︎-2, ♦︎-11, ❤︎-5, ❤︎-4, ♠︎-13,
#  ♠︎-4, ♣︎-13, ♠︎-1, ♦︎-1, ♠︎-2, ♦︎-8, ❤︎-8, ♣︎-5, ♣︎-6, ♠︎-7, ♣︎-7,
#  ♠︎-11, ♠︎-6, ♠︎-5, ♦︎-6, ❤︎-5, ♣︎-6, ♣︎-13, ❤︎-7, ♦︎-7, ❤︎-10, ❤︎-11,
#  ♠︎-5, ❤︎-2, ❤︎-13, ♦︎-3, ♠︎-1, ❤︎-1, ♦︎-4, ♣︎-2, ❤︎-2, ♣︎-2]
print(deck.cards)

#Number of cards 104
print(len(deck.cards))

#Draw 5 cards[♣︎-2, ❤︎-2, ♣︎-2, ♦︎-4, ❤︎-1]
#The drawn card is removed from the list
print(deck.get_card(5))

With this, the deck of playing cards and the function of drawing cards have been implemented!

Next time, I would like to summarize the data retention of Player and Dealer!

Recommended Posts

I tried to implement a card game of playing cards in Python
I tried to implement blackjack of card game in Python
I tried playing a typing game in Python
I tried to implement a one-dimensional cellular automaton in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement TOPIC MODEL in Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I want to easily implement a timeout in python
I tried to implement Dragon Quest poker in Python
I tried to implement what seems to be a Windows snipping tool in Python
I tried "How to get a method decorated in Python"
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried a stochastic simulation of a bingo game with Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
Implement a deterministic finite automaton in Python to determine multiples of 3
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to create a Python script to get the value of a cell in Microsoft Excel
I want to create a window in Python
I want to make a game with Python
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to develop a Formatter that outputs Python logs in JSON
I want to color a part of an Excel string in Python
I tried adding a Python3 module in C
I tried to display the altitude value of DTM in a graph
I tried to make a ○ ✕ game using TensorFlow
I tried to implement merge sort in Python with as few lines as possible
I tried to create a class that can easily serialize Json in Python
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
[Azure] I tried to create a Linux virtual machine in Azure of Microsoft Learn
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I tried to summarize how to use matplotlib of python
I tried to implement Minesweeper on terminal with python
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I tried to implement a recommendation system (content-based filtering)
I want to randomly sample a file in Python
I tried to implement an artificial perceptron with python
I want to work with a robot in python.
I tried to automatically generate a password with Python3
I tried to summarize how to use pandas in python
[Python] I tried to get Json of squid ring 2
I tried using Python (3) instead of a scientific calculator
I tried to implement automatic proof of sequence calculation
I tried to summarize the string operations of Python
I tried to implement PCANet
I tried to implement StarGAN (1)
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
I tried to implement a volume moving average with Quantx
I tried to implement a basic Recurrent Neural Network model
I tried to find the entropy of the image with python
I made a simple typing game with tkinter in Python