-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.py
More file actions
195 lines (140 loc) · 4.65 KB
/
Copy pathCard.py
File metadata and controls
195 lines (140 loc) · 4.65 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 9 11:06:36 2013
@author: yuege
"""
class Card:
suitList = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
rankList = ['narf', 'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
def __init__(self, suit = 0, rank = 2):
self.suit = suit
self.rank = rank
def __str__(self):
return (self.rankList[self.rank] + " Of " + self.suitList[self.suit])
def __cmp__(self, other):
if self.suit > other.suit:
return 1
if self.suit < other.suit:
return -1
if self.rank > other.rank:
return 1
if self.rank < other.rank:
return -1
return 0
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(14):
self.cards.append(Card(suit, rank))
def printDeck(self):
for card in self.cards:
print card
def __str__(self):
s = ''
for i in range(len(self.cards)):
s = s + str(self.cards[i]) + '\n'
return s
def shuffle(self):
import random
nCards = len(self.cards)
for i in range(nCards):
j = random.randrange(i, nCards)
self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
# tmp = self.cards[i]
# self.cards[i] = self.cards[j]
# self.cards[j] = tmp
def removeCard(self, card):
if card in self.cards:
self.cards.remove(card)
return True
else:
return False
def popCard(self):
return self.cards.pop()
def isEmpty(self):
return (len(self.cards) == 0)
def deal(self, hands, nCards = 999):
nHands = len(hands)
for i in range(nCards):
if self.isEmpty():
break
card = self.popCard()
hand = hands[i % nHands]
hand.addCard(card)
class Hand(Deck):
def __init__(self, name = ''):
self.cards = []
self.name = name
def addCard(self, card):
self.cards.append(card)
def __str__(self):
s = 'Hand' + self.name
if self.isEmpty():
return s + ' is empty\n'
else:
return s + ' contains\n' + Deck.__str__(self)
class CardGame:
def __init__(self):
self.deck = Deck()
self.deck.shuffle()
class OldMaidHand(Hand):
def removeMatches(self):
count = 0
originalCards = self.cards[:]
for card in originalCards:
match = Card(3 - card.suit, card.rank)
if match in self.cards:
self.cards.remove(card)
self.cards.remove(match)
print 'Hand %s: %s matches %s' % (self.name,card,match)
count += 1
return count
class OldMaidGame(CardGame):
def play(self, names):
# remove Queen of Clubs
self.deck.removeCard(Card(0,12))
# make a hand for each player
self.hands = []
for name in names:
self.hands.append(OldMaidHand(name))
# deal the cards
self.deck.deal(self.hands)
print '---------- Cards have been dealt'
self.printHands()
# remove initial matches
matches = self.removeAllMatches()
print '---------- Matches discarded, play negins'
self.printHands()
# play until all 50 cars are mathced
turn = 0
numHands = len(self.hands)
while matches < 25:
matches = matches + self.playOneTurn(turn)
turn = (turn + 1) % numHands
print '---------- Game is Over'
self.printHands()
def removeAllMatches(self):
count = 0
for hand in self.hands:
count = count + hand.removeMatches()
return count
def printHands(self):
for hand in self.hands:
hand.printDeck()
def playOneTurn(self, i):
if self.hands[i].isEmpty():
return 0
neighbor = self.findNeighbor(i)
pickedCard = self.hands[neighbor]
self.hands[i].addCard(pickedCard)
print 'Hand', self.hands[i].name, 'pciked', pickedCard
count = self.hands[i].removeMatches()
self.hands[i].shuffle()
return count
def findNeighbor(self, i):
numHands= len(self.hands)
for next in range(1, numHands):
neighbor = (i + next) % numHands
if not self.hands[neighbor].isEmpty():
return neighbor