11import random
22
3- class Card (object ):
3+ class Card ( object ):
44 def __init__ (self , name , value , suit , symbol ):
55 self .value = value
66 self .suit = suit
77 self .name = name
88 self .symbol = symbol
99 self .showing = False
10+
1011 def __repr__ (self ):
1112 if self .showing :
1213 return self .symbol
1314 else :
1415 return "Card"
1516
1617class Deck (object ):
17- def shuffle (self , times = 1 ):
18+ def shuffle (self , times = 1 ):
1819 random .shuffle (self .cards )
1920 print ("Deck Shuffled" )
20-
21+
2122 def deal (self ):
2223 return self .cards .pop (0 )
2324
24- class StandardDeck (object ):
25+ class StandardDeck (Deck ):
2526 def __init__ (self ):
2627 self .cards = []
2728 suits = {"Hearts" :"♡" , "Spades" :"♠" , "Diamonds" :"♢" , "Clubs" :"♣" }
28- values = {"Two" :2 ,
29- "Three" :3 ,
30- "Four" :4 ,
31- "Five" :5 ,
32- "Six" :6 ,
33- "Seven" :7 ,
34- "Eight" :8 ,
35- "Nine" :9 ,
36- "Ten" :10 ,
37- "Jack" :11 ,
38- "Queen" :12 ,
39- "King" :13 ,
40- "Ace" :14 }
29+ values = {"Two" :2 ,
30+ "Three" :3 ,
31+ "Four" :4 ,
32+ "Five" :5 ,
33+ "Six" :6 ,
34+ "Seven" :7 ,
35+ "Eight" :8 ,
36+ "Nine" :9 ,
37+ "Ten" :10 ,
38+ "Jack" :11 ,
39+ "Queen" :12 ,
40+ "King" :13 ,
41+ "Ace" :14 }
42+
4143 for name in values :
4244 for suit in suits :
4345 symbolIcon = suits [suit ]
4446 if values [name ] < 11 :
4547 symbol = str (values [name ])+ symbolIcon
4648 else :
47- symbol = name [0 ] + symbolIcon
48- self .cards .append (Card (name , values [name ], suit , symbol ))
49+ symbol = name [0 ]+ symbolIcon
50+ self .cards .append ( Card (name , values [name ], suit , symbol ) )
4951
5052 def __repr__ (self ):
51- return "Standard deck of cards:{0} cards remaining" .format (len (self .cards ))
52-
53- def shuffle (self , times = 1 ):
54- random .shuffle (self .cards )
55- print ("Deck Shuffled" )
56-
57- def deal (self ):
58- return self .cards .pop (0 )
53+ return "Standard deck of cards:{0} remaining" .format (len (self .cards ))
5954
6055class Player (object ):
6156 def __init__ (self ):
@@ -67,6 +62,7 @@ def cardCount(self):
6762 def addCard (self , card ):
6863 self .cards .append (card )
6964
65+
7066class PokerScorer (object ):
7167 def __init__ (self , cards ):
7268 # Number of cards
@@ -77,61 +73,198 @@ def __init__(self, cards):
7773
7874 def flush (self ):
7975 suits = [card .suit for card in self .cards ]
80- if len (set (suits )) == 1 :
76+ if len ( set (suits ) ) == 1 :
8177 return True
82- else :
83- return False
78+ return False
8479
8580 def straight (self ):
8681 values = [card .value for card in self .cards ]
8782 values .sort ()
8883
89- if not len (set (values )) == 5 :
90- return False
91- if value [4 ] == 14 and value [3 ] == 5 and value [2 ] == 4 and value [1 ] == 3 and value [0 ] == 2 :
92- return True
84+ if not len ( set (values )) == 5 :
85+ return False
86+
87+ if values [4 ] == 14 and values [0 ] == 2 and values [1 ] == 3 and values [2 ] == 4 and values [3 ] == 5 :
88+ return 5
89+
9390 else :
94- if not value [0 ] + 1 == value [1 ]: return False
95- if not value [1 ] + 1 == value [2 ]: return False
96- if not value [2 ] + 1 == value [3 ]: return False
97- if not value [3 ] + 1 == value [4 ]: return False
91+ if not values [0 ] + 1 == values [1 ]: return False
92+ if not values [1 ] + 1 == values [2 ]: return False
93+ if not values [2 ] + 1 == values [3 ]: return False
94+ if not values [3 ] + 1 == values [4 ]: return False
9895
99- return True
96+ return values [ 4 ]
10097
10198 def highCard (self ):
10299 values = [card .value for card in self .cards ]
103100 highCard = None
104101 for card in self .cards :
105102 if highCard is None :
106103 highCard = card
107- elif highCard .value < card .value :
108- highCard = card
104+ elif highCard .value < card .value :
105+ highCard = card
106+
109107 return highCard
110108
109+ def highestCount (self ):
110+ count = 0
111+ values = [card .value for card in self .cards ]
112+ for value in values :
113+ if values .count (value ) > count :
114+ count = values .count (value )
115+
116+ return count
117+
118+ def pairs (self ):
119+ pairs = []
120+ values = [card .value for card in self .cards ]
121+ for value in values :
122+ if values .count (value ) == 2 and value not in pairs :
123+ pairs .append (value )
124+
125+ return pairs
126+
127+ def fourKind (self ):
128+ values = [card .value for card in self .cards ]
129+ for value in values :
130+ if values .count (value ) == 4 :
131+ return True
132+
133+ def fullHouse (self ):
134+ two = False
135+ three = False
136+
137+ values = [card .value for card in self .cards ]
138+ if values .count (values ) == 2 :
139+ two = True
140+ elif values .count (values ) == 3 :
141+ three = True
142+
143+ if two and three :
144+ return True
145+
146+ return False
147+
111148def Poker ():
112149 player = Player ()
113-
114- # initial amount
115- initialAmount = 100
116- # cost per hand
150+
151+ # Intial Amount
152+ points = 100
153+
154+ # Cost per hand
117155 handCost = 5
118156
119- ## hand loop
120- discard = []
121-
122- # shuffle
123- deck = StandardDeck ()
124- deck .shuffle ()
125-
126- # deal
127- for i in range (5 ):
128- player .addCard (deck .deal ())
129-
130- for card in player .cards :
131- card .showing = True
132-
133- # hold or pass
134- # score
135-
136- print (player .cards )
137-
157+ end = False
158+ while not end :
159+ print ( "You have {0} points" .format (points ) )
160+ print ()
161+
162+ points -= 5
163+
164+ ## Hand Loop
165+ deck = StandardDeck ()
166+ deck .shuffle ()
167+
168+ # Deal Out
169+ for i in range (5 ):
170+ player .addCard (deck .deal ())
171+
172+ # Make them visible
173+ for card in player .cards :
174+ card .showing = True
175+ print (player .cards )
176+
177+ validInput = False
178+ while not validInput :
179+ print ("Which cards do you want to discard? ( ie. 1, 2, 3 )" )
180+ print ("*Just hit return to hold all, type exit to quit" )
181+ inputStr = input ()
182+
183+ if inputStr == "exit" :
184+ end = True
185+ break
186+
187+ try :
188+ inputList = [int (inp .strip ()) for inp in inputStr .split ("," ) if inp ]
189+
190+ for inp in inputList :
191+ if inp > 6 :
192+ continue
193+ if inp < 1 :
194+ continue
195+
196+ for inp in inputList :
197+ player .cards [inp - 1 ] = deck .deal ()
198+ player .cards [inp - 1 ].showing = True
199+
200+ validInput = True
201+ except :
202+ print ("Input Error: use commas to separated the cards you want to hold" )
203+
204+ print (player .cards )
205+ #Score
206+ score = PokerScorer (player .cards )
207+ straight = score .straight ()
208+ flush = score .flush ()
209+ highestCount = score .highestCount ()
210+ pairs = score .pairs ()
211+
212+ # Royal flush
213+ if straight and flush and straight == 14 :
214+ print ("Royal Flush!!!" )
215+ print ("+2000" )
216+ points += 2000
217+
218+ # Straight flush
219+ elif straight and flush :
220+ print ("Straight Flush!" )
221+ print ("+250" )
222+ points += 250
223+
224+ # 4 of a kind
225+ elif score .fourKind ():
226+ print ("Four of a kind!" )
227+ print ("+125" )
228+ points += 125
229+
230+ # Full House
231+ elif score .fullHouse ():
232+ print ("Full House!" )
233+ print ("+40" )
234+ points += 40
235+
236+ # Flush
237+ elif flush :
238+ print ("Flush!" )
239+ print ("+25" )
240+ points += 25
241+
242+ # Straight
243+ elif straight :
244+ print ("Straight!" )
245+ print ("+20" )
246+ points += 20
247+
248+ # 3 of a kind
249+ elif highestCount == 3 :
250+ print ("Three of a Kind!" )
251+ print ("+15" )
252+ points += 15
253+
254+ # 2 pair
255+ elif len (pairs ) == 2 :
256+ print ("Two Pairs!" )
257+ print ("+10" )
258+ points += 10
259+
260+ # Jacks or better
261+ elif pairs and pairs [0 ] > 10 :
262+ print ("Jacks or Better!" )
263+ print ("+5" )
264+ points += 5
265+
266+ player .cards = []
267+
268+ print ()
269+ print ()
270+ print ()
0 commit comments