-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcards.py
More file actions
executable file
·159 lines (115 loc) · 3.06 KB
/
cards.py
File metadata and controls
executable file
·159 lines (115 loc) · 3.06 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python3
################################
# File Name: cardsExample.py
# Author: Chadd Williams
# Date: 10/20/2014
# Class: CS 360
# Assignment: Lecture Examples
# Purpose: a deck of cards to demonstrate unittests
################################
import random
class DeckOfCards:
suits= ['hearts', 'clubs','diamonds','spades', 'none']
namedCards = ['jack','queen','king','ace', 'joker']
def __init__(self):
""" constructor. initialize the deck
"""
self.initializeDeck()
def getName(self, card):
""" return the string value for the value of the card
this value could be 2-10 or any string in namedCards
"""
if card[0] > 10 :
name = self.namedCards[card[0]-11]
else:
name = str(card[0])
return name
def initializeDeck(self):
""" reset deck to unshuffled state. No jokers
"""
# add the numbers
# 11-14 is jack, queen, king, ace
# 16 is joker
self._cards = [ (number,suit) for suit in self.suits[0:4] for number in range(2,15)]
def addJokers(self):
""" add two jokers to the end of the deck
"""
self._cards.append( (15, 'none'))
self._cards.append( (15, 'none'))
def displayCard(self, card):
""" display a card in a nice way
"""
name = self.getName(card)
print(name, "of", card[1])
def display(self):
""" display the entire deck
"""
for card in self._cards:
self.displayCard(card)
def shuffle(self):
""" shuffle the deck
"""
random.shuffle(self._cards)
def draw(self):
""" draw the top card off the end and return that card
"""
return self._cards.pop()
def top(self):
""" look at the top card on the deck but don't remove the card
"""
return self._cards[len(self._cards)-1]
def isEmpty(self):
""" is the deck empty ?
"""
return len(self._cards) == 0
def higherCard(self, left, right):
""" Which card is higher?
return 0 if left is higher
return 1 if right is higher
return -1 on a tie
raise ValueError if duplicate cards are given other than jokers
"""
if left[0] > right[0] :
return 0
elif left[0] < right[0] :
return 1
elif self.suits.index(left[1]) > self.suits.index(right[1]) :
return 0
elif self.suits.index(left[1]) < self.suits.index(right[1]) :
return 1
elif 'joker' != self.getName(left) :
raise ValueError("Duplicate cards!")
else:
return -1
"""
deck = DeckOfCards()
deck.display()
deck.addJokers()
deck.shuffle()
print("-------------")
deck.display()
print(deck.draw())
print(deck.isEmpty())
print(deck.draw())
print(deck.isEmpty())
cardLeft = deck.draw()
cardRight = deck.draw()
print("-------------")
deck.displayCard(cardLeft)
deck.displayCard(cardRight)
print("The higher card is:")
higher = deck.higherCard(cardLeft, cardRight)
if 0 == higher:
deck.displayCard(cardLeft)
elif 1 == higher:
deck.displayCard(cardRight)
cardLeft = (15, 'none') # joker
deck.displayCard(cardLeft)
deck.displayCard(cardRight)
print("The higher card is:")
higher = deck.higherCard(cardLeft, cardRight)
if 0 == higher:
deck.displayCard(cardLeft)
elif 1 == higher:
deck.displayCard(cardRight)
"""