-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
1294 lines (1203 loc) · 41.8 KB
/
game.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
import random
import copy
import textwrap
import socket
import time
from cards import Spell, Minion, Card
class Game:
def __init__(self, player1, player2):
self.player1 = player1
self.player2 = player2
self.activePlayer = player1
self.passivePlayer = player2
self.turnCounter = 0
self.TCP_IP = '127.0.0.1'
self.TCP_PORT = 5005
self.BUFFER_SIZE = 1024
# self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.playersConnected = 0
self.localGame = True
self.againstAI = True
self.simulation= False
self.printing = True
self.debug = True
self.isSimulated = False
def evaluateCompleteRound(game,iteration,previousMoves):
iteration += 1
hand = game.activePlayer.hand
fMinions = game.activePlayer.activeMinions
tempGame = copy.deepcopy(game)
if len(previousMoves)>0 and previousMoves[-1] == -1:
print("done")
return game.evaluateState()
if tempGame.canDoSomething() and iteration <4:
# print("remaining hand len",len(tempGame.activePlayer.hand))
bestMove = tempGame.generateBestLegalMove(iteration,previousMoves)
previousMoves.append(bestMove[0])
previousMoves.append(bestMove[1])
print(previousMoves)
print("bestMove",bestMove)
# print("bestmove:",bestMove)
if bestMove[0]=="noMoreMoves":
# print("1 leaf node reached")
score = tempGame.evaluateState()
# print("score",score)
print(previousMoves)
return score
if bestMove[0]=="bestCard":
tempGame.playCard(bestMove[1])
# print("4: played card")
return tempGame.evaluateCompleteRound(iteration,previousMoves)
# print("Spiller beste kort")
elif bestMove[0]=="bestAttackFace":
# print("attacking face with",tempGame.activePlayer.activeMinions[bestMove[1]].name)
tempGame.attackFace(tempGame.activePlayer.activeMinions[bestMove[1]])
print("attacked?",tempGame.activePlayer.activeMinions[bestMove[1]].hasAttacked)
return tempGame.evaluateCompleteRound(iteration,previousMoves)
# bestMove[1].hasAttacked=True
elif bestMove[0]=="bestAttackMinion":
tempGame.attackMinion(tempGame.activePlayer.activeMinions[bestMove[1][0]],bestMove[1][1])
# print("5, attacked mnion")
return tempGame.evaluateCompleteRound(iteration,previousMoves)
else:
# print("3 leaf node reached")
# print("returning",tempGame.evaluateState())
score = tempGame.evaluateState()
# print("score",score)
print(previousMoves)
return (score)
else:
# print("2 leaf node reached")
# print("returning",tempGame.evaluateState())
score = tempGame.evaluateState()
# print("score",score)
print(previousMoves)
return (score)
def evaluateState(game):
totalAttackAndHealYourSide = 0 # Høyere er bedre
totalAttackAndHealEnemySide = 0 # Lavere er bedre
yourHealth = game.activePlayer.health # Høyere er bedre
enemyHealth = game.passivePlayer.health # Lavere er bedre
ownHandLength = len(game.activePlayer.hand)
# print("own hand",ownHandLength)
enemyHandLength = len(game.passivePlayer.hand)
numberOfEnemyMinions = len(game.passivePlayer.activeMinions)
numberOfFriendlyMinions = len(game.activePlayer.activeMinions)
remainingMana = game.activePlayer.currentMana
frozenEnemies = 0
for minion in game.activePlayer.activeMinions:
totalAttackAndHealYourSide += minion.currentAttack + minion.currentHealth
for minion in game.passivePlayer.activeMinions:
totalAttackAndHealEnemySide += minion.currentAttack + minion.currentHealth
if minion.frozenRounds>0: frozenEnemies += 1
if numberOfEnemyMinions==0:
frozenEnemiesScale = 0
else:
frozenEnemiesScale = frozenEnemies / numberOfEnemyMinions
numberOfFriendlyMinionsScale = numberOfFriendlyMinions / 3
numberOfEnemyMinionsScale = numberOfFriendlyMinions / 3
yourHealthScale = yourHealth/30
enemyHealthScale = enemyHealth/30
ownHandLengthScale = ownHandLength/7
enemyHandLengthScale = enemyHandLength/4
totalAttackAndHealYourSideScale = totalAttackAndHealYourSide / 30
totalAttackAndHealEnemySideScale = totalAttackAndHealEnemySide / 30
remainingManaScale = remainingMana / 10
if yourHealthScale>1: yourHealthScale = 1
if enemyHealthScale>1: enemyHealthScale = 1
if ownHandLengthScale>1: ownHandLengthScale = 1
if enemyHandLengthScale>1: enemyHandLengthScale = 1
if totalAttackAndHealYourSideScale>1: totalAttackAndHealYourSideScale = 1
if totalAttackAndHealEnemySideScale>1: totalAttackAndHealEnemySideScale = 1
if numberOfFriendlyMinionsScale>1: numberOfFriendlyMinionsScale = 1
if numberOfEnemyMinionsScale>1: numberOfEnemyMinionsScale = 1
# if game.printing:
# print("frozen",frozenEnemiesScale)
# utility = (enemyHealthScale*((yourHealthScale*-5)+(ownHandLengthScale*-1)+(enemyHandLengthScale)+(totalAttackAndHealYourSide*-1)+(totalAttackAndHealEnemySideScale)))
utility = (enemyHealthScale * 6) - (yourHealthScale*6) -(numberOfFriendlyMinionsScale*5)+(numberOfEnemyMinionsScale*5)+ (totalAttackAndHealEnemySideScale*5) - (totalAttackAndHealYourSideScale*5) + (enemyHandLengthScale*1) - (ownHandLengthScale*3) - (remainingManaScale*0.1)
if (enemyHealth - yourHealth)>12 or yourHealth<15 :
utility = (frozenEnemiesScale*(-6)) + (enemyHealthScale * 3) + (yourHealthScale*-12) + (numberOfFriendlyMinionsScale*-1) + (numberOfEnemyMinionsScale * 10) + (totalAttackAndHealEnemySideScale * 15) + (totalAttackAndHealYourSideScale*-1) + (ownHandLengthScale * -0.5) + (remainingManaScale * -0.05)
# print("Util under 10nliv")
# 0 utility is optimal state
return utility
def generateBestLegalMove(game,iteration,p_moves):
nyGame = copy.deepcopy(game)
nyGame2 = copy.deepcopy(game)
currentUtility = game.evaluateState()
bestCard = -1
bestCardUtility = 999999
for card in range(len(nyGame.activePlayer.hand)):
tempGame=copy.deepcopy(nyGame)
tempGame.printing=False
tempGame.isSimulated=True
# tempGame.evaluateCompleteRound()
tempGame.playCard(card)
tempUtility = 999999
tempUtility = tempGame.evaluateState()
# if iteration >3:
# tempUtility = tempGame.evaluateState()
# else:
# tempUtility = tempGame.evaluateCompleteRound(iteration,p_moves)
# print("Done evaluateCompleteRound",tempUtility)
# print("util",tempUtility,card)
if tempUtility < bestCardUtility:
bestCardUtility = tempUtility
bestCard = card
# print("this was better")
bestAttackMinion = -1
bestAttackTarget = -1
bestAttackMinionUtility = 999999
bestAttackFaceUtility = 999999
for minion in range(len(nyGame2.activePlayer.activeMinions)):
if not nyGame2.activePlayer.activeMinions[minion].hasAttacked:
tempMinion = copy.deepcopy(minion)
tempGame=copy.deepcopy(nyGame2)
tempGame.printing=False
tempGame.isSimulated=True
possibleTargets = tempGame.getPossibleTargetsOfMinion(tempGame.activePlayer.activeMinions[tempMinion])
for target in possibleTargets[0]:
tempGame=copy.deepcopy(game)
tempGame.printing=False
tempGame.attackMinion(tempGame.activePlayer.activeMinions[tempMinion],target)
# tempGame.activePlayer.activeMinions[tempMinion].hasAttacked=False
# tempUtility = tempGame.evaluateCompleteRound(iteration,p_moves)
tempUtility = tempGame.evaluateState()
if tempUtility < bestAttackMinionUtility:
bestAttackMinionUtility = tempUtility
bestAttackMinion = tempMinion
bestAttackTarget = target
for minion in range(len(nyGame2.activePlayer.activeMinions)):
if not nyGame2.activePlayer.activeMinions[minion].hasAttacked:
# print("sjekker value for å gå face for minion",minion)
tempMinion = copy.deepcopy(minion)
if possibleTargets[1]: # Face virker ikke
# print("tester face")
tempGame=copy.deepcopy(nyGame2)
tempGame.printing=False
tempGame.attackFace(tempGame.activePlayer.activeMinions[tempMinion])
# tempGame.activePlayer.activeMinions[tempMinion].hasAttacked=False
tempUtility = tempGame.evaluateState()
# tempUtility = tempGame.evaluateCompleteRound(iteration,p_moves)
# print("angriping med",tempMinion,"gir utility: ",tempUtility)
if tempUtility < bestAttackFaceUtility:
bestAttackFaceUtility = tempUtility
bestAttackMinion = tempMinion
bestAttackTarget = -1
bestAttackMinionTarget = [bestAttackMinion,bestAttackTarget]
time.sleep(0)
# print("Util: card, attMinion, attFace, current")
print("utilities:",bestCardUtility,bestAttackMinionUtility,bestAttackFaceUtility,currentUtility)
if currentUtility <= bestCardUtility and currentUtility <= bestAttackMinionUtility and currentUtility < bestAttackFaceUtility:
return ("noMoreMoves",-1)
if bestCardUtility < bestAttackMinionUtility and bestCardUtility < bestAttackFaceUtility:
return ("bestCard",bestCard)
elif bestAttackMinionUtility < bestCardUtility and bestAttackMinionUtility < bestAttackFaceUtility:
return ("bestAttackMinion",bestAttackMinionTarget)
elif bestAttackFaceUtility < bestCardUtility and bestAttackMinionUtility > bestAttackFaceUtility:
return ("bestAttackFace",bestAttackMinion)
if bestCardUtility==999999 and bestCardUtility==999999:
return("noMoreMoves",-1)
return("noMoreMoves",-1)
# # what to do, which card, which target
# bestMove = ["playCard",0,1]
# bestMove = ["attackFace",1]
# bestMove = ["attackMinion",1,0]
def customPrint(text):
if not self.localGame and not self.againstAI:
self.activePlayer.connection.send(text.encode())
else:
print(text)
def getInput(self,description):
if not self.localGame:
addr = self.activePlayer.IP
conn = self.activePlayer.connection
message=description
conn.send(message.encode())
return conn.recv(1024).decode()
else:
return (input(description))
def startServer(self):
self.s.bind((self.TCP_IP, self.TCP_PORT))
self.s.listen(1)
print("Waiting for clients to connect")
while 1:
conn, addr = self.s.accept()
print("Connection from:",addr)
if self.playersConnected==0:
data = conn.recv(1024).decode()
print("received data:",data)
# conn.send("Received".encode())
if data=="Player":
self.player1.IP=addr
self.player1.connection = conn
print("Player 1 is:",addr)
self.player2.IP=addr
self.player2.connection = conn
print("Player 2 is:",addr)
break
# elif self.playersConnected==1:
# data = conn.recv(1024).decode()
# print("received data:",data)
# # conn.send("Received".encode())
# if data=="Player":
# self.player2.IP=addr
# self.player2.connection = conn
# print("Player 2 is:",addr)
def initialize(self,player1,player2):
self.startServer()
self.start()
def printCardDescription(self,cards):
c = 0
print("index - cost - name - effects")
for card in cards:
print(" {} - {} - {}: {}".format(c,card.cost,card.name,card.description))
c+=1
def printAttackReadyMinions(self):
print("\nMinions who can attack:\n")
longestName = 0
for minion in self.activePlayer.activeMinions:
if len(minion.name)>longestName:
longestName = len(minion.name)
if longestName > 25:
longestName = 25
count = 0
print ("Position - Name - Stats - Effects")
for minion in self.activePlayer.activeMinions:
if not minion.hasAttacked and minion.frozenRounds<=0:
tempName = "{0:<{1}}".format(minion.name,longestName)
if len(tempName)>longestName-1:
tempName=tempName[0:longestName]
tempType = "{0:<6}".format(minion.type)
print(" {4} - {1} - {5}/{6} - {2}".format(minion.cost,tempName,minion.description,tempType,count,minion.currentAttack,minion.currentHealth))
count += 1
print("")
def printPlayableCards(self,player):
print("\nCards you can play:\n")
longestName = 0
for card in self.activePlayer.hand:
if len(card.name)>longestName and card.cost <= self.activePlayer.currentMana:
longestName = len(card.name)
if longestName > 25:
longestName = 25
count = 0
tempText = 'Name'
print (" # -Cost- {0: <{1}} - Type - Stats - Effects".format(tempText,longestName))
for card in self.activePlayer.hand:
if card.cost <= self.activePlayer.currentMana:
tempName = "{0:<{1}}".format(card.name,longestName)
if len(tempName)>longestName-1:
tempName=tempName[0:longestName]
tempType = "{0:<6}".format(card.type)
if card.type=="Spell":
tempType += " - N/A"
print(" {4} - {0} - {1} - {3} - {2}".format(card.cost,tempName,card.description,tempType,count))
else:
print(" {4} - {0} - {1} - {3} - {5}/{6} - {2}".format(card.cost,tempName,card.description,tempType,count,card.currentAttack,card.currentHealth))
count += 1
print("")
def printHandDetails(self,player):
print("\nYour hand:")
longestName = 0
for card in self.activePlayer.hand:
if len(card.name)>longestName:
longestName = len(card.name)
if longestName > 25:
longestName = 25
count = 0
print ("Position - Cost - Name - Type - Stats - Effects")
for card in self.activePlayer.hand:
tempName = "{0:<{1}}".format(card.name,longestName)
if len(tempName)>longestName-1:
tempName=tempName[0:longestName]
tempType = "{0:<6}".format(card.type)
if card.type=="Spell":
tempType += " - N/A"
print(" {4} - {0} - {1} - {3} - {2}".format(card.cost,tempName,card.description,tempType,count))
else:
print(" {4} - {0} - {1} - {3} - {2}".format(card.cost,tempName,card.description,tempType,count))
count += 1
def printDetails(self):
print("\n === GAME STATE DETAILS === \n")
print("\nEnemy minions:")
for minion in self.passivePlayer.activeMinions:
print(" {} - {} - {}".format(minion.name,minion.currentAttack,minion.currentHealth,minion.description))
print("\nYour minions:")
for minion in self.activePlayer.activeMinions:
print(" {} - {} - {}".format(minion.name,minion.currentAttack,minion.currentHealth,minion.description))
self.printHandDetails(self.activePlayer)
print("")
def printPassiveHand(self):
line0=''
line1=''
line2=''
line3=''
line4=''
line5=''
# line6=''
line7=''
cards =''
cardsInHand = self.passivePlayer.getHand()
for i in range(len(cardsInHand)):
# cards += "{} ".format(self.passivePlayer.hand[i].getName())
cards +=self.passivePlayer.hand[i].getName()+" "
line0+=" _______ "
line1+="| | "
line2+="| | "
line3+="| | "
# line6+="| | "
line7+="| | "
line4+="|_______| "
print("\n*{}*".format(self.passivePlayer.getName()))
if (len(cardsInHand)>0):
print(cards )
print(line0)
print(line1)
print(line2)
print(line3)
# print(line6)
print(line7)
print(line4)
print(line5)
def printHand(self):
# 8 kolonner per minion
line00=''
line0=''
line1=''
line10=''
line11=''
line2=''
line3=''
line4=''
line5=''
cardsInHand = self.activePlayer.getHand()
for i in range(len(cardsInHand)):
line00+="{} - ".format(cardsInHand[i].getName())
line0+=" _________ "
line1+="|{} | ".format(cardsInHand[i].cost)
tempName = cardsInHand[i].name
if len(tempName)>9:
tempName = tempName[0:9]
line10+="|{0: <9}| ".format(tempName)
line11+="| | "
if (cardsInHand[i].getType()=="Minion"):
line3+="| {}/{} | ".format(cardsInHand[i].currentAttack,cardsInHand[i].currentHealth)
if (cardsInHand[i].hasTaunt):
line2+="| Taunt | "
else:
line2+="| | "
elif (cardsInHand[i].getType()=="Spell"):
line3+="| SPELL | "
# line2+="| | "
tempDesc = cardsInHand[i].description
if len(tempDesc)>9:
tempDesc=tempDesc[0:9]
line2+="|{}| ".format(tempDesc)
line4+="|_________| "
line5+=" #{} ".format(i)
if (len(cardsInHand)>0):
print("MANA: {}/{}".format(self.activePlayer.currentMana,self.activePlayer.maxMana))
# print(line00)
print(line0)
print(line1)
print(line10)
print(line11)
print(line2)
print(line3)
print(line4)
print(line5)
else:
print("*{}*\n MANA: {}/{}".format(self.activePlayer.getName(), self.activePlayer.currentMana,self.activePlayer.maxMana))
def printPassivePlayerActiveMinion(self):
align = 5 - len(self.passivePlayer.activeMinions)
if align<0:
align = 0
line00=''
line0=''
line1=''
line10=''
line11=''
line2=''
line3=''
line4=''
line5=''
cardsInHand = self.passivePlayer.activeMinions
for i in range(align):
line00+=" "
line0+=" "
line1+=" "
line10+=" "
line11+=" "
line2+=" "
line3+=" "
line4+=" "
line5+=" "
for i in range(len(cardsInHand)):
line00+="{} - ".format(cardsInHand[i].getName())
line0+=" _________ "
hasAttacked = cardsInHand[i].hasAttacked
frozRounds = cardsInHand[i].frozenRounds
hasTaunt = cardsInHand[i].hasTaunt
tempString = cardsInHand[i].getName()
if len(tempString)>7:
tempString = tempString[0:5]+".."
line00+="{}- ".format(tempString)
if not hasAttacked and frozRounds<=0 :
line1+="|{} | ".format(cardsInHand[i].cost)
elif frozRounds>0:
line1+="|{} FROZEN| ".format(cardsInHand[i].cost)
else:
line1+="|{} Z| ".format(cardsInHand[i].cost)
tempName = cardsInHand[i].name
if len(tempName)>9:
tempName = tempName[0:9]
line10+="|{0: <9}| ".format(tempName)
line11+="| | "
if (cardsInHand[i].getType()=="Minion"):
line3+="| {}/{} | ".format(cardsInHand[i].currentAttack,cardsInHand[i].currentHealth)
if (cardsInHand[i].hasTaunt):
line2+="| Taunt | "
else:
line2+="| | "
elif (cardsInHand[i].getType()=="Spell"):
line3+="| SPELL | "
# line2+="| | "
tempDesc = cardsInHand[i].description
if len(tempDesc)>9:
tempDesc=tempDesc[0:9]
line2+="|{}| ".format(tempDesc)
line4+="|_________| "
line5+=" #{} ".format(i)
if (len(cardsInHand)>0):
# print(line00)
print(line0)
print(line1)
print(line10)
print(line11)
print(line2)
print(line3)
print(line4)
print(line5)
else:
print ("\nEmpty board side\n")
def printActivePlayerActiveMinion(self):
align = 5 - len(self.activePlayer.activeMinions)
if align<0:
align = 0
line00=''
line0=''
line1=''
line10=''
line11=''
line2=''
line3=''
line4=''
line5=''
cardsInHand = self.activePlayer.activeMinions
for i in range(align):
line00+=" "
line0+=" "
line1+=" "
line10+=" "
line11+=" "
line2+=" "
line3+=" "
line4+=" "
line5+=" "
for i in range(len(cardsInHand)):
line00+="{} - ".format(cardsInHand[i].getName())
line0+=" _________ "
hasAttacked = cardsInHand[i].hasAttacked
frozRounds = cardsInHand[i].frozenRounds
hasTaunt = cardsInHand[i].hasTaunt
tempString = cardsInHand[i].getName()
if len(tempString)>7:
tempString = tempString[0:5]+".."
line00+="{}- ".format(tempString)
if not hasAttacked and frozRounds<=0 :
line1+="|{} | ".format(cardsInHand[i].cost)
elif frozRounds>0:
line1+="|{} FROZEN| ".format(cardsInHand[i].cost)
else:
line1+="|{} Z| ".format(cardsInHand[i].cost)
tempName = cardsInHand[i].name
if len(tempName)>9:
tempName = tempName[0:9]
line10+="|{0: <9}| ".format(tempName)
line11+="| | "
if (cardsInHand[i].getType()=="Minion"):
line3+="| {}/{} | ".format(cardsInHand[i].currentAttack,cardsInHand[i].currentHealth)
if (cardsInHand[i].hasTaunt):
line2+="| Taunt | "
else:
line2+="| | "
elif (cardsInHand[i].getType()=="Spell"):
line3+="| SPELL | "
# line2+="| | "
tempDesc = cardsInHand[i].description
if len(tempDesc)>9:
tempDesc=tempDesc[0:9]
line2+="|{}| ".format(tempDesc)
line4+="|_________| "
line5+=" #{} ".format(i)
if (len(cardsInHand)>0):
# print(line00)
print(line0)
print(line1)
print(line10)
print(line11)
print(line2)
print(line3)
print(line4)
print(line5)
else:
print ("\nEmpty board side\n")
def printGameState(self):
# print("----PASSIVE---")
print ("Health----------------",self.passivePlayer.getHealth(),"----------------------")
player1Minions = self.passivePlayer.getActiveMinions()
player2Minions = self.activePlayer.getActiveMinions()
p1MinionStr = ''
p2MinionStr = ''
# for i in range (len(player1Minions)):
# p1MinionStr = '{} {}/{}'.format(p1MinionStr,player1Minions[i].getAttack(),player1Minions[i].getHealth())
# print(p1MinionStr)
# for i in range (len(player2Minions)):
# p2MinionStr = '{} {}/{}'.format(p2MinionStr,player2Minions[i].getAttack(),player2Minions[i].getHealth())
# print(p2MinionStr)
self.printPassivePlayerActiveMinion()
print("================================================")
self.printActivePlayerActiveMinion()
print("Health----------------",self.activePlayer.getHealth(),"----------------------")
# print("----ACTIVE----")
def getPossibleTargetsOfMinion(self,attacker):
enemyMinions = self.passivePlayer.activeMinions
taunts = []
targets = []
face = False
c = 0
for minion in enemyMinions:
if minion.hasTaunt:
taunts.append(c)
c += 1
if attacker.onlyAbleToAttackMinions==False and len(taunts)==0:
targets = range(0,len(enemyMinions))
face = True
elif len(taunts)>0:
targets = taunts
elif attacker.onlyAbleToAttackMinions and len(taunts)==0:
targets = range(0,len(enemyMinions))
elif len(targets)==0:
targets = []
face = True
return targets,face
def printPossibleTargetsOfMinion(self,attacker):
enemyMinions = self.passivePlayer.activeMinions
taunts = []
targets = []
face = False
c = 0
for minion in enemyMinions:
if minion.hasTaunt:
taunts.append(c)
c += 1
if attacker.onlyAbleToAttackMinions==False and len(taunts)==0:
targets = range(0,len(enemyMinions))
face = True
elif len(taunts)>0:
targets = taunts
elif attacker.onlyAbleToAttackMinions and len(taunts)==0:
targets = range(0,len(enemyMinions))
elif len(targets)==0:
targets = []
face = True
print("\nLegal Targets:\n")
longestName = 0
for target in targets:
if len(enemyMinions[target].name)>longestName:
longestName = len(enemyMinions[target].name)
if longestName > 25:
longestName = 25
count = 0
print ("Position - Name - Stats - Effects")
for target in targets:
tempName = "{0:<{1}}".format(enemyMinions[target].name,longestName)
if len(tempName)>longestName-1:
tempName=tempName[0:longestName-1]
tempType = "{0:<6}".format(enemyMinions[target].type)
print(" {4} - {1} - {2} - {5}/{6} - {3}".format(enemyMinions[target].cost,tempName,enemyMinions[target].description,tempType,targets[count],enemyMinions[target].currentAttack,enemyMinions[target].currentHealth))
count += 1
if face:
print(" f - FACE")
print("")
def nextTurn(self):
for minion in self.activePlayer.activeMinions:
minion.roundEnd(self,minion)
self.removeDeadMinions()
if self.activePlayer == self.player1:
self.activePlayer = self.player2
self.passivePlayer = self.player1
print("===============",self.activePlayer.name,"'s turn =============")
else:
self.activePlayer = self.player1
self.passivePlayer = self.player2
self.turnCounter += 1
if self.turnCounter%2==0:
self.nextRound()
self.activateRoundStartMinions()
# print (self.activePlayer.getName()+"'s turn")
for minion in self.activePlayer.activeMinions:
minion.hasAttacked=False
for minion in self.passivePlayer.activeMinions:
minion.hasAttacked=False
def nextRound(self):
self.passivePlayer.maxMana += 1
self.passivePlayer.currentMana = self.passivePlayer.maxMana
self.activePlayer.maxMana+=1
self.activePlayer.currentMana = self.activePlayer.maxMana
def activateRoundStartMinions(self):
for minion in self.activePlayer.activeMinions:
minion.onRoundStart(self)
def mulligan(self,player):
if player.AI==False:
# print("Your hand:")
c=0
for card in player.hand:
c+=1
self.printHand()
remove = str(input("Which cards do you want to remove?: (e.g. '02')"))
k=0
for card in remove:
# print("removing",card)
tempCard = player.hand.pop(int(card)-k)
# print(tempCard.name)
player.deck.cards.append(tempCard)
k+=1
player.deck.shuffle()
for card in remove:
cardDrawn = player.deck.draw()
player.hand.append( cardDrawn)
# print("Done with mulligan")
# for card in player.hand:
# print(card.name)
else:
c=0
k=0
# for card in player.hand:
# print(" kort:",card.name)
for card in player.hand:
if card.cost>6:
if self.printing:
print("mulligan",card.name)
player.deck.cards.append(card)
print("popper:",player.hand[k])
player.hand.pop(k)
c+=1
break
k+=1
# player.deck.shuffle()
# print(player.hand)
k=0
for card in player.hand:
if card.cost>5:
# if self.printing:
# print("mulligan",card.name)
player.deck.cards.append(card)
# print("popper:",player.hand[k])
player.hand.pop(k)
c+=1
break
k+=1
# player.deck.shuffle()
# print(player.hand)
k=0
for card in player.hand:
if card.cost>4:
# if self.printing:
# print("mulligan",card.name)
player.deck.cards.append(card)
# print("popper:",player.hand[k])
player.hand.pop(k)
c+=1
break
k+=1
k=0
for card in player.hand:
if card.cost>3:
# if self.printing:
# print("mulligan",card.name)
player.deck.cards.append(card)
# print("popper:",player.hand[k])
player.hand.pop(k)
c+=1
break
k+=1
# print(player.hand)
player.deck.shuffle()
# if player == self.activePlayer:
# self.draw(c,"a")
# else:
# self.draw(c,"p")
for count in range(c):
cardDrawn = player.deck.draw()
player.hand.append( cardDrawn)
# if self.printing:
# print("mullied",c,"cards")
def updateContinousEffects(self):
for minion in self.activePlayer.activeMinions:
minion.continousEffect(self,minion)
for minion in self.passivePlayer.activeMinions:
minion.continousEffect(self,minion)
def canDoSomething(self):
if self.canPlayCard():
# print("canPlayCard")
return True
if self.canAttack():
# print("canAttack")
return True
return False
def canPlayCard(self):
t=''
for card in self.activePlayer.hand:
t+=card.name+" "
if card.cost <= self.activePlayer.currentMana:
return True
def canAttack(self):
for minion in self.activePlayer.activeMinions:
if minion.hasAttacked==False and minion.frozenRounds<=0:
return True
def playCard(self,cardPosition):
card = self.activePlayer.hand[int(cardPosition)]
if (card.cost <= self.activePlayer.currentMana):
self.activePlayer.currentMana = self.activePlayer.currentMana - card.cost
if (card.getType()=="Minion"):
self.playMinion(int(cardPosition))
else:
self.playSpell(int(cardPosition))
return True
else:
if self.activePlayer.AI==False:
print("You don't have enough mana")
return False
def playSpell(self,cardPosition):
spell = self.activePlayer.hand[cardPosition]
if spell.targetOwnMinions and len(self.activePlayer.activeMinions)==0:
print("This spell needs a friendly target")
return False
spell = self.activePlayer.hand.pop(cardPosition)
if self.printing:
print (self.activePlayer.name,"played spell: ", spell)
# print (spell.getName(),"Has the effect:",spell.getDescription())
if spell.damageOne[0]>0:
if not spell.damageOne[1] and self.activePlayer.AI==False:
target = input("Which minion do you want to damage? ('f' for face): ")
if target == "f":
self.passivePlayer.reduceHealth(spell.damageOne[0])
if self.printing:
print(self.activePlayer.getName(),"damaged",self.passivePlayer.getName(),spell.damageOne[0],"damage")
else:
p2minion = self.passivePlayer.getActiveMinions()[int(target)]
p2Health = p2minion.getHealth() - spell.damageOne[0]
self.passivePlayer.getActiveMinions()[int(target)].currentHealth=p2Health
if self.printing:
print(self.activePlayer.getName(),"damaged",p2minion.getName(),spell.damageOne[0],"damage")
else:
self.passivePlayer.reduceHealth(spell.damageOne[0])
if self.printing:
print(self.activePlayer.getName(),"damaged",self.passivePlayer.getName(),spell.damageOne[0],"damage")
if spell.damageEnemyAOE[0]>0:
if spell.damageEnemyAOE[1]:
#Spell hits enemy minions and face
for minion in self.passivePlayer.activeMinions:
minion.damage(spell.damageEnemyAOE[0])
self.passivePlayer.reduceHealth(spell.damageEnemyAOE[0])
else:
#Spell hits enemy minions
for minion in self.passivePlayer.activeMinions:
minion.damage(spell.damageEnemyAOE[0])
spell.effect(self)
self.ActivateOnSpellCastMinions()
self.removeDeadMinions()
self.updateContinousEffects()
def playMinion(self,cardPosition):
tempMinion = self.activePlayer.hand.pop(cardPosition)
if self.printing:
print (self.activePlayer.name,"played minion: ", tempMinion)
self.activePlayer.activeMinions.append(tempMinion)
tempMinion.bc(self,tempMinion)
self.removeDeadMinions()
self.updateContinousEffects()
def summonMinion(self,minion,player): #Summon minion, called from effects in other cards
newMinion = copy.deepcopy(minion)
player.activeMinions.append(newMinion)
newMinion.bc(self,newMinion)
newMinion.setOwner(player)
self.removeDeadMinions()
self.updateContinousEffects()
def ActivateOnSpellCastMinions(self):
for minion in self.activePlayer.activeMinions:
minion.onSpell(self)
minion.onSpellOwnRound(self,minion)
# print("spell cast effects activated")
for minion in self.passivePlayer.activeMinions:
minion.onSpell(self)
def draw(self,amount,player):
if player=="a":
for i in range (amount):
cardDrawn = self.activePlayer.deck.draw()
# print (self.activePlayer.name,"drew a card")
self.activePlayer.hand.append( cardDrawn)
if player=="p":
for i in range (amount):
cardDrawn = self.passivePlayer.deck.draw()
# print (self.passivePlayer.name,"drew a card")
self.passivePlayer.hand.append( cardDrawn)
def attackMinion(self,attacker,p2Pos):
if attacker.hasAttacked == False and attacker.frozenRounds<=0:
pMinions = self.passivePlayer.getActiveMinions()
taunts = []
for minion in pMinions:
if minion.hasTaunt:
taunts.append(minion)
p2minion = self.passivePlayer.getActiveMinions()[int(p2Pos)]
if len(taunts)>0:
if not p2minion.hasTaunt:
if self.activePlayer.AI==False:
print("*You need to target a minion with taunt*")
return False
p2Health = p2minion.getHealth() - attacker.getAttack()
self.passivePlayer.getActiveMinions()[int(p2Pos)].currentHealth=p2Health
p1Health = attacker.getHealth() - p2minion.getAttack()
attacker.currentHealth=p1Health
if self.printing:
print(attacker.getName(),"attacked",p2minion.getName())
attacker.attacked()
self.updateContinousEffects()
self.removeDeadMinions()
self.updateContinousEffects()
attacker.afterAttack(self,attacker)
def didAnyoneWin(self):
aWon = False
pWon = False
if self.activePlayer.getHealth() <= 0:
pWon = True
if self.passivePlayer.getHealth() <=0:
aWon = True
return (aWon,pWon)
def attackFace(self,attacker):
# if self.printing:
# print("Inni attackface")
# print("hasattacked: ",attacker.hasAttacked,"froze:",attacker.frozenRounds)
if not attacker.hasAttacked and attacker.frozenRounds<=0:
# if self.printing:
# print("etter hasattacked og frozen")
if attacker.onlyAbleToAttackMinions==False:
if attacker.currentAttack<=0:
print("This minion has no attack")
else:
pMinions = self.passivePlayer.getActiveMinions()
taunts = []
for minion in pMinions:
if minion.hasTaunt:
taunts.append(minion)
if len(taunts)>0:
if self.activePlayer.AI==False:
print("You need to attack a minion with taunt")
return False
self.passivePlayer.reduceHealth(attacker.getAttack())
if self.printing:
print(attacker.name,"has attacked",self.passivePlayer.getName(),"for",attacker.currentAttack,"damage")
attacker.attacked()
attacker.afterAttack(self,attacker)
else:
print("This minion may only attack minions")
else:
if self.activePlayer.AI==False:
print("that minion has to wait a round to attack")
self.updateContinousEffects()
def removeDeadMinions(self):
listOfDeathrattles = []
killedAny=False
count = 0
for minion in self.activePlayer.activeMinions:
if minion.currentHealth<=0:
if self.printing:
print("{}'s minion '{}' has died".format(self.activePlayer.getName(),minion.getName()))