-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardGame.py
More file actions
412 lines (341 loc) · 14.1 KB
/
CardGame.py
File metadata and controls
412 lines (341 loc) · 14.1 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
'''
Card game
Every card selections will be based on a key (number) assigned to each card
Level 1:
Select three cards
If 3 cards are of the same family - pass level 1
else ask user to guess a riddle to go to the next level.
If user gueses right level passed. Else 1 life gone - start all over again. Game ends if you loose 3 lives.
Level 2:
User gets 10 chances to select pairs per chance out of the deck of cards.
For every game/chance the two cards selected will not be shown, point will increase if they are pairs,
else points remain the same. After every game the pairs chosen will be removed from the deck of cards.
She/He shoudl have won atleast 1 game more than the threshold score calculated to go to the next game.
Else go back to previous level
Threshold score calculation: The first card that you score for is noted, and its face value is the score
you have got to compete!
Level 3:
From the remaining cards from level 2 (30 cards) take 1 card but you wont know what that card is.
You will have 7 chances to inbetween which you can opt to see the pivot card,
but once you do this you won't be able to play further.
At the end of 7 games you for every card you selected if the face value of the card is > the face value of pivot card,
you wont that game, else you lost.
Total number of games won is this round should be at least two greater than your
counterpart computer score!
'''
'''
Required functions/methods
card class:
shuffle()
play game class:
level1() level2() level3()
computer_plays
score_for_Level2()
General:
players_total_score
lifecount
set of riddles with answer
'''
'''
GOAL : Use concepts of Inheritance , Class, function , multi threading
'''
from random import shuffle
from random import randint
import random
import sys
deck={}
card={}
#This class would contain shuffle, card deck generation
class Card(object):
def deck_Of_Cards(self):
family=['Heart','Spades','Clubs','Diamond'] #Family of Cards
number=['Ace','King','Queen','Jack',"Ten","Nine","Eight","Seven","Six","Five","Four","Three","Two"]
deck=[[a,b] for a in family for b in number]
#Call the generateNumberforCard to have a unique number for each card
card = self.generate_Number_for_Card(deck)
return card
def shuffle_card(self,card):
values=card.values()
shuffle(values)
shuffledCard=dict(zip(card.keys(),values))
return shuffledCard
#Generate a unique number for each card
def generate_Number_for_Card(self,deck):
for i in range(0,len(deck)):
card[i]=deck[i]
return card
#playGame class inherits Card class
class playGame(Card):
selectedCard=[]
life=3
scoreComp=0
def start_Game(self,card):
card = cardDeck.deck_Of_Cards()
card = cardDeck.shuffle_card(card)
#print card
print "\n You have started your game! Buckle up and let us test your memory!\n"
print " This is a Card Game. You wont be seeing a card, but each number corresponds to a card!\n"
print "Beware your cards are shuffled! Lets start now!! \n"
enter = raw_input("Press Enter to continue\n")
if not enter:
self.level3(card)
#compare User score and computer score
def level1(self,card):
#For level 1 n=3
#print card
chances=0
score = 0
choice="Y"
print "This is level 1. Game Rules!"
print "******************************"
print self.game_rules(1)
print "*********************************\n"
print " Atleast 4 of your 5 cards shoudl be of the same family\n"
while chances<10 and self.life!=0 and choice!="COMPLETE":
choice = self.level1_Check(card)
if choice=='R':
answer=self.riddle()
if answer==True:
print "YAY you passed this Riddle!!"
choice="COMPLETE"
else:
print "Oops that was wrong again! You loose a life"
self.life=self.life-1
card = cardDeck.shuffle_card(card)
else:
card = cardDeck.shuffle_card(card)
chances=chances+1
print "CHOICE from LEVEL1 ",choice
if choice=="COMPLETE":
print "YAY you passed this level!!\n"
enter = raw_input("Press enter to continue\n")
if not enter:
self.level2(cardDeck.shuffle_card(card))
#return choice
def level1_Check(self,card):
lost=True
option=self.userChoice(5)
for i in option:
self.selectedCard.append(card[int(i)])
if len(set([k for k,v in self.selectedCard]))>4:
lost=True
choice=raw_input("OOPs bad luck! Do you want to Try again to answer a riddle to pass the level?!\n Type R for riddle! ")
else:
lost=False
choice="COMPLETE"
return choice
def level2(self,card):
#10 chances for user to sekect pair of cards
score=0
chance=10
computerscore=0
deletedCard=[]
print "This is level 2. Game Rules!"
print "******************************"
print self.game_rules(2)
print "*********************************\n"
while chance>0:
self.selectedCard=[]
#Use Multithreading to make user and computer play together
#This is users game
option=self.userChoice(2)
try:
for i in option:
if i in deletedCard:
raise
except:
#sys.exit("I am sorry, you entered a number that has been removed off the deck! You need to have a better memory. Game Over!")
print "I am sorry, you entered a number that has been removed off the deck! You need to have a better memory. \nGo back to level1!"
score=0
enter = raw_input("Press enter to continue\n")
if not enter:
self.start_Game(card)
break
sys.stdout.write('\x1b[1A')
sys.stdout.write('\x1b[2K')
for i in option:
self.selectedCard.append(card[int(i)])
#del card[int(i)]
#use try catch to make sure the sae number isnt chosen again
print [v for k,v in self.selectedCard]
if len(set([v for k,v in self.selectedCard]))==1:
lost=True
score=score+1
chance=chance+1
if score==1:
computerscore= [int(v) for k,v in self.face_value(self.selectedCard).items()][0]
print computerscore
for i in option:
deletedCard.append(i)
del card[int(i)]
else:
lost=False
chance = chance-1
if score > computerscore and score>0:
print "\nYay! your score is",score
print " And you've won the threshold score of", computerscore
print "Congrats, you are goin to the next level!\n"
enter = raw_input("Press enter to continue\n")
if not enter:
self.level3(card)
elif score <= computerscore and score>0:
print "OOPS your score is only",score
print " You have to go back to level 1 because you haven't crossed the score of\n",computerscore
card = cardDeck.deck_Of_Cards()
enter = raw_input("Press enter to continue\n")
if not enter:
self.level1(cardDeck.shuffle_card(card))
elif score==0:
print "I am sorry you lost the game.\nYou've got to start from beginning.\n"
enter = raw_input("Press enter to continue\n")
if not enter:
self.start_Game(card)
#return score,computer score
def checkCompScore(self,card):
#Computer Selects two cards from 0-51 every game.
a,b= random.sample(range(0,51),2)
if card[int(a)]==card[int(b)]:
self.scoreComp=self.scoreComp+1
return self.scoreComp
def level3(self,card):
print "This is level 3. Game Rules!"
print "******************************"
print self.game_rules(3)
print "*********************************\n"
chance=0
chosenCard=[]
pivotCard=[]
cardVal=[]
print [k for k,v in card.items()]
pivot = int(raw_input("Pick one card as a pivot "))
print "Great! Your Pivot card has been chosen. Well you can't see it,\n but lets test your luck with the other 7 chances!"
while chance<7:
print "This is your",chance,"st/nd/rd choice! "
val=int(raw_input("Select your card "))
#Should check if the value chosen is from the list
chosenCard.append(val)
choice = raw_input("Do you want to see the Pivot and quit this game?(Y/N) ")
if choice=='Y':
print " You chose to end the game.\n"
break
chance=chance+1
for i in chosenCard:
cardVal.append(card[i])
pivotCard.append(card[pivot])
pivotVal = [v for k,v in self.face_value(pivotCard).items()][0]
print "This is your Pivot card! : ",pivotCard," Its Face Value is",pivotVal," Lets see your luck now!\n"
#print cardVal
print self.face_value(cardVal)
def riddle(self):
print "This is a riddle!"
riddle = {1:"What can travel around the world while staying in a corner?",
2:"If you have me, you want to share me. If you share me, you havent got me. What am I?",
3:"I run distances, often making many turns, yet I never move one foot. What am I?",
4:"You answer me, although I never ask you questions. What am I?"
}
answers={1:"stamp",2:"secret",3:"watch",4:"phone"}
number=randint(1,4)
userAnswer= raw_input(riddle[number])
if userAnswer.lower()==answers[number]:
return True
else:
return False
def game_rules(self,n):
rules={1:"Level 1:\n\
Select three cards\n\
If 4 cards are of the same family - pass level 1\n\
else ask user to guess a riddle to go to the next level.\n\
If user gueses right level passed. Else 1 life gone - start all over again. Game ends if you loose 3 lives.",
2: "User gets 10 chances to select pairs per chance out of the deck of cards.\n\
For every game/chance the two cards selected will not be shown, point will increase if they are pairs,\n\
else points remain the same. After every game the pairs chosen will be removed from the deck of cards\n\
She/He shoudl have won atleast 1 game more than the threshold score calculated to go to the next game.\n\
Else go back to previous level. Bonus chances to play for every score you get!\n\
\n\
Threshold score calculation: The first card that you score for is noted, and its face value is the \n\
score you have got to compete!",
3:"From the remaining cards from level 2 (30 cards) take 1 card but you wont know what that card is.\n\
You will have 7 chances to inbetween which you can opt to see the pivot card,\n\
but once you do this you won't be able to play further.\n\
At the end of 7 games you for every card you selected if the face value of the card is > the face value of pivot card,\n\
you wont that game, else you lost.\n\
Total number of games won is this round should be at least two greater than your\n\
counterpart computer score!"}
return(rules[n])
def user_Total_Score():
pass
def face_value(self,card):
value={}
number={'Ace':14,'King':13,'Queen':12,'Jack':11,"Ten":10,"Nine":9,"Eight":8,"Seven":7,"Six":6,"Five":5,"Four":4,"Three":3,"Two":2}
for k,v in card:
value[v]=number[v]
return value
def userChoice(self,n):
choice=raw_input("Choose %s unique numbers (0-51) with space between each number: " %(n))
option=choice.split(" ")
while len(set(option))< n:
print "Sorry you either chose the same card more than once or entered the wrong number of cards"
choice=raw_input("Choose %s unique numbers (0-51) with space between each number: " %(n))
option=choice.split(" ")
return option
cardDeck = playGame()
cardDeck.start_Game(card)
'''
deck={}
card={}
def deckOfCards():
family=['Heart','Spades','Clubs','Diamond'] #Family of Cards
number=['Ace','King','Queen','Jack',"Ten","Nine","Eight","Seven","Six","Five","Four","Three","Two"]
deck=[[a,b] for a in family for b in number]
return deck
def userChoice(n):
choice=raw_input("Choose %d unique numbers (0-51) with space between each number: ",n)
option=choice.split(" ")
return option
#Generate a unique number for each card
def generateNumberforCard(deck):
for i in range(0,len(deck)):
card[i]=deck[i]
return card
def shuffle_card(card):
values=card.values()
shuffle(values)
shuffledCard=dict(zip(card.keys(),values))
return shuffledCard
def printSelectedCard(selectedCard):
for i in selectedCard:
print 3*" ",i[1],"of",i[0]
print " "
def isWinner(selectedCard):
number=[]
for x in selectedCard:
n=x
number.append([y.split(',') for y in n][1])
x=number[0]
number.remove(x)
if len(number)==0:
print "YAY! YOU WON"
else:
print "OOPS! YOU LOST!"
def main():
print " "
selectedCard=[]
#Stores Deck of Cards
deck=deckOfCards()
#Creates a number for Deck of Cards
card=generateNumberforCard(deck)
#Shuffle the cards
shuffledCard = shuffle_card(card)
#Takes in User Options (4 numbers)
n=2
option=userChoice(n)
for i in option:
selectedCard.append(shuffledCard[int(i)])
print "The Cards you have chosen are: "
#Print the cards you have chosen
printSelectedCard(selectedCard)
#Check if winner or not
isWinner(selectedCard)
if __name__ == '__main__':
main()
'''