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!
For learning Python syntax etc., I referred to the following.
[Self-study programmer from the basics of the Python language to how to work](https://www.amazon.co.jp/%E7%8B%AC%E5%AD%A6%E3%83%97%E3%83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9E% E3% 83% BC-Python% E8% A8% 80% E8% AA% 9E% E3% 81% AE% E5% 9F% BA% E6% 9C% AC% E3% 81% 8B% E3% 82% 89% E4% BB% 95% E4% BA% 8B% E3% 81% AE% E3% 82% 84% E3% 82% 8A% E6% 96% B9% E3% 81% BE% E3% 81% A7-% E3% 82% B3% E3% 83% BC% E3% 83% AA% E3% 83% BC% E3% 83% BB% E3 % 82% A2% E3% 83% AB% E3% 82% BD% E3% 83% 95 / dp / 4822292274 / ref = tmm_hrd_swatch_0? _Encoding = UTF8 & qid = 1588765972 & sr = 8-2)
I wanted to implement something in Python before machine learning, so I did an introduction to Python
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.
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)
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