-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_deck.py
More file actions
55 lines (46 loc) · 1.69 KB
/
card_deck.py
File metadata and controls
55 lines (46 loc) · 1.69 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# =============================================================================
#
# FILE: card_deck.py
# AUTHOR: Tan Duc Mai <henryfromvietnam@gmail.com>
# CREATED: 2021-12-10
# DESCRIPTION: A module to draw and fill card.
# I hereby declare that I completed this work without any improper help
# from a third party and without using any aids other than those cited.
#
# =============================================================================
# ------------------------------- Module Import -------------------------------
from random import shuffle
# ------------------------------ Global Constant ------------------------------
# Contains cards in the format [value, suit]
# DO NOT ACCESS THIS VARIABLE!
# Call draw_card() instead.
deck = []
# ---------------------------- Function Definitions ---------------------------
def draw_card():
"""
Removes and returns the last item from the global deck.
The deck will be reset after the last card is drawn.
"""
global deck
if len(deck) == 0:
fill(deck)
return deck.pop()
def fill(deck):
"""
DO NOT call this function.
This function is called automatically by draw_card().
Creates and returns a new deck of 52 cards.
Each card is a list in the format [value, suit] e.g., ['Ace', 'Spades'].
"""
deck.clear()
for value in [
'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10',
'Jack', 'Queen', 'King'
]:
for suit in [
'Hearts', 'Spades', 'Diamonds', 'Clubs'
]:
deck.append([value, suit])
shuffle(deck)