-
Notifications
You must be signed in to change notification settings - Fork 0
/
BangGame.py
957 lines (911 loc) · 40.3 KB
/
BangGame.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
#Bang Game
from AlphaBetaAI import AlphaBetaAI
from BoardClass import BoardsBang
from PlayerClass import PlayerBang
from DeckClass import Deck
class BangGame:
#takes in number of players and sets up deck, players, and boards
def __init__(self, np, playerType = None):
self.played = False
self.players = []
self.alphaBetaPlayers = []
self.guns = ["volcanic","schofield","remington","carabine","winchester"]
self.meStatus = ["barrel","dynamite","mustang","scope"]
self.ply = 10 #Stores depth for A-B search
self.numPlayers = np
self.deck = Deck()
self.boards = BoardsBang(np)
i = 0
while i < np:
h = []
r = self.boards.showRole(i,True)
h.append(self.deck.draw())
h.append(self.deck.draw())
h.append(self.deck.draw())
h.append(self.deck.draw())
if r == "Sheriff":
h.append(self.deck.draw())
if playerType == None:
print("What type of player is player ", i)
t = input("Your choices are human, dumbAI, or ai: ")
while t != "human" and t != "ai" and t != "dumbAI":
print("invalid input")
t = input("Your choices are human, dumbAI, or ai: ")
else:
t = playerType
p = PlayerBang(i,r,h,t)
self.players.append(p)
#Make the artificial players as needed
if t == "ai":
self.alphaBetaPlayers.append( AlphaBetaAI(np, i, r) )
else:
self.alphaBetaPlayers.append(0)
i = i+1
#plays a game of Bang
def Play(self):
while self.played == False:
self.played = True
for player in self.players:
if self.boards.showRole(player.getPlayerNum(),True) == "Sheriff":
turn = player.getPlayerNum()
w = self.boards.Winner()
while w == None:
if self.players[turn].getType() == "dumbAI": #AI player
self.DumbAITurn(turn)
elif self.players[turn].getType() == "ai": #AI player
self.AlphaBetaAITurn(turn)
elif self.players[turn].getType() == "human":#do nothing
self.HumanTurn(turn)
turn = turn+1
if turn == self.numPlayers: #circle back
turn = 0
w = self.boards.Winner()
print("\nWinner:",w)
#self.askPlayAgain()
return w
#plays an AI turn for player pNum
def DumbAITurn(self,pNum):
#check can play
if self.boards.canPlay(pNum) == False:
print("/nPlayer",pNum,"is dead")
return None
print("\nPlayer ",pNum,"'s Turn")
if self.boards.getHealth(pNum) > 0:
print("Your health: ",self.boards.getHealth(pNum))
self.boards.displayBoard(pNum)
print("Your role: ",self.players[pNum].getRole())
else:
print("Player ",pNum," is dead!")
#check dynamite
if self.checkDynamite(pNum) == False:
return None
#check jail
if self.checkJail(pNum) == False:
return None
#draw 2 cards
self.startDraw(pNum)
#play cards
for i in range(self.numPlayers):
print("Player",i,"health =",self.boards.getHealth(i))
print("\n")
toDiscard = []
hand = self.players[pNum].retHand()
volcanic = self.boards.gunIsVolcanic(pNum)
hasShot = False
cNum = 0
while cNum in range(len(hand)):
if hand[cNum].getCard() in self.guns:
c = self.boards.removeGun(pNum)
if c != False:
self.deck.discard(c)
self.boards.playGun(pNum,hand[cNum])
toDiscard.append(hand[cNum])
print("Player",pNum,"played ",hand[cNum].getCard())
elif hand[cNum].getCard() in self.meStatus:
name = hand[cNum].getCard()
#mustang
if name == "mustang" and self.boards.hasMustang(pNum) == False:
print("Player",pNum,"played Mustang")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#barrel
elif name == "barrel" and self.boards.hasBarrel(pNum) == False:
print("Player",pNum,"played Barrel")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#scope
elif name == "scope" and self.boards.hasScope(pNum) == False:
print("Player",pNum,"played scope")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#dynamite
elif name == "dynamite" and self.boards.hasMustang(pNum) == False:
print("Player",pNum,"played dynamite")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#Bang!
elif hand[cNum].getCard() == "bang":
if volcanic == True or hasShot == False:
self.shootOne(pNum,hand[cNum])
hasShot = True
#beer or saloon
elif hand[cNum].getCard() == "beer" or hand[cNum].getCard() == "saloon":
if self.boards.showRole(pNum,True) == "sheriff":
if self.boards.getHealth(pNum) < 5:
self.boards.increaseHealth(pNum)
toDiscard.append(hand[cNum])
if hand[cNum].getCard == "saloon":
for player in range(self.numPlayers-1):
if player != pNum:
self.boards.increaseHealth(player)
else:
if self.boards.getHealth(pNum) < 4:
self.boards.increaseHealth(pNum)
toDiscard.append(hand[cNum])
if hand[cNum].getCard == "saloon":
for player in range(self.numPlayers-1):
if player != pNum:
self.boards.increaseHealth(player)
#duel
elif hand[cNum].getCard() == "duel":
self.duel(pNum,hand[cNum])
#gatling
elif hand[cNum].getCard() == "gatling":
self.shootAll(pNum,False,hand[cNum])
#indians
elif hand[cNum].getCard() == "indians":
self.shootAll(pNum,True,hand[cNum])
#panic
elif hand[cNum].getCard() == "panic":
if self.panicCatBalou(pNum,True,hand[cNum]) == True:
toDiscard.append(hand[cNum])
#cat balou
elif hand[cNum].getCard() == "cat balou":
if self.panicCatBalou(pNum,False,hand[cNum]) == True:
toDiscard.append(hand[cNum])
#jail
elif hand[cNum].getCard() == "jail":
opp = pNum+1
if opp == self.numPlayers:
opp = 0
while self.boards.canJail(opp) == False or self.boards.canPlay(opp) == False:
opp += 1
if opp == self.numPlayers:
opp = 0
if opp != pNum:
self.boards.playStatus(opp,hand[cNum])
toDiscard.append(hand[cNum])
#general store
elif hand[cNum].getCard() == "general store":
self.generalStore(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#stagecoach
elif hand[cNum].getCard() == "stagecoach":
print("Played Stagecoach")
c = [self.deck.draw(),self.deck.draw()]
self.players[pNum].addToHand(c)
toDiscard.append(hand[cNum])
#wells fargo
elif hand[cNum].getCard() == "wells fargo":
print("Played Wells Fargo")
c = [self.deck.draw(),self.deck.draw(),self.deck.draw()]
self.players[pNum].addToHand(c)
toDiscard.append(hand[cNum])
hand = self.players[pNum].retHand()
volcanic = self.boards.gunIsVolcanic(pNum)
cNum += 1
for card in toDiscard:
h = self.players[pNum].retHand()
found = False
i = 0
while found == False and i != len(h):
if h[i] == card:
found = True
c = self.players[pNum].getFromHand(i)
self.deck.discard(c)
i += 1
self.discardExtra(pNum)
return None
#plays an Alpha-Beta AI
def AlphaBetaAITurn(self,pNum):
#check can play
if self.boards.canPlay(pNum) == False:
print("\nPlayer",pNum,"is dead")
return None
print("\nPlayer ",pNum,"'s Turn")
if self.boards.getHealth(pNum) > 0:
print("Your health: ",self.boards.getHealth(pNum))
self.boards.displayBoard(pNum)
print("Your role: ",self.players[pNum].getRole())
else:
print("Player ",pNum," is dead!")
#check dynamite
if self.checkDynamite(pNum) == False:
return None
#check jail
if self.checkJail(pNum) == False:
return None
#draw 2 cards
self.startDraw(pNum)
#play cards
for i in range(self.numPlayers):
print("Player ",i," health = ",self.boards.getHealth(i))
print("\n")
toDiscard = []
hand = self.players[pNum].retHand()
volcanic = self.boards.gunIsVolcanic(pNum)
hasShot = False
cNum = 0
#Go through cards playing non-bangs as needed
while cNum in range(len(hand)):
#Play guns unless volcanic
if hand[cNum].getCard() in self.guns and hand[cNum].getCard() != "volcanic":
c = self.boards.removeGun(pNum)
if c != False:
self.deck.discard(c)
self.boards.playGun(pNum,hand[cNum])
toDiscard.append(hand[cNum])
print("Player",pNum,"played ",hand[cNum].getCard())
#Play status cards
elif hand[cNum].getCard() in self.meStatus:
name = hand[cNum].getCard()
#mustang
if name == "mustang" and self.boards.hasMustang(pNum) == False:
print("Player",pNum,"played Mustang")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#barrel
elif name == "barrel" and self.boards.hasBarrel(pNum) == False:
print("Player",pNum,"played Barrel")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#scope
elif name == "scope" and self.boards.hasScope(pNum) == False:
print("Player",pNum,"played scope")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#dynamite
elif name == "dynamite" and self.boards.hasMustang(pNum) == False:
print("Player",pNum,"played dynamite")
self.boards.playStatus(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#beer or saloon
elif hand[cNum].getCard() == "beer" or hand[cNum].getCard() == "saloon":
if self.boards.showRole(pNum,True) == "sheriff":
if self.boards.getHealth(pNum) < 5:
self.boards.increaseHealth(pNum)
toDiscard.append(hand[cNum])
if hand[cNum].getCard == "saloon":
for player in range(self.numPlayers-1):
if player != pNum:
self.boards.increaseHealth(player)
else:
if self.boards.getHealth(pNum) < 4:
self.boards.increaseHealth(pNum)
toDiscard.append(hand[cNum])
if hand[cNum].getCard == "saloon":
for player in range(self.numPlayers-1):
if player != pNum:
self.boards.increaseHealth(player)
#duel
elif hand[cNum].getCard() == "duel":
self.duel(pNum,hand[cNum])
#gatling
elif hand[cNum].getCard() == "gatling":
self.shootAll(pNum,False,hand[cNum])
#indians
elif hand[cNum].getCard() == "indians":
self.shootAll(pNum,True,hand[cNum])
#panic
elif hand[cNum].getCard() == "panic":
if self.panicCatBalou(pNum,True,hand[cNum]) == True:
toDiscard.append(hand[cNum])
#cat balou
elif hand[cNum].getCard() == "cat balou":
if self.panicCatBalou(pNum,False,hand[cNum]) == True:
toDiscard.append(hand[cNum])
#jail
elif hand[cNum].getCard() == "jail":
opp = pNum+1
if opp == self.numPlayers:
opp = 0
while self.boards.canJail(opp) == False or self.boards.canPlay(opp) == False:
opp += 1
if opp == self.numPlayers:
opp = 0
if opp != pNum:
self.boards.playStatus(opp,hand[cNum])
toDiscard.append(hand[cNum])
#general store
elif hand[cNum].getCard() == "general store":
self.generalStore(pNum,hand[cNum])
toDiscard.append(hand[cNum])
#stagecoach
elif hand[cNum].getCard() == "stagecoach":
print("Played Stagecoach")
c = [self.deck.draw(),self.deck.draw()]
self.players[pNum].addToHand(c)
toDiscard.append(hand[cNum])
#wells fargo
elif hand[cNum].getCard() == "wells fargo":
print("Played Wells Fargo")
c = [self.deck.draw(),self.deck.draw(),self.deck.draw()]
self.players[pNum].addToHand(c)
toDiscard.append(hand[cNum])
hand = self.players[pNum].retHand()
volcanic = self.boards.gunIsVolcanic(pNum)
cNum += 1
#BANG!!
#Determine who to shoot
for cNum in range(len(hand)):
if hand[cNum].getCard() == "bang":
if volcanic == True or hasShot == False:
self.shootOne(pNum, hand[cNum])
hasShot = True
#Discard cards
for card in toDiscard:
h = self.players[pNum].retHand()
found = False
i = 0
while found == False and i != len(h):
if h[i] == card:
found = True
c = self.players[pNum].getFromHand(i)
self.deck.discard(c)
i += 1
self.discardExtra(pNum)
return None
#plays a human turn for player pNum
def HumanTurn(self,pNum):
#check can play
if self.boards.canPlay(pNum) == False:
return None
print("\nPlayer ",pNum,"'s Turn")
if self.boards.getHealth(pNum) > 0:
print("Player ",pNum,"''s health:",self.boards.getHealth(pNum))
self.boards.displayBoard(pNum)
print("Your role: ",self.players[pNum].getRole())
print("Who is alive:")
for playerNum in range(self.numPlayers):
if self.boards.getHealth(playerNum) > 0:
print("Player ",playerNum)
else:
print("Player ",pNum," is dead!")
#check dynamite
if self.checkDynamite(pNum) == False:
return None
#check jail
if self.checkJail(pNum) == False:
return None
#draw 2 cards
self.startDraw(pNum)
#play cards
canPlayBang = True
while self.players[pNum].handSize() > 0 and self.boards.canPlay(pNum):
result = False
while result == False:
cnum = self.humanPickCard(pNum)
if cnum < 0:
self.discardExtra(pNum)
return None
else:
c = self.players[pNum].getFromHand(cnum)
result = self.humanPlayCard(pNum,c,canPlayBang)
if result == True and c.getCard() == "bang":
canPlayBang = False
if result == False:
self.players[pNum].addOneToHand(c)
self.discardExtra(pNum)
return None
def humanPlayCard(self,pNum,c,canPlayBang):
cname = c.getCard()
if cname in self.guns:
if self.boards.playGun(pNum,c) == False:
print("error")
return False
else:
c = self.boards.removeGun(pNum)
self.boards.playGun(pNum,c)
self.deck.discard(c)
elif cname in self.meStatus:
if self.boards.playStatus(pNum,c) == False:
print("you already have one of those")
return False
elif cname == "bang":
if canPlayBang == True or self.boards.gunIsVolcanic(pNum):
print("attempting shot")
if self.shootOne(pNum,c) == False:
print("can't shoot that person!")
return False #Couldn't shoot that person
return True
print("You may use a Bang once per turn")
return False
elif cname == "duel":
if self.duel(pNum,c) == False:
return False
elif cname == "panic":
if self.panicCatBalou(pNum,True,c) == False:
return False
elif cname == "cat balou":
if self.panicCatBalou(pNum,False,c) == False:
return False
elif cname == "jail":
opp = int(input("which player would you like to put in jail: "))
if self.boards.canJail(opp) == True:
if self.boards.playStatus(opp,c) == False:
print("They are already in Jail")
elif cname == "indians":
self.shootAll(pNum,True,c)
elif cname == "gatling":
self.shootAll(pNum,False,c)
elif cname == "general store":
self.generalStore(pNum,c)
elif cname == "wells fargo":
draw = [self.deck.draw(),self.deck.draw(),self.deck.draw()]
self.players[pNum].addToHand(draw)
elif cname == "stagecoach":
draw = [self.deck.draw(),self.deck.draw()]
self.players[pNum].addToHand(draw)
elif cname == "beer":
if self.boards.increaseHealth(pNum) == False:
print("your health is already full")
return False
elif cname == "saloon":
for i in range(self.numPlayers):
self.boards.increaseHealth(pNum)
elif cname == "missed":
print("you cant play a 'missed' now")
return False
else:
return False
return True
def duel(self,pNum,c):
if self.players[pNum].getType() == "human":
opp = int(input("who would you like to duel: "))
while opp == pNum or opp >= self.numPlayers or opp < 0:
print("Invalid player Number (enter -1 to retract card")
opp = int(input("Please pick another Player: "))
if opp == -1:
return False
self.deck.discard(c)
again = False
while again == False:
if self.bangResponse(opp) == False:
return True
if self.bangResponse(pNum) == False:
return True
elif self.players[pNum].getType() == "dumbAI":
opp = pNum+1
if opp == self.numPlayers:
opp = 0
while self.boards.canPlay(opp) == False:
opp += 1
if opp == self.numPlayers:
opp = 0
print("Player",pNum,"will duel",opp)
again = False
while again == False:
if self.bangResponse(opp) == False:
return True
if self.bangResponse(pNum) == False:
return True
def panicCatBalou(self,pNum,panic,c):
if panic:
cp = False
if self.players[pNum].getType() == "human":
while cp == False:
opp = int(input("Who would you like to panic: "))
if opp == -1:
return False
while opp == pNum or opp >= self.numPlayers or opp < 0:
print("Invalid player Number (enter -1 to retract card)")
opp = int(input("Please pick another Player: "))
if opp == -1:
return False
cp = self.boards.canPanic(pNum,opp)
if cp == False:
print("That player is out of range (enter -1 to retract card) ")
print("Please pick another Player")
self.boards.displayBoard(opp)
steal = int(input("Which card would you like to steal: "))
c2 = self.boards.removeStatus(opp,steal)
if c2 == False:
c2 = self.boards.removeGun(opp)
if c2 == False:
stealable = False
else:
stealable = True
while stealable == False:
print("Invalid card (enter -1 to retract card)")
steal = int(input("Please pick another card to steal: "))
if steal == -1:
return False
if steal != "colt.45":
stealable = True
c2 = self.boards.removeStatus(opp,steal)
if c2 == False:
c2 = self.boards.removeGun(opp)
if c2 == False:
stealable = False
else:
stealable = True
self.players[pNum].addOneToHand(c2)
self.deck.discard(c)
return True
elif self.players[pNum].getType() == "dumbAI":
opp = pNum+1
if opp == self.numPlayers:
opp = 0
while self.boards.canPlay(opp) == False:
opp += 1
if opp == self.numPlayers:
opp = 0
if self.boards.hasMustang(opp) == True:
if self.boards.hasScope(pNum) == True:
#panic
c1 = self.boards.removeStatus(opp,"barrel")
if c1 == False:
c1 = self.boards.removeStatus(opp,"scope")
if c1 == False:
self.boards.removeStatus(opp,"dynamite")
if c1 == False:
self.boards.removeStatus(opp,"jail")
if c1 == False:
self.boards.removeGun(opp)
if c1 == False:
return False;
print("Player ",pNum," stole ",c1.getCard()," from player ",opp)
self.players[pNum].addOneToHand(c1)
return True
else:
return False
else:
#panic
c1 = self.boards.removeStatus(opp,"barrel")
if c1 == False:
c1 = self.boards.removeStatus(opp,"scope")
if c1 == False:
self.boards.removeStatus(opp,"dynamite")
if c1 == False:
self.boards.removeStatus(opp,"jail")
if c1 == False:
c1 = self.boards.removeGun(opp)
if c1 == False:
return False;
print("Player ",pNum," stole ",c1.getCard()," from player ",opp)
self.players[pNum].addOneToHand(c1)
return True
else:
if self.players[pNum].getType == 'human':
opp = int(input("Who would you like to cat balou: "))
while opp == pNum or opp >= self.numPlayers or opp < 0:
print("Invalid player Number (enter -1 to retract card")
opp = int(input("Please pick another Player: "))
if opp == -1:
return False
self.boards.displayBoard(opp)
cb = int(input("Which card would you like to cat balou (-1 to retract card): "))
while cb < 0:
print("boom")
return False
c2 = self.boards.removeStatus(opp,cb)
if c2 == False:
c2 = self.boards.removeGun(opp)
if c2 == False:
cbable = False
else:
cbable = True
while cbable == False:
print("Invalid card (enter -1 to retract card) ")
cb = int(input("Please pick another card to cat balou: "))
if cb == -1:
return False
if cb != "colt.45":
cbable = True
c2 = self.boards.removeStatus(opp,cb)
if c2 == False:
c2 = self.boards.removeGun(opp)
if c2 == False:
cbable = False
else:
cbable = True
self.deck.discard(c)
self.deck.discard(c2)
return True
elif self.players[pNum].getType() == "dumbAI":
opp = pNum+1
if opp == self.numPlayers:
opp = 0
while self.boards.canPlay(opp) == False:
opp += 1
if opp == self.numPlayers:
opp = 0
#cat balou
c1 = self.boards.removeStatus(opp,"barrel")
if c1 == False:
c1 = self.boards.removeStatus(opp,"scope")
if c1 == False:
self.boards.removeStatus(opp,"dynamite")
if c1 == False:
self.boards.removeStatus(opp,"jail")
if c1 == False:
c1 = self.boards.removeGun(opp)
if c1 == False:
return False;
print("Player ",pNum," discarded ",c1.getCard()," from player ",opp)
self.deck.discard(c1)
return True
def generalStore(self,pNum,c):
print("Player",pNum,"played General Store")
self.deck.discard(c)
avail = []
numRemainingPlayers = self.numPlayers
for i in range(self.numPlayers):
if self.boards.getHealth(i) <= 0:
i -= 1
for i in range(numRemainingPlayers):
avail.append(self.deck.draw())
circle = False
start = pNum
loc = pNum
end = pNum-1
if end < 0:
end = self.numPlayers-1
while circle == False:
if self.players[loc].getType() == "human":
print("Player ",loc)
i = 0
for card in avail:
print(i,": ",card.getCard())
i = i+1
c = int(input("which card would you like to take: "))
while c not in range(len(avail)):
print("invalid card")
c = int(input("which card would you like to take: "))
choice = []
choice.append(avail[c])
avail.remove(choice[0])
self.players[loc].addToHand(choice)
if loc != start:
self.discardExtra(loc)
loc = loc+1
if loc == self.numPlayers:
loc = 0
if loc == start:
circle = True
else:
if self.boards.getHealth(loc) > 0:
choice = []
choice.append(avail[0])
avail.remove(choice[0])
self.players[loc].addToHand(choice)
if loc != start:
self.discardExtra(loc)
print("Player",loc,"took",choice[0].getCard())
loc = loc+1
if loc == self.numPlayers:
loc = 0
if loc == start:
circle = True
return True
def discardExtra(self,pNum):
if self.players[pNum].getType() == "human":
while self.players[pNum].handSize() > self.boards.checkHealth(pNum):
self.players[pNum].displayHand()
print("Your health is ",self.boards.getHealth(pNum)," so your hand size is too big!")
c = int(input("Which card would you like to discard? "))
card = self.players[pNum].getFromHand(c)
self.deck.discard(card)
return True
elif self.players[pNum].getType() == "dumbAI":
while self.players[pNum].handSize() > self.boards.checkHealth(pNum):
card = self.players[pNum].getFromHand(0)
self.deck.discard(card)
print("Player",pNum,"discarded",card.getCard())
return True
else:
#smart AI stuff
return True
def missedResponse(self,oppNum):
h = self.players[oppNum].retHand()
if self.players[oppNum].getType() == "human":
for c in h:
if c.getCard() == 'missed':
print("would player ",oppNum," like to play a missed?")
use = input("(y/n): ")
if use == "y":
self.players[oppNum].removeFromHand(c)
self.deck.discard(c)
return True
else:
self.boards.decreaseHealth(oppNum)
print("Player ",oppNum," was shot!")
return False
else:
for c in h:
if c.getCard() == 'missed':
self.players[oppNum].removeFromHand(c)
self.deck.discard(c)
print("Player",oppNum,"was missed")
return True
self.boards.decreaseHealth(oppNum)
print("Player ",oppNum," was shot!")
return False
def bangResponse(self,oppNum):
h = self.players[oppNum].retHand()
if self.players[oppNum].getType() == "human":
for c in h:
if c.getCard() == 'bang':
print("Would player ",oppNum," like to play a Bang?")
use = input("(y/n): ")
if use == "y":
self.players[oppNum].removeFromHand(c)
self.deck.discard(c)
return True
else:
self.boards.decreaseHealth(oppNum)
print("Player ",oppNum," was shot!")
return False
self.boards.decreaseHealth(oppNum)
return False
else:
for c in h:
if c.getCard() == 'bang':
self.players[oppNum].removeFromHand(c)
self.deck.discard(c)
print("Player",oppNum,"played a Bang")
return True
self.boards.decreaseHealth(oppNum)
print("Player ",oppNum," was shot!")
return False
def shootAll(self,pNum,indians,c):
if indians:
print("Player ",pNum," played Indians. Shoot or take damage!")
for p in range(self.numPlayers):
if p != pNum:
self.bangResponse(p)
else:
print("Player ",pNum," played Gatling. Miss or take damage!")
for p in range(self.numPlayers):
if p != pNum:
self.missedResponse(p)
self.players[pNum].removeFromHand(c)
self.deck.discard(c)
return True
def shootOne(self,pNum,c):
if self.players[pNum].getType() == "human":
opp = int(input("Who would you like to shoot: "))
cs = False
while cs == False:
while opp == pNum or opp >= self.numPlayers or opp < 0 or self.boards.checkHealth(opp) == 0:
print("Invalid player Number (enter -1 to retract card)")
opp = int(input("Please pick another Player: "))
if opp == -1:
return False
cs = self.boards.canShoot(pNum,opp,self.numPlayers)
if cs == False:
print("That player is out of range (enter -1 to retract card)")
opp = int(input("Please pick another Player: "))
if opp == -1:
return False
self.missedResponse(opp)
self.deck.discard(c)
elif self.players[pNum].getType() == "dumbAI":
opp = pNum+1
if opp == self.numPlayers:
opp = 0
while(self.boards.checkHealth(opp) == 0):
opp += 1
if opp == self.numPlayers:
opp = 0
if self.boards.canShoot(pNum,opp,self.numPlayers) == True:
print("Player ",pNum,"shot at player ",opp)
self.missedResponse(opp)
return True
return False
#IF you are a smarter AI, use the alpha-beta method to shoot someone
elif self.players[pNum].getType() == "ai":
#Get the person you should shoot using alpha-beta pruning
v, opp = self.alphaBetaPlayers[pNum].alphaBetaMove(self.boards, self.ply)
if opp == self.numPlayers:
opp = 0
#If you can't actually shoot the person you want to shoot, move to the next person
while(self.boards.checkHealth(opp) == 0):
opp += 1
if opp == self.numPlayers:
opp = 0
#Shoot that person!
if self.boards.canShoot(pNum,opp,self.numPlayers):
print("Player ",pNum,"shot at player ",opp)
print("The value of this move was: ",v)
self.missedResponse(opp)
return True
return False
return True
def humanPickCard(self,pNum):
self.boards.displayBoard(pNum)
self.players[pNum].displayHand()
cnum = int(input("Which card would you like to play (-1 to end your turn): "))
return cnum
#takes in player number and draws two cards
def startDraw(self,pNum):
c = [self.deck.draw(), self.deck.draw()]
self.players[pNum].addToHand(c)
return None
#takes in player number and checks if they are in jail
#returns true if they get out and false if they are suck
def checkJail(self,pNum):
if self.boards.inJail(pNum):
print("Player ",pNum," is in Jail and must draw a heart to play!")
c = self.deck.draw()
print("Player ",pNum," drew a ",c.getSuit())
if c.getSuit() != "heart":
print("Player ",pNum," must spend the night in Jail")
self.deck.discard(c)
jail = self.boards.removeStatus(pNum,"jail")
self.deck.discard(jail)
return False
else:
print("Player ",pNum," is released from Jail")
self.deck.discard(c)
jail = self.boards.removeStatus(pNum,"jail")
self.deck.discard(jail)
return True
return True
#takes in a player number and checks if they have dynamite
# returns true if they survive, false if they die
def checkDynamite(self,pNum):
if self.boards.hasDynamite(pNum):
print("Player ",pNum," is holding Dynamite! Do not draw a 2-9 of spades!")
c = self.deck.draw()
print("Player ",pNum," drew a ",c.getNum()," of ",c.getSuit(),"s")
if c.getSuit() == "spade" and c.getNum() >= '2' and c.getNum() <= '9':
self.deck.discard(c)
print("The Dynamite exploded! Player ",pNum," lost 3 health points!")
for i in range(3):
if self.boards.decreaseHealth(pNum) == False:
print("Player ",pNum," has died!")
return False
return True
else:
self.deck.discard(c)
print("Player ",pNum," safely passed the Dynamite to the next player")
self.boards.passDynamite(pNum)
return True
return True
#asks if the user wants to play again
def askPlayAgain(self):
again = input("Do you want to play again (yes/no): ")
while again != "yes" or again != "no":
print("invalid input")
again = input("Do you want to play again (yes/no): ")
if again == 'yes':
n = int(input("How many players (4-7): "))
while n > 7 or n < 4:
print("invalid input")
n = int(input("How many players (4-7): "))
self.playAgain(n)
return
elif again == "no":
return
return
#takes in number of players and resets deck, players, and boards
def PlayAgain(self,np):
self.numPlayers = np
deck = Deck()
boards = BoardsBang(np)
i = 0
h = []
while i < np:
r = boards.getRole(i,True)
h.append(deck.draw())
h.append(deck.draw())
h.append(deck.draw())
h.append(deck.draw())
if r == "Sheriff":
h.append(deck.draw())
p = PlayerBang(i,r,h)
self.players.append(p)
i = i+1
self.played = False
return