-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSabacc.py
329 lines (291 loc) · 10.3 KB
/
Sabacc.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
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
from Cards import *
from Variants import Rules
import os
def clear():
os.system('cls')
def main():
clear()
print("Welcome to Sabacc. The Poker game of the future")
print()
r = Rules().getRuleNames()
for i,rule in enumerate(r):
print(str(i+1) + ") " + rule)
ruleSelection = input("Select a rule to play Sabacc: " )
if ruleSelection.isdigit():
ruleSelection = int(ruleSelection) - 1
else:
ruleSelection = 0
print("You're playing by " + r[ruleSelection] + " rules")
g = Game(r[ruleSelection])
choice = "Y"
while(choice != "n"):
#Loop until user says "N"
clear()
print("Current Score")
g.printScores()
g.play()
choice = input("Would you like to play another round? (Y/N)").lower()
#end loop
print()
print("Final Scores")
print()
g.printScores()
class Player:
__name = ""
__Score = 0
__hand = []
__handScore = 0
__discardCount = 0
def __init__(this, name):
this.__name = name
def getDiscardCount(this):
return this.__discardCount
def getName(this):
return this.__name
def getScore(this):
return this.__Score
def setScore(this, score):
this.__Score = score
def setHand(this, hand):
this.__hand = hand
def getHand(this):
return this.__hand
def getCard(this, index):
return this.__hand[index]
def AddCard(this, card):
if len(this.__hand) == 4:
print("Can't add any more cards to the hand")
else:
this.__hand.append(card)
def removeCard(this, cardIndex):
this.__hand.pop(cardIndex)
def printHand(this):
for i,d in enumerate(this.__hand):
print(str(i)+") " + d.getName())
def discardCard(this, index):
if this.getCard(index).discard():
this.__discardCount = this.__discardCount + 1
else:
this.__discardCount = this.__discardCount - 1
def cleanHand(this):
handCopy = this.__hand[:]
for c in handCopy:
if c.isDiscarded():
this.__hand.remove(c)
this.__discardCount = 0
def hasSuit(this, suit):
for c in this.__hand:
if c.getSuit() == suit:
return True
return False
class Game:
__discardList = []
__player = None
__computer = None
__array = None
__desk = None
__idiotsArray = {'name':"Idiot's", 'cards':['*:Idiot', '2:*', '3:*']}
__rules = None
def getArray(this,type=0):
if type == 0:
return this.__idiotsArray
else:
return this.__array
def printScores(this):
print(this.__player.getName() + ":" + str(this.__player.getScore()))
print(this.__computer.getName() + ":" + str(this.__computer.getScore()))
def __init__(this, rule):
this.__rules = Rules()
r = this.__rules.getRule(rule)
this.__deck = Deck(r['faces'])
this.__player = Player(input('Enter your name: '))
this.__computer = Player('Computer')
this.__discardList = r['discardList']
this.__array = r['array']
def play(this):
ph, ch = this.__deck.deal()
this.__player.setHand(ph)
this.__computer.setHand(ch)
this.discardAndDraw(1) #player
clear()
this.discardAndDraw(2) #computer
clear()
playerrank = this.rankHand(this.__player.getHand())
computerrank = this.rankHand(this.__computer.getHand())
if playerrank > computerrank:
#Player wins
this.declareWinner(1, this.__player.getHand(), this.__computer.getHand())
elif computerrank > playerrank:
#Computer wins
this.declareWinner(2, this.__player.getHand(), this.__computer.getHand())
elif computerrank == playerrank:
if playerrank == 1:
playerhand = this.getHandValue(this.__player.getHand())
computerhand = this.getHandValue(this.__computer.getHand())
if playerhand > 23:
playerhand = playerhand - 23
if computerhand > 23:
computerhand = computerhand - 23
if playerhand > computerhand:
#player wins
this.declareWinner(1, this.__player.getHand(), this.__computer.getHand())
elif computerhand > playerhand:
#computer wins
this.declareWinner(2, this.__player.getHand(), this.__computer.getHand())
else:
this.declareWinner(3, this.__player.getHand(), this.__computer.getHand())
print()
print("Scores: ")
print()
this.printScores()
print()
#If no winner, calculate score
#If score > 23 then subtrac 23
#Compare final scores, largest wins.
#declare winner
#Add score to final score for each player
def discardAndDraw(this, playerid):
print()
if playerid == 1:
print("Player's Turn")
this.changeHand(this.__player)
this.__player.printHand()
else:
print("Computer's Turn")
this.changeHand(this.__computer)
this.__computer.printHand()
def changeHand(this, owner):
loop = True
draw = 0
while(loop):
owner.printHand()
print("Discarded: " + str(owner.getDiscardCount()))
index = input("Select Card to Discard (q to continue): #")
if index == 'q':
draw = owner.getDiscardCount()
discardRules = this.__discardList
if draw in discardRules.keys():
if discardRules[draw] == "*":
loop = False
else:
if owner.hasSuit(discardRules[draw]):
loop = False
else:
print("You need a " + discardRules[draw] + " to discard " + str(draw) + " cards")
else:
message = "You can only discard "
for x in discardRules.keys():
message = message + str(x) + ", "
print(message + " cards using the current rules")
elif this.isInt(index) and int(index) < 4:
owner.discardCard(int(index))
owner.cleanHand()
newCards = this.__deck.draw(draw)
for c in newCards:
owner.AddCard(c)
def isInt(this, v):
try: i = int(v)
except: return False
return True
def declareWinner(this, playerid, hand1, hand2):
if playerid == 1:
print("Player won")
print("By " + this.rankHandMessage(hand1))
elif playerid == 2:
print("Computer won")
print("By " + this.rankHandMessage(hand2))
else:
print("It's a DRAW!")
playerscore = this.figureScore(hand1)
computerscore = this.figureScore(hand2)
this.__player.setScore(this.__player.getScore() + playerscore)
this.__computer.setScore(this.__computer.getScore() + computerscore)
def figureScore(this, playerhand):
score = 0
if this.isArray(playerhand, this.__idiotsArray) or this.isArray(playerhand, this.__array):
score = 23
else:
score = this.getHandValue(playerhand)
if score > 46:
score = 46 - score #if over 46 decrease score by difference
elif score > 23:
score = score - 23
return score
def isArray(this, hand, arrayDef):
###################
#Array List format
###################
#value:suit
#Value can be a wild card # = any number, r = any face, * = any value
#Value can also be a specific value 1 - 15
#suit can be a wild card * = any suit, f = any face
#suit can also be a specific suit or face name
#I'm using strings instead of dicts as they will share keys
matchList = []
arrayDef = arrayDef['cards']
for j in arrayDef:
value,suit = j.split(':')
for i,c in enumerate(hand):
if this.compareValues(value, c.getValue()):
if this.compareSuits(suit, c.getSuit()):
if not i in matchList:
matchList.append(i)
return len(matchList) >= len(arrayDef)
def compareValues(this, value1, value2):
if this.isInt(value1): value1 = int(value1)
if value1 == "*":
return True
elif value1 == "#":
return True
else:
if value1 == value2:
return True
return False
def compareSuits(this, suit1, suit2):
if suit1 == "*":
if suit2 in ['Staves', 'Sabers', 'Flasks', 'Coins']:
return True
elif suit1 == "f":
if suit2 in ['Idiot', 'Rancor','Jedi Knight','Jedi Master','Dark Jedi', 'Lord of the Sith', 'Smuggler', 'Bounty Hunter']:
return True
else:
if suit1 == suit2:
return True
return False
def rankHand(this, hand):
handvalue = this.getHandValue(hand)
handrank = 0
if handvalue == 23:
handrank = 5
elif handvalue == 46:
handrank = 4
elif this.isArray(hand, this.__idiotsArray):
handrank = 3
elif this.isArray(hand, this.__array):
handrank = 2
elif handvalue > 46:
handrank = 0
else:
handrank = 1
return handrank
def rankHandMessage(this,hand):
handrank = this.rankHand(hand)
if handrank == 5:
return "Pure Sabacc"
elif handrank == 4:
return "Sabacc"
elif handrank == 3:
return "Idiot's Array"
elif handrank == 2:
return this.__array['name'] + " array"
elif handrank == 0:
return "Opponent got over 46 points"
else:
return "Highest score"
def getHandValue(this, hand):
handvalue = 0
for c in hand:
handvalue = handvalue + c.getValue()
return handvalue
if __name__ == "__main__":
main()