-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
executable file
·185 lines (132 loc) · 4.15 KB
/
game.py
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
#!/usr/bin/env python
## write a card game called Old Maid. Here is how it works:
# - 1st: Get list of players
# - 2nd: Hand out 10 cards to each player
# - 3rd: Each player matches his cards based on same rank and complimentary suits (clubs<->spades) (diamonds<->hearts)
# - 4th: The matched cards are discarded.
# - 5th: Game begins with the rest of the cards
# - 6th: Each player takes a card from the player on his left and matches it with his stack.
# if there is a match, he removes both the matching cards else, he adds the new card to his stack
# - 7th: Step 6 is repeated until one player has no more cards left
# Properties of card:
# 1. suit
# 2. rank
# methods:
# 1. init
# 2. str
# 3. cmp
class Card:
suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
ranks = ['NA', 'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
def __init__(self, suit=0, rank=0):
self.suit = suit
self.rank = rank
def __str__(self):
return '%s of %s' %(self.ranks[self.rank], self.suits[self.suit])
# any kind of comparision (<, >, ==, in etc)
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
# Properties of Deck:
# 1. list of cards
# methods:
# 1. init
# 2. print_deck
# 3. shuffle
# 4. remove
# 5. add
# 6. pop
# 7. deal
# 8. is_empty
class Deck:
def __init__(self):
self.cards = []
for s in range(4):
for r in range(1, 14):
self.cards.append(Card(s, r))
def print_deck(self):
for s in range(4):
for r in range(1, 14):
print Card(s, r)
def shuffle(self):
import random
n = len(self.cards)
for i in range(n/2):
j = random.randrange(i, n)
self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
def remove(self, card):
if card in self.cards:
self.cards.remove(card)
return True
else:
return False
def add(self, card):
self.cards.append(card)
def is_empty(self):
return (len(self.cards) == 0)
def pop(self):
return self.cards.pop()
def deal(self, hands, num_cards=99):
n = len(hands)
for i in range(num_cards):
if self.is_empty(): break
hand = hands[i % n]
card = self.pop()
hand.add(card)
def remove_matches(self):
dup_cards = self.cards[:]
for c in dup_cards:
for i in range(len(self.cards)):
if dup_cards.rank == self.cards[i].rank:
self.cards.remove(self.cards[i])
dup_cards.remove(c)
# Properties of Hand:
# 1. name of the player
# 2. list of cards
# methods:
# 1. init
# 2. str
# all methods of Deck
class Hand(Deck):
def __init__(self, name=""):
self.name = name
self.cards = []
def __str__(self):
l = len(self.cards)
s = "Hand for player %s: \n\t" %(self.name)
if (l <= 0): s += "Empty!!\n"
else:
for i in range(l):
s += str(self.cards[i]) + "\n\t"
return s
# CardGame: parent class for OldMaidGame
class CardGame:
def __init__(self):
self.deck = Deck()
self.deck.shuffle()
class OldMaidGame(CardGame):
pass
# players is a list of objects of Hands
def remove_all_matches(players):
for player in players:
player.remove_matches()
# declare the constants
names = ["Adit", "Rishith", "Manu", "Sonu"]
cards_per_player = 10
players = []
# set the deck
deck = Deck()
deck.shuffle()
# initialize the players
for i, name in enumerate(names):
players.append(Hand(name))
# deal the cards to the players
deck.deal(players, cards_per_player*len(players))
# print each player's hand
for player in players:
print player
# remove matching cards from within each hand
remove_all_matches(players)