-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
1269 lines (1058 loc) · 31.6 KB
/
server.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 15-112: Principles of Programming and Computer Science
# Final Project: Poker game, server.py
# Name : Stefan Baumann
# AndrewID : sbaumann
# Created : 23 NOV 2018
# Updated : 10 DEC 2018
from random import *
import socket
import itertools
from Tkinter import *
class Card:
def __init__(self, value, suit):
self.value = value # 2-10, J, Q, K, A
self.suit = suit # C(lubs), D(iamonds), H(earts), S(pades)
def getCard(self):
''' returns value and suit of card object '''
return self.value + self.suit
class Deck:
def __init__(self):
self.thedeck = []
self.shuffled = False
self.makeDeck()
def makeDeck(self):
""" creates 52 card objects and adds to thedeck list """
values = []
for i in range(2, 11):
values.append(str(i))
values += ['J', 'Q', 'K', 'A']
suits = ['C', 'D', 'H', 'S']
for j in values:
for s in suits:
cardobj = Card(j, s)
card = cardobj.getCard()
self.thedeck.append(card)
def shuffleDeck(self):
''' shuffles thedeck list '''
shuffle(self.thedeck)
self.shuffled = True
def removeCard(self, card):
''' removes given card from deck '''
self.thedeck.remove(card)
def takeTopCard(self):
thecard = self.thedeck[0]
self.removeCard(thecard)
return thecard
class Player:
''' creates a player on the table '''
def __init__(self, username):
self.playername = username
self.location = 'Login'
self.dealer = False
self.smallblind = False
self.bigblind = False
self.stack = 2500
self.holecards = []
self.active = False
self.yourturn = False
self.bet = 0
self.roundturns = 0
self.result = 11
self.besthand = ()
class Start:
def __init__(self, parent):
self.wnd = parent
self.gamewnd = None # window for Game instance
self.server = None
self.s = None
self.allfriendnames = [] # list of names of all friends
self.totalplayers = {} # dictionary of names and player objs of friends
self.onlineplayers = {} # dictionary of names and player objs of online friends
self.openplayers = [] # list of player objs that are in Open
self.tableplayers = [] # list of player objs that are in Table
self.gameplayers = [] # list of player objs that are in Game
self.anythingNew = False # Boolean for new messages received
self.game = None # Game instance when game is started
self.gameon = False # Boolean for game running
if self.getOnline():
self.timer1()
else:
self.wnd.destroy()
def getOnline(self):
''' creates connection to server and logs into sbaumann11/poker server '''
self.server = ChatProgram()
self.s = self.server.startConnection('86.36.46.10', 15112)
if self.server.login(self.s, 'sbaumann11', 'sbaumann'):
Label(self.wnd, text='Server is online.').pack()
print 'Server is online.'
return True
else:
print 'Cannot get online.'
return False
def timer1(self):
''' reocurring function checks for new players and new messages '''
self.acceptNewFriends()
self.getAllFriends()
self.addNewPlayers()
self.checkMessages()
oldopen, oldtable, oldgame = self.openplayers, self.tableplayers, self.gameplayers
self.locatePlayers()
if self.anythingNew:
for plyrobj in self.openplayers:
self.giveHomeData(plyrobj)
for plyrobj in self.tableplayers:
self.giveTableData(plyrobj)
self.wnd.after(500, self.timer1)
def acceptNewFriends(self):
''' goes through active users and checks for friend requests '''
allusers = self.server.taskTwo(self.s, '@users')
for i in allusers:
self.server.taskThree(self.s, i, '@accept')
def getAllFriends(self):
''' turns allfriends into a list of all friends '''
self.allfriendnames = self.server.taskTwo(self.s, '@friends')
def addNewPlayers(self):
''' checks which friends arent players and adds them to totalplayers '''
oldplayers = self.totalplayers
for name in self.allfriendnames:
if name not in oldplayers:
plyrobj = Player(name)
self.totalplayers[name] = plyrobj
def checkMessages(self):
''' checks for new messages from players and changes location of players'''
messagelist = self.server.getMail(self.s)
if messagelist == []:
self.anythingNew = False
else:
self.anythingNew = True
for t in messagelist:
username = t[0]
msg = t[1]
if msg == 'Logon-Player':
self.onlineplayers[username] = self.totalplayers[username]
self.onlineplayers[username].location = 'Open'
self.server.sendMessage(self.s, username, "Welcome to Stefan's online poker room.")
self.server.sendMessage(self.s, username, "Join the table, invite some friends, and have some fun!")
elif msg == 'Logoff-Player':
if username in self.onlineplayers:
self.onlineplayers[username].location = 'Login'
del self.onlineplayers[username]
elif msg == 'Try-Logon':
if username not in self.onlineplayers:
self.server.sendMessage(self.s, username, "Can-Login")
else:
self.server.sendMessage(self.s, username, "Cannot-Login")
''' ignore messages from users who aren't poker players '''
if username in self.onlineplayers:
if msg == 'Sit-Table':
self.onlineplayers[username].location = 'Table'
self.server.sendMessage(self.s, username, "The game will start when a player")
self.server.sendMessage(self.s, username, "clicks the 'Start game' button.")
elif msg == 'Leave-Table':
self.onlineplayers[username].location = 'Open'
elif msg == 'Start-Game':
for plyrobj in self.tableplayers:
plyrobj.location = 'Game'
self.gameon = True
for plyrobj in self.openplayers:
self.giveHomeData(plyrobj)
self.initiateGame()
elif msg == 'Game-Done':
for plyrobj in self.gameplayers:
plyrobj.location = 'Table'
self.gameon = False
def locatePlayers(self):
''' assorts each online player to a list depending on their location '''
self.openplayers = []
self.tableplayers = []
self.gameplayers = []
for name in self.onlineplayers:
plyrobj = self.onlineplayers[name]
location = plyrobj.location
if location == 'Open':
self.openplayers.append(plyrobj)
elif location == 'Table':
self.tableplayers.append(plyrobj)
elif location == 'Game':
self.gameplayers.append(plyrobj)
else:
if name in self.onlineplayers:
del self.onlineplayers[name]
def initiateGame(self):
''' starts game for players on table and creates Toplevel as Game instance '''
self.gamewnd = Toplevel(self.wnd)
self.gamewnd.geometry('150x150')
self.game = Game(self.gamewnd, self.s, self.server, self.tableplayers)
def giveHomeData(self, plyrobj):
''' sends message with new data to all friends whenever there are updates on gui side '''
plyrname = plyrobj.playername
data = ['homexx']
data += ['location']
data += [plyrobj.location]
data += ['onlineusers']
''' sends names of online users '''
for name in self.onlineplayers:
data.append(name)
data += ['canplay']
''' sends true/false if user can play '''
data += [str(self.canPlay(plyrobj))]
data += ['myinfo']
''' sends name of player and stack size '''
for j in [plyrobj.playername, plyrobj.stack]:
data += [str(j)]
data += ['tablespace']
space = 9 - len(self.tableplayers)
data += [str(space)]
data += ['tableplyrs']
for obj in self.tableplayers:
data.append(obj.playername)
data += ['gameon']
data += [str(self.gameon)]
data += ['END']
dstring = '-'.join(data)
self.server.sendMessage(self.s, plyrname, dstring)
def giveTableData(self, plyrobj):
''' sends message with new data to all friends whenever there are updates on gui side of table'''
plyrname = plyrobj.playername
data = ['tablexx']
data += ['location']
data += [plyrobj.location]
data += ['tableplyrs']
for v in self.tableplayers:
data.append(v.playername)
data += ['gameon']
data += [str(self.gameon)]
data += ['canplay']
data += [str(self.canPlay(plyrobj))]
data += ['END']
dstring = '-'.join(data)
self.server.sendMessage(self.s, plyrname, dstring)
def canPlay(self, plyrobj):
if plyrobj.stack > 100:
return True
return False
class Game:
def __init__(self, window, socket, server, players):
self.wnd = window
self.createWindow()
self.s = socket # socket connection
self.server = server # ChatProgram object acting as server
self.themail = [] # newest mail received
self.tableplayers = players # list of all player objs on table during game
self.activeplayers = [] # list of all active player objs in game
self.playercntr = 0 # counts players for rounds
self.iswinner = False # Boolean for having winner
self.currentplyrobj = None # Player obj of current player
self.possibleacts = [] # list of possible acts for current player
self.deck = Deck() # creates Deck object
self.deck.shuffleDeck() # shuffles Deck obj
self.pot = 0
self.communitycards = []
self.street = '' # street Game is in
self.maxbet = 0 # current maxbet of street
self.dealerCounter = 0
self.dealer = None
self.whoDealer(self.dealerCounter, self.tableplayers)
self.smallblind = 10
self.bigblind = 20
self.smallblinder = None
self.whoSmallblind(self.dealerCounter, self.tableplayers)
self.bigblinder = None
self.whoBigblind(self.dealerCounter, self.tableplayers)
self.pushMsg('Game started.')
self.game_Blinds()
''' specific functions '''
def createWindow(self):
self.wnd.geometry('150x150')
self.wnd.title('Poker -- Server -- Game')
lbl = Label(self.wnd, text='Game in progress.')
lbl.pack()
def whoDealer(self, counter, players):
''' checks which player is dealer '''
dealer = players[counter]
dealer.dealer = True
counter += 1
counter %= len(players)
self.dealer = dealer
def whoSmallblind(self, counter, players):
smallblinder = players[counter+1]
smallblinder.smallblind = True
self.smallblinder = smallblinder
def whoBigblind(self, counter, players):
bigblinder = players[counter+2]
bigblinder.bigblind = True
self.bigblinder = bigblinder
''' action functions '''
def checked(self, player):
self.pushMsg('[all]>> %s checked.' % player.playername)
def called(self, player):
thebet = self.maxbet - player.bet
player.bet += thebet
player.stack -= thebet
self.pot += thebet
self.pushMsg('[all]>> %s called.' % player.playername)
def betted(self, player, betamt):
thebet = betamt
player.bet = thebet
self.maxbet = thebet
self.pot += thebet
player.stack -= thebet
self.pushMsg('[all]>> %s bet %s.' % (player.playername, betamt))
def raised(self, player, raiseamt):
thebet = raiseamt
player.bet += thebet
self.maxbet = player.bet
player.stack -= thebet
self.pot += thebet
self.pushMsg('[all]>> %s raised by %s.' % (player.playername, raiseamt))
def folded(self, player):
self.activeplayers.remove(player)
self.pushMsg('[all]>> %s folded.' % player.playername)
''' game functions '''
def game_Blinds(self):
self.street = 'blinds'
print 'We reached the blinds phase.'
for player in self.tableplayers:
player.holecards.append(self.deck.takeTopCard())
player.holecards.append(self.deck.takeTopCard())
string = '[%s]>> Your holecards are: %s.' % (player.playername, player.holecards)
self.sendMsg(player.playername, string)
if player == self.smallblinder:
player.bet = self.smallblind
player.stack -= self.smallblind
self.pot += self.smallblind
self.pushMsg('[all]>> %s paid the small blind of %s.' % (player.playername, self.smallblind))
elif player == self.bigblinder:
player.bet = self.bigblind
player.stack -= self.bigblind
self.pot += self.bigblind
self.maxbet = self.bigblind
self.pushMsg('[all]>> %s paid the big blind of %s.' % (player.playername, self.bigblind))
''' turns all players into active players '''
for i in self.tableplayers[3:]:
self.activeplayers.append(i)
for i in self.tableplayers[:3]:
self.activeplayers.append(i)
self.game_Preflop()
def game_Preflop(self):
self.street = 'preflop'
print 'We reached the preflop phase.'
self.playercntr = 0
while self.roundOn():
self.processSt()
self.reInitialize()
self.game_Flop()
def game_Flop(self):
if self.iswinner == True:
return
self.beginSt('flop')
while self.roundOn():
self.processSt()
self.reInitialize()
self.game_Turn()
def game_Turn(self):
if self.iswinner == True:
return
self.beginSt('turn')
while self.roundOn():
self.processSt()
self.reInitialize()
self.game_River()
def game_River(self):
if self.iswinner == True:
return
self.beginSt('river')
while self.roundOn():
self.processSt()
if self.iswinner == False:
winners = self.calcWinners()
self.win(winners)
''' game helper functions '''
def possibleActs(self):
if self.maxbet == 0:
return ['bet', 'check', 'fold']
elif self.maxbet == self.bigblind and self.currentplyrobj.bigblind and self.street == 'preflop':
return ['check', 'raise', 'fold']
else:
return ['call', 'raise', 'fold']
def beginSt(self, st):
print 'We reached the %s.' % st
if self.iswinner == False:
self.street = st
self.maxbet = 0
self.playercntr = 0
self.communitycards.append(self.deck.takeTopCard())
if st == 'flop':
for i in range(2):
self.communitycards.append(self.deck.takeTopCard())
msg = '[all]>> The community cards after the %s are %s.' % (self.street, self.communitycards)
self.pushMsg(msg)
def roundOn(self):
if len(self.activeplayers) == 1:
self.win([self.activeplayers[0]])
return False
for i in self.activeplayers:
if i.bet != self.maxbet or i.roundturns == 0:
print 'The round is on.'
return True
return False
def processSt(self):
player = self.activeplayers[self.playercntr] # get player object
self.currentplyrobj = player
self.possibleacts = self.possibleActs()
player.yourturn = True
for obj in self.tableplayers:
self.giveGameData(obj)
lets.timer1()
print "It is %s's turn." % player.playername
str1 = '[%s]>> Your holecards are %s.' % (player.playername, player.holecards)
self.sendMsg(player.playername, str1)
str2 = '[%s]>> You can do the following %s.' % (player.playername, self.possibleacts)
self.sendMsg(player.playername, str2)
act = self.getResponse()
while (act == None):
act = self.getResponse()
if act == 'check':
self.checked(player)
elif act == 'call':
self.called(player)
elif act =='bet':
betstr = '[%s]>> You want to bet: ' % player.playername
self.sendMsg(player.playername, betstr)
betamt = self.getIntResponse()
while (betamt == None):
betamt = self.getIntResponse()
self.betted(player, betamt)
elif act == 'raise':
raisestr = '[%s]>> You want to raise by: ' % player.playername
self.sendMsg(player.playername, raisestr)
raiseamt = self.getIntResponse()
while (raiseamt == None):
raiseamt = self.getIntResponse()
self.raised(player, raiseamt)
elif act == 'fold':
self.folded(player)
player.roundturns += 1
player.yourturn = False
if act != 'fold':
self.playercntr += 1
self.playercntr %= len(self.activeplayers)
def reInitialize(self):
''' reinitializing variables for active players '''
for i in self.activeplayers:
i.bet = 0
i.roundturns = 0
''' winning functions '''
def win(self, winners):
self.iswinner = True
for i in winners:
winnings = float(self.pot)/len(winners)
i.stack += winnings
for k in self.tableplayers:
str1 = '[all]>> ' + i.playername + ' won: ' + str(winnings)
self.sendMsg(k.playername, str1)
if self.street == 'river':
str2 = '[all]>> ' + i.playername + "'s hand was: " + ', '.join(i.besthand)
self.sendMsg(k.playername, str2)
for j in self.tableplayers:
j.dealer = False
j.smallblind = False
j.bigblind = False
j.bet = 0
j.roundturns = 0
j.result = 11
j.besthand = ()
self.pushMsg('Game done.')
print 'The game is done.'
lets.timer1()
self.wnd.destroy()
def getAllHands(self, player):
''' returns list of all possible hands with community and hole cards '''
allcards = player.holecards + self.communitycards
return list(itertools.combinations(allcards, 5))
def getBestHand(self, player):
''' returns strength of player's hand '''
allhands = self.getAllHands(player)
counter = 11
saved = ()
for hand in allhands:
eve = HandEvaluator(list(hand))
pos = eve.evaluate()
if pos < counter:
counter = pos
saved = hand
return counter, saved
def calcWinners(self):
''' calculates who are the winners, returns as list '''
winners = []
best = 11
for plyr in self.activeplayers:
plyr.result, plyr.besthand = self.getBestHand(plyr)
if plyr.result <= best:
best = plyr.result
winners.append(plyr)
for plyr in winners:
if plyr.result != best:
winners.remove(plyr)
return winners
''' general functions '''
def pushMsg(self, message):
for plyrobj in self.tableplayers:
plyrname = plyrobj.playername
self.server.sendMessage(self.s, plyrname, message)
def sendMsg(self, plyrname, message):
self.server.sendMessage(self.s, plyrname, message)
def getResponse(self):
plyrname = self.currentplyrobj.playername
gotit = False
recvsmt = False
response = ''
self.themail = self.server.getMail(self.s)
for t in self.themail:
user, msg = t[0], t[1]
if user == plyrname:
if msg in self.possibleacts:
response = msg
gotit = True
else:
recvsmt = True
if gotit:
if (response == 'call' or response == 'raise') and self.maxbet > self.currentplyrobj.stack:
str1 = '[%s]>> That is not a possibility.' % plyrname
self.sendMsg(plyrname, str1)
else:
return response
else:
if recvsmt:
str1 = '[%s]>> That is not a possibility.' % plyrname
self.sendMsg(plyrname, str1)
str2 = '[%s]>> You can do the following %s.' % (plyrname, self.possibleacts)
self.sendMsg(plyrname, str2)
def getIntResponse(self):
plyrname = self.currentplyrobj.playername
ints = range(int(self.currentplyrobj.stack)+1)
for nr in range(len(ints)):
ints[nr] = str(ints[nr])
gotit = False
recvsmt = False
response = ''
self.themail = self.server.getMail(self.s)
for t in self.themail:
user, msg = t[0], t[1]
if user == plyrname:
if msg in ints:
response = msg
gotit = True
else:
recvsmt = True
if gotit:
return int(response)
else:
if recvsmt:
str1 = '[%s]>> That is not a possibility.' % plyrname
self.sendMsg(plyrname, str1)
str2 = '[%s]>> Please enter a value: ' % plyrname
self.sendMsg(plyrname, str2)
def giveGameData(self, playerobj):
plyrname = playerobj.playername
data = ['gamexx']
data += ['location']
data += [playerobj.location]
data += ['street']
data += [self.street]
data += ['activeplayers']
if self.street == '' or self.street == 'blinds':
for v in self.tableplayers:
data.append(v.playername)
else:
for v in self.activeplayers:
data.append(v.playername)
data += ['gameinfo']
for i in [self.maxbet, playerobj.bet, self.pot, playerobj.stack]:
data += [str(i)]
data += ['holecards']
for j in playerobj.holecards:
data += [j]
data += ['yourturn']
data += [str(playerobj.yourturn)]
data += ['possibleacts']
for l in self.possibleacts:
data += [l]
data += ['communitycards']
for k in self.communitycards:
data += [k]
data += ['END']
dstring = '-'.join(data)
self.sendMsg(plyrname, dstring)
class HandEvaluator:
''' returns value that represents strength of hand '''
def __init__(self, hand):
self.allcards = hand
self.dec = 0.99
self.vs = {'A':0.01, 'K':0.02, 'Q':0.03, 'J':0.04, '10':0.05, '9':0.06, '8':0.07, '7':0.08, \
'6':0.09, '5':0.1, '4':0.11, '3':0.12, '2':0.13}
self.values = []
self.onlyvalues = []
self.valueCount = 6
self.getValueCount()
self.suits = []
self.suitCount = 6
self.getSuitCount()
self.isStraight = False
self.checkStraight()
self.isFlush = False
self.checkFlush()
def getValueCount(self):
''' returns number of different values of hand '''
for i in self.allcards:
if len(i) != 3:
self.onlyvalues.append(i[0])
else:
self.onlyvalues.append(i[0:2])
self.values = list(set(self.onlyvalues))
self.valueCount = len(self.values)
def getSuitCount(self):
''' returns number of different suits of hand '''
for i in self.allcards:
if len(i) != 3:
self.suits.append(i[1])
else:
self.suits.append(i[2])
self.suits = list(set(self.suits))
self.suitCount = len(self.suits)
def checkStraight(self):
''' checks if hand is a straight '''
if self.valueCount == 5:
realvalues = []
for i in self.values:
''' turn J, Q, K, A into number '''
if i == 'J':
i = '11'
elif i == 'Q':
i = '12'
elif i == 'K':
i = '13'
elif i == 'A':
i = '14'
realvalues.append(int(i))
realvalues.sort()
if realvalues == range(realvalues[0], realvalues[0]+5):
self.isStraight = True
return
elif realvalues == (range(realvalues[0], realvalues[0]+4) + [14]):
self.isStraight = True
return
self.isStraight = False
return
self.isStraight = False
return
def checkFlush(self):
if self.suitCount == 1:
self.isFlush = True
return
self.isFlush = False
return
def checkRoyalFlush(self):
if self.isFlush and self.isStraight:
royal = ['A', 'K', 'Q', 'J', '10']
for i in royal:
if i not in self.values:
return False
return True
return False
def checkStraightFlush(self):
if self.isFlush and self.isStraight:
self.getDec('hi1')
return True
return False
def checkFourKind(self):
if len(self.values) == 2:
if self.onlyvalues.count(self.values[0]) == 4:
return True
elif self.onlyvalues.count(self.values[1]) == 4:
return True
return False
return False
def checkFullHouse(self):
if len(self.values) == 2:
if not self.checkFourKind():
return True
return False
return False
def checkThreeKind(self):
if len(self.values) == 3:
if self.onlyvalues.count(self.values[0]) == 3:
return True
elif self.onlyvalues.count(self.values[1]) == 3:
return True
elif self.onlyvalues.count(self.values[2]) == 3:
return True
return False
return False
def checkTwoPair(self):
if len(self.values) == 3:
if not self.checkThreeKind():
return True
return False
return False
def checkPair(self):
if len(self.values) == 4:
return True
return False
def getDec(self, kind):
if kind == 'hi1':
for val in self.values:
if self.vs[val] < self.dec:
value = val
if self.isStraight:
if value == 'A' and '2' in self.values:
value = '5'
self.dec = self.vs[value]
else:
''' for high card or flush cases'''
dec1 = 0.99
val1 = None
restvals1 = self.values
restvals1.remove(value)
for val in restvals1:
if self.vs[val] < dec1:
dec1 = self.vs[val]
val1 = val
dec2 = 0.99
val2 = None
restvals2 = restvals1
restvals2.remove(val1)
for val in restvals2:
if self.vs[val] < dec2:
dec2 = self.vs[val]
val2 = val
dec3 = 0.99
val3 = None
restvals3 = restvals2
restvals3.remove(val2)
for val in restvals3:
if self.vs[val] < dec3:
dec3 = self.vs[val]
val3 = val
restvals3.remove(val3)
val4 = restvals3[0]
dec4 = self.vs[val4]
self.dec = self.vs[value] + (dec1 * 0.01) + (dec2 * 0.0001) + (dec3 * 0.000001) + (dec4 * 0.00000001)
elif kind == 'pick4':
cntr0, cntr1 = 0, 0
for card in self.allcards:
if self.values[0] in card:
cntr0 += 1
else:
cntr1 += 1
if cntr0 > cntr1:
val = self.values[0]
otherval = self.values[1]
else:
val = self.values[1]
otherval = self.values[0]
self.dec = self.vs[val] + (self.vs[otherval] * 0.01)
elif kind == 'pick3':
cntr0, cntr1, cntr2 = 0, 0, 0
for card in self.allcards:
if self.values[0] in card:
cntr0 += 1
elif self.values[1] in card:
cntr1 += 1
else:
cntr2 += 1
if cntr0 == 3:
val = self.values[0]
if self.vs[self.values[1]] < self.vs[self.values[2]]:
otherval1 = self.values[1]
otherval2 = self.values[2]
else:
otherval1 = self.values[2]
otherval2 = self.values[1]
elif cntr1 == 3:
val = self.values[1]
if self.vs[self.values[2]] < self.vs[self.values[0]]:
otherval1 = self.values[2]
otherval2 = self.values[0]
else:
otherval1 = self.values[0]
otherval2 = self.values[2]
else:
val = self.values[2]
if self.vs[self.values[1]] < self.vs[self.values[0]]:
otherval1 = self.values[1]
otherval2 = self.values[0]
else:
otherval1 = self.values[0]
otherval2 = self.values[1]
self.dec = self.vs[val] + (self.vs[otherval1] * 0.01) + (self.vs[otherval2] * 0.0001)
elif kind == 'pick2':
cntr0, cntr1, cntr2 = 0, 0, 0
for card in self.allcards:
if self.values[0] in card:
cntr0 += 1
elif self.values[1] in card:
cntr1 += 1
else:
cntr2 += 1
if cntr0 == cntr1:
comp = (self.values[0], self.values[1])
otherval = self.values[2]
elif cntr0 == cntr2:
comp = (self.values[0], self.values[2])
otherval = self.values[1]
else:
comp = (self.values[1], self.values[2])
otherval = self.values[0]
if self.vs[comp[0]] < self.vs[comp[1]]:
val1 = comp[0]
val2 = comp[1]
else:
val1 = comp[1]
val2 = comp[0]
self.dec = self.vs[val1] + (self.vs[val2] * 0.01) + (self.vs[otherval] * 0.0001)
elif kind == 'pickpair':
cntr0, cntr1, cntr2, cntr3 = 0, 0, 0, 0
for card in self.allcards:
if self.values[0] in card:
cntr0 += 1
elif self.values[1] in card:
cntr1 += 1
elif self.values[2] in card: