forked from Miyanaqy/cardgame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main
232 lines (203 loc) · 7.51 KB
/
main
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding:utf-8 -*-
import random
from cards import Card
#其实更好的方法是,import method ,然后通过method.XXX()去调用函数呢
def get_new_suit():
global active_suit
got_suit = False
while not got_suit:
suit = raw_input("Pick a suit: ")
if suit.lower() == 'd':
active_suit = "Diamonds"
got_suit = True
elif suit.lower() == 's':
active_suit = "Spades"
got_suit = True
elif suit.lower() == 'h':
active_suit = "Hearts"
got_suit = True
elif suit.lower() == 'c':
active_suit = "Clubs"
got_suit = True
else:
print "Not a valid suit. Try again."
print "You picked ", active_suit
def computer_turn():
global c_hand, deck, up_card, active_suit, blocked
options = []
for card in c_hand:
if card.rank == '8':
c_hand.remove(card)
up_card = card
print "\nComputer played ", card.short_name
print "Up card: ", up_card.short_name
#suit totals: [diamonds, hearts, spades, clubs]
suit_totals = [0, 0, 0, 0]
for suit in range(1, 5):
for card in c_hand:
if card.suit_id == suit:
suit_totals[suit-1] += 1
long_suit = 0
for i in range(4):
if suit_totals[i] > long_suit:
long_suit = i
if long_suit == 0: active_suit = "Diamonds"
if long_suit == 1: active_suit = "Hearts"
if long_suit == 2: active_suit = "Spades"
if long_suit == 3: active_suit = "Clubs"
print " Computer changed suit to ", active_suit
return # done computer's hand - back to main loop
if card.suit == active_suit:
options.append(card)
if card.rank == up_card.rank:
options.append(card)
if len(options) > 0:
best_play = options[0]
for card in options:
if card.value > best_play.value:
best_play = card
c_hand.remove(best_play)
up_card = best_play
print "\nComputer played ", best_play.short_name
else:
if len(deck) > 0:
next_card = random.choice(deck)
c_hand.append(next_card)
deck.remove(next_card)
print "\nComputer drew a card"
else:
print "Computer is blocked"
blocked += 1
print "Computer has %i cards left" %(len(c_hand))
#print("Suit is ",active_suit)
def player_turn():
global deck, p_hand, blocked, up_card, active_suit
print "Suit is: ",active_suit
print "What would you like to do? ",
response = raw_input("\nType a card name to play or 'Draw' to take a card: ")
is_eight = False
## play or draw a card
valid_play = False
while not valid_play:
selected_card = None
while selected_card == None:
if response.lower() == 'draw':
valid_play = True
if len(deck) > 0:
card = random.choice(deck)
p_hand.append(card)
deck.remove(card)
print "You drew", card.short_name
print "\nYour hand: "
for card in p_hand:
print card.short_name,
else:
print "There are no cards left in the deck"
blocked += 1
break
else:
for card in p_hand:
if response.upper() == card.short_name:
selected_card = card
if selected_card == None:
response = raw_input("You don't have that card. Try again:")
if selected_card.rank == '8':
valid_play = True
is_eight = True
get_new_suit()
p_hand.remove(selected_card)
up_card = selected_card
print "You played:",selected_card.short_name
print "\nYour hand:"
for card in p_hand:
print card.short_name,
elif selected_card.suit == active_suit:
valid_play = True
p_hand.remove(selected_card)
up_card = selected_card
up_card.suit = active_suit
print "You played:",selected_card.short_name
print "\nYour hand:"
for card in p_hand:
print card.short_name,
#print "\nSuit is ",up_card.suit
elif selected_card.rank == up_card.rank:
valid_play = True
p_hand.remove(selected_card )
up_card = selected_card
print "You played:",selected_card.short_name
print "\nYour hand:"
for card in p_hand:
print card.short_name,
print "\nSuit still is",active_suit
else:
if not valid_play:
response = raw_input("That's not a legal play. Try again: ")
def init_cards():
global deck, up_card, active_suit, p_hand, c_hand
for suit_id in range(1, 5):
for rank_id in range(1, 14):
new_card = Card(suit_id, rank_id)
if new_card.rank_id == 8:
new_card.value = 50
deck.append(new_card)
if len(deck) == 52:
up_card = random.choice(deck)
deck.remove(up_card)
active_suit = up_card.suit
for i in range(1,6):
a = random.choice(deck)
p_hand.append(a)
deck.remove(a)
for i in range(1,6):
b = random.choice(deck)
c_hand.append(b)
deck.remove(b)
print "\nYour hand: ",
for card in p_hand:
print card.short_name,
print " Up card: ", up_card.short_name
if up_card.rank == '8':
print" Suit is", active_suit
p_hand = []
c_hand = []
deck = []
done = False
p_total = c_total = 0
while not done:
game_done = False
while not game_done:
blocked = 0
init_cards()
player_turn()
if len(p_hand) == 0:
game_done = True
print "You won !"
if not game_done:
computer_turn()
if len(c_hand) == 0:
game_done = True
print "Computer won! "
#display the game score here
if blocked >= 2:
game_done = True
print "Both player blocked, GAME OVER."
p_points = 0
for card in c_hand:
p_points += card.value
p_total += p_points
c_points = 0
for card in p_hand:
c_point += card.value
c_total += c_points
print "You got %i points for computer's hand" % p_points
print "Computer got %i points for your hand" % c_points
play_again = raw_input("play again (Y/N)? ")
if play_again.lower().startswith('y'):
done = False
print"\nSo far,you have %i points" % p_total
print"\nand the computer has %i points." % c_total
else:
done = True
print "\n Final Score:"
print "You: %i Computer: %i" %(p_total, c_total)