-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathField_setting.py
executable file
·2134 lines (1911 loc) · 109 KB
/
Field_setting.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
# -*- coding: utf-8 -*-
EMBEDDING_RANGE=3000-1
from my_moduler import get_module_logger
import numpy as np
import random
import math
import copy
from card_setting import *
import collections
from collections import deque
import itertools
import Player_Ability_setting
from util_ability import *
# mylogger = get_module_logger('mylogger')
from my_enum import *
import time
import os
check_follower = lambda card:card.card_category=="Creature"
from Game_setting import get_data
from trigger_ability_list import *
mylogger = get_module_logger(__name__)
class Field:
def __init__(self, max_field_num):
self.card_location = [[], []]
self.card_num = [0, 0]
self.cost = [0, 0]
self.max_cost = 10
self.remain_cost = [0, 0]
self.max_field_num = max_field_num
self.turn_end = False
self.graveyard = Graveyard()
self.play_cards = Play_Cards()
self.drawn_cards = Drawn_Cards()
self.players = [None, None]
self.evo_point = [2, 3]
self.able_to_evo_turn = [5, 4]
self.current_turn = [0, 0]
self.evo_flg = False
self.ex_turn_count = [0, 0]
self.turn_player_num = 0
self.stack = deque()
self.chain_num = 0
self.players_play_num = 0
self.player_ability = [[], []]
self.state_log = deque()
self.stack_num = 0
self.time = time.time()
self.secret = True
self.state_value_history = []
self.start_count = 0
self.copy_func = lambda card:card.get_copy()
self.before_observable_fields = [None,None]
def eq(self, other,debug=False):
if type(self) != type(other):
assert False, "NoImplemented"
if len(self.card_location[0]) != len(other.card_location[0]):
print("1",len(self.card_location[0]),len(other.card_location[0])) if debug else None
return False
if len(self.card_location[1]) != len(other.card_location[1]):
print("2,",len(self.card_location[1]), len(other.card_location[1])) if debug else None
return False
if self.turn_player_num != other.turn_player_num:
print("3",self.turn_player_num , other.turn_player_num) if debug else None
return False
observable = self.get_observable_data(player_num=0)
other_observable = other.get_observable_data(player_num=0)
# for key in list(observable.keys()):
# player_first = observable[key]
# other_first = other_observable[key]
# for second_key in list(observable[key].keys()):
# if player_first[second_key] !=other_first[second_key]:
# print("first:{} second:{} {} != {}".format(key,second_key,
# player_first[second_key],other_first[second_key])) if debug else None
#
# return False
tmp=any(any(observable[key][second_key] != other_observable[key][second_key] for second_key\
in tuple(observable[key].keys())) for key in tuple(observable.keys()))
if tmp: return False
observable = self.get_observable_data(player_num=1)
other_observable = other.get_observable_data(player_num=1)
# for key in list(observable.keys()):
# player_first = observable[key]
# other_first = other_observable[key]
# for second_key in list(observable[key].keys()):
# if player_first[second_key] !=other_first[second_key]:
# print("first:{} second:{} {} != {}".format(key,second_key,
# player_first[second_key],other_first[second_key])) if debug else None
# return False
tmp=any(any(observable[key][second_key] != other_observable[key][second_key] for second_key\
in tuple(observable[key].keys())) for key in tuple(observable.keys()))
if tmp: return False
#仮実装
self.play_cards.play_cards_set()
other.play_cards.play_cards_set()
for i in range(2):
i_len = len(self.card_location[i])
tmp = any(not self.card_location[i][j].eq(other.card_location[i][j]) for j in range(i_len))
if tmp: return False
# for j in range(i_len):
# first_card = self.card_location[i][j]
# second_card = other.card_location[i][j]
# if not first_card.eq(second_card):
# print("{}!={}".format(first_card,second_card)) if debug else None
# return False
player_name_list = self.play_cards.name_list[i]
for cost_key in sorted(list(player_name_list.keys())):
other_name_list = other.play_cards.name_list[i]
if cost_key not in other_name_list:
print("{} not in {}".format(cost_key, other_name_list)) if debug else None
return False
other_cost_list = other_name_list[cost_key]
player_cost_list = player_name_list[cost_key]
for category_key in sorted(list(player_cost_list.keys())):
if category_key not in other_cost_list:
print("{} not in {}".format(category_key, other_cost_list)) if debug else None
return False
player_category_list = player_cost_list[category_key]
other_category_list = other_cost_list[category_key]
tmp = any((name_key not in other_category_list) or \
other_category_list[name_key] != player_category_list[name_key] for name_key in tuple(player_category_list.keys()))
if tmp:return False
# for name_key in sorted(list(player_category_list.keys())):
# if name_key not in other_category_list:
# print("{} not in {}".format(name_key, other_category_list)) if debug else None
# return False
# if other_category_list[name_key] !=\
# player_category_list[name_key]:
# print("{} != {}".format(other_category_list[name_key], player_category_list[name_key])) if debug else None
# return False
return True
def set_data(self, field):
#self.card_location[0].clear()
#self.card_location[1].clear()
self.card_location[0] = list(map(self.copy_func,field.card_location[0]))
self.card_location[1] = list(map(self.copy_func,field.card_location[1]))
self.card_num = field.card_num[:]
self.cost = field.cost[:]
self.remain_cost = field.remain_cost[:]
self.turn_end = field.turn_end
self.graveyard.shadows = field.graveyard.shadows[:]
self.drawn_cards.name_list = [side[:] for side in field.drawn_cards.name_list]
self.play_cards.play_cards = [side[:] for side in field.play_cards.play_cards]
self.play_cards.played_turn_dict = [copy.copy(side) for side in field.play_cards.played_turn_dict]
self.graveyard.graveyard= [side[:] for side in field.graveyard.graveyard]#[field.graveyard.graveyard[0][:],field.graveyard.graveyard[1][:]]
"""
self.drawn_cards.name_list[0] = field.drawn_cards.name_list[0][:]
self.play_cards.play_cards[0] = field.play_cards.play_cards[0][:]
self.play_cards.played_turn_dict[0] = copy.copy(field.play_cards.played_turn_dict[0])
self.graveyard.graveyard[0] = field.graveyard.graveyard[0][:]
self.drawn_cards.name_list[1] = field.drawn_cards.name_list[1][:]
self.play_cards.play_cards[1] = field.play_cards.play_cards[1][:]
self.play_cards.played_turn_dict[1] = copy.copy(field.play_cards.played_turn_dict[1])
self.graveyard.graveyard[1] = field.graveyard.graveyard[1][:]
"""
self.players = [side.get_copy(field) for side in field.players]
#self.players[0] = field.players[0].get_copy(field)
#self.players[1] = field.players[1].get_copy(field)
self.players[0].field = self
self.players[1].field = self
self.update_hand_cost(player_num=0)
self.update_hand_cost(player_num=1)
self.evo_point = field.evo_point[:]
self.current_turn = field.current_turn[:]
self.evo_flg = field.evo_flg
self.ex_turn_count = field.ex_turn_count[:]
self.turn_player_num = int(field.turn_player_num)
self.players_play_num = int(field.players_play_num)
if len(field.player_ability[0]) > 0:
self.player_ability[0] = field.player_ability[0][:]
if len(field.player_ability[1]) > 0:
self.player_ability[1] = field.player_ability[1][:]
self.reset_time_stamp()
def solve_lastword_ability(self, virtual=False, player_num=0):
while len(self.stack) > 0:
(ability_id, player_num, itself) = self.stack.pop()
category = itself.card_category
used_ability_dict = creature_ability_dict if category == "Creature" else amulet_ability_dict
used_ability_dict[ability_id](self, self.players[player_num], self.players[1 - player_num], virtual, None, itself)
if self.check_game_end():
return
"""
(ability, player_num, itself) = self.stack.pop()
if not virtual:
mylogger.info("{}'s lastword ability actived".format(itself.name))
ability(self, self.players[player_num], self.players[1 - player_num], virtual, None, itself)
if self.check_game_end():
return
"""
# if virtual==False:
# mylogger.info("rest_num={}".format(len(self.stack)))
def solve_field_trigger_ability(self, virtual=False, player_num=0):
ability_list = deque()
if len(self.state_log) == 0: return
index = len(self.state_log)
if not self.secret:
for i in range(2):
for card in self.card_location[(i+player_num)%2]:
print("{:<30}:timestamp:{}".format(card.name,card.time_stamp))
mylogger.info("current_state_log:{}".format(["{}:{}".format(State_Code(cell[0]).name,cell[-1]) for cell in self.state_log]))
[[
(
[Player_Ability_setting.player_ability_id_2_func[player_ability_id](self, self.players[(i + player_num) % 2],
virtual,
state_log=self.state_log[j]) for
player_ability_id in self.player_ability[(i + player_num) % 2]],
[[ability_list.appendleft(
(ability_id, [self, self.players[(i + player_num) % 2], self.players[1 - (i + player_num) % 2], virtual, None,
self.card_location[(i + player_num) % 2][location_id], self.state_log[j]])) for ability_id in
self.card_location[(i + player_num) % 2][location_id].trigger_ability] for location_id
in reversed(range(len(self.card_location[(i + player_num) % 2])))
if self.card_location[(i + player_num) % 2][location_id].time_stamp < self.state_log[j][-1]]
)
for i in range(2)]
for j in reversed(range(index))]
"""
for j in reversed(range(index)):
target_state_log = self.state_log[j]
for i in range(2):
side_id = (i + player_num) % 2
[Player_Ability_setting.player_ability_id_2_func[player_ability_id](self, self.players[side_id], virtual,
state_log=target_state_log) for player_ability_id in self.player_ability[side_id]]
#for player_ability_id in self.player_ability[side_id]:
# ability = Player_Ability_setting.player_ability_id_2_func[player_ability_id]
# ability(self, self.players[side_id], virtual, state_log=target_state_log)
side = self.card_location[side_id]
#location_id = len(side) - 1
initial_id = len(side)
[[ability_list.appendleft(
(ability_id, [self, self.players[side_id], self.players[1 - side_id], virtual, None,
side[location_id], target_state_log])) for ability_id in
side[location_id].trigger_ability] for location_id in reversed(range(initial_id))
if side[location_id].time_stamp < target_state_log[-1]]
#while location_id >= 0:
# if side[location_id].time_stamp < target_state_log[-1]:
# [ability_list.appendleft((ability_id, [self, self.players[side_id], self.players[1 - side_id], virtual, None,
# side[location_id], target_state_log])) for ability_id in side[location_id].trigger_ability]
#
# for ability_id in side[location_id].trigger_ability:
# argument = [self, self.players[side_id], self.players[1 - side_id], virtual, None,
# side[location_id], target_state_log]
# ability_list.appendleft((ability_id, argument))
#
# location_id -= 1
#
"""
[self.state_log.popleft() for _ in range(index)]
#for _ in range(index):
# self.state_log.popleft()
"""
if not virtual:
mylogger.info("trigger_ability:{}".format([cell[-2] for cell in ability_list]))
[trigger_ability_dict[tmp_ability_pair[0]](tmp_ability_pair[1][0], tmp_ability_pair[1][1], tmp_ability_pair[1][2],
tmp_ability_pair[1][3], tmp_ability_pair[1][4], tmp_ability_pair[1][5],
state_log=tmp_ability_pair[1][6]) for tmp_ability_pair in ability_list]
"""
for tmp_ability_pair in ability_list:
ability = trigger_ability_dict[tmp_ability_pair[0]]
ability(tmp_ability_pair[1][0], tmp_ability_pair[1][1], tmp_ability_pair[1][2],
tmp_ability_pair[1][3], tmp_ability_pair[1][4], tmp_ability_pair[1][5],
state_log=tmp_ability_pair[1][6])
if not self.secret:
mylogger.info("next_state_log:{}".format(self.state_log))
def ability_resolution(self, virtual=False, player_num=0):
chain_len = 0
self.check_active_ability()
self.check_death(player_num, virtual=virtual)
while len(self.stack) > 0 or len(self.state_log) > 0:
self.solve_lastword_ability(virtual=virtual, player_num=player_num)
self.check_death(player_num, virtual=virtual)
self.solve_field_trigger_ability(virtual=virtual, player_num=player_num)
self.check_death(player_num, virtual=virtual)
chain_len += 1
assert chain_len < 100, "infinite_chain_error"
def check_active_ability(self):
for i in range(2):
for card in self.card_location[i]:
if card.have_active_ability:
if active_ability_check_func_list[card.func_id](self.players[i]):#card.active_ability_check_func(self.players[i]):
card.get_active_ability()
else:
card.lose_active_ability()
def get_observable_data(self, player_num=0):
observable_data_dict = {"player": {}, "opponent": {}}
for key in list(observable_data_dict.keys()):
player_id = (1 - 2 * int(key != "player")) * player_num + int(key != "player")
target_dict = observable_data_dict[key]
target_dict["leader_class"] = self.players[player_id].deck.leader_class.name
target_dict["life"] = self.players[player_id].life
target_dict["max_life"] = self.players[player_id].max_life
target_dict["hand_len"] = len(self.players[player_id].hand)
target_dict["deck_len"] = len(self.players[player_id].deck.deck)
target_dict["shadows"] = self.graveyard.shadows[player_id]
target_dict["pp/max_pp"] = (self.remain_cost[player_id], self.cost[player_id])
target_dict["evo_point"] = self.evo_point[player_id]
target_dict["leader_effects"] = \
"{}".format([Player_Ability_setting.player_ability_id_2_func[player_ability_id].name
for player_ability_id in self.player_ability[player_id]])
return observable_data_dict
def discard_card(self, player,hand_id):
if not self.secret:
mylogger.info("Player{} discard {}".format(player.hand[hand_id]))
del player.hand[hand_id]
self.graveyard.shadows[player.player_num] += 1
def reset_time_stamp(self):
self.stack.clear()
self.state_log.clear()
self.stack_num = 0
for i in range(2):
for card in self.card_location[i]:
card.time_stamp = 0
def restore_player_life(self, player=None, num=0, virtual=False, at_once=False):
tmp = num
if player.max_life - player.life < tmp:
tmp = player.max_life - player.life
player.life += tmp
if not virtual:
mylogger.info("Player {} restore {} life".format(player.player_num + 1, tmp))
if not at_once:
self.stack_num += 1
self.state_log.append([State_Code.RESTORE_PLAYER_LIFE.value, player.player_num, player.field.stack_num])
def restore_follower_toughness(self, follower=None, num=0, virtual=False, at_once=False):
side_id = 0
if follower in self.card_location[0]:
side_id = 0
elif follower in self.card_location[1]:
side_id = 1
else:
assert False, "follower does not exist!"
amount = follower.restore_toughness(num)
if not virtual:
mylogger.info("{} restore {} life".format(follower.name, amount))
if not at_once:
self.stack_num += 1
self.state_log.append([State_Code.RESTORE_FOLLOWER_TOUGHNESS.value, side_id, self.stack_num])
def gain_max_pp(self, player_num=0, num=0, virtual=False):
if self.cost[player_num] < self.max_cost:
if not virtual:
mylogger.info("Player {} gain {} max PP".format(player_num + 1, num))
self.cost[player_num] += 1
def resotre_pp(self, player_num=0, num=0, virtual=False):
value = int(self.remain_cost[player_num])
self.remain_cost[player_num] += num
self.remain_cost[player_num] = min(self.remain_cost[player_num], self.cost[player_num])
value = self.cost[player_num] - value
if not virtual:
mylogger.info("Player {} gain {} PP".format(player_num + 1, value))
def check_death(self, player_num=0, virtual=False):
for j in range(2):
i = 0
count = 0
while i < len(self.card_location[(player_num + j) % 2]):
card = self.card_location[(player_num + j) % 2][i]
if not card.is_in_field or card.is_in_graveyard:
self.remove_card([(player_num + j) % 2, i], virtual=virtual)
else:
i += 1
count += 1
assert count < 100,"infinite loop!"
def append_played_turn(self,card_name=None):
assert card_name is not None
if card_name not in self.play_cards.played_turn_dict[self.turn_player_num]:
self.play_cards.played_turn_dict[self.turn_player_num][card_name] = \
[self.current_turn[self.turn_player_num]]
else:
self.play_cards.played_turn_dict[self.turn_player_num][card_name].append(
self.current_turn[self.turn_player_num])
def play_creature(self, hand, card_id, player_num, player, opponent, virtual=False, target=None):
if self.card_num[player_num] < self.max_field_num:
tmp = hand.pop(card_id)
self.stack_num += 1
self.state_log.append(
[State_Code.PLAY.value, (player_num, tmp.card_category, tmp.card_id), self.stack_num]) # 1はプレイ
tmp.is_in_field = True
tmp.is_tapped = True
self.set_card(tmp, player_num, virtual=virtual)
if tmp.fanfare_ability is not None:
creature_ability_dict[tmp.fanfare_ability](self, player, opponent, virtual, target, tmp)
self.play_cards.append(tmp.card_category, tmp.card_id, player_num)
self.append_played_turn(card_name=tmp.name)
self.check_death(player_num=player_num, virtual=virtual)
else:
self.players[player_num].show_hand()
mylogger.info("card_id:{}".format(card_id))
self.show_field()
raise Exception('field is full!\n')
def play_spell(self, hand, card_id, player_num, player, opponent, virtual=False, target=None,check=None):
tmp = hand.pop(card_id)
self.stack_num += 1
self.state_log.append([State_Code.PLAY.value, (player_num, tmp.card_category, tmp.card_id), self.stack_num])
self.spell_boost(player.player_num)
for ability in tmp.triggered_ability:
ability(self, player, opponent, virtual, target, tmp)
tmp.is_in_graveyard = True
self.graveyard.append(tmp.card_category, tmp.card_id, player_num)
self.play_cards.append(tmp.card_category, tmp.card_id, player_num)
self.append_played_turn(card_name=tmp.name)
self.check_death(player_num=player_num, virtual=virtual)
def play_amulet(self, hand, card_id, player_num, player, opponent, virtual=False, target=None):
if self.card_num[player_num] < self.max_field_num:
tmp = hand.pop(card_id)
self.stack_num += 1
self.state_log.append([State_Code.PLAY.value, (player_num, tmp.card_category, tmp.card_id), self.stack_num])
tmp.is_in_field = True
self.set_card(tmp, player_num, virtual=virtual)
if tmp.fanfare_ability is not None:
amulet_ability_dict[tmp.fanfare_ability](self, player, opponent, virtual, target, tmp)
#tmp.fanfare_ability(self, player, opponent, virtual, target, tmp)
self.play_cards.append(tmp.card_category, tmp.card_id, player_num)
self.append_played_turn(card_name=tmp.name)
self.check_death(player_num=player_num, virtual=virtual)
else:
self.players[player_num].show_hand()
mylogger.info("card_id:{}".format(card_id))
self.show_field()
raise Exception('field is full!\n')
def spell_boost(self, player_num):
hand = self.players[player_num].hand
for card in hand:
if card.card_class.name == "RUNE" and card.spell_boost is not None:
card.spell_boost += 1
# if card.cost_down==True:
# card.cost=max(0,card.origin_cost-card.spell_boost)
def play_as_other_card(self, hand, card_id, player_num, virtual=False, target=None):
player = self.players[player_num]
opponent = self.players[1 - player_num]
play_card = player.hand[card_id]
if play_card.have_accelerate and play_card.active_accelerate_code[0]:
if not virtual: mylogger.info("Accelerate")
play_card = self.players[player_num].hand.pop(card_id)
assert play_card.active_accelerate_code[1] in play_card.accelerate_card_id
new_card_id = play_card.accelerate_card_id[play_card.active_accelerate_code[1]]
new_card = Spell(new_card_id)
self.stack_num += 1
self.state_log.append([State_Code.PLAY.value, (player_num, "Spell", new_card_id), self.stack_num])
self.spell_boost(player_num)
for ability in new_card.triggered_ability:
ability(self, player, opponent, virtual, target, new_card)
self.append_played_turn(card_name = new_card.name)
self.play_cards.append("Spell", new_card_id, player_num)
new_card.is_in_graveyard = True
self.graveyard.append(new_card.card_category, new_card_id, player_num)
def set_card(self, card, player_num, virtual=False):
if len(self.card_location[player_num]) < self.max_field_num:
self.stack_num += 1
self.state_log.append([State_Code.SET.value, (player_num, card.card_category, card.card_id, id(card)),
self.stack_num]) # 2は場に出たとき
self.card_location[player_num].append(card)
self.card_num[player_num] += 1
card.is_tapped = True
card.is_in_field = True
card.time_stamp = self.stack_num
else:
if not virtual:
mylogger.info("{} is vanished".format(card.name))
def remove_card(self, location, virtual=False, by_effects=False):
#assert self.card_location[location[0]][location[1]] is not None
tmp = self.card_location[location[0]][location[1]]
if KeywordAbility.BANISH_WHEN_LEAVES.value in tmp.ability:
self.banish_card(location, virtual=virtual)
return
tmp = self.card_location[location[0]].pop(location[1])
self.card_num[location[0]] -= 1
self.stack_num += 1
self.state_log.append(
[State_Code.DESTROYED.value, (location[0], tmp.card_category, tmp.card_id), self.stack_num]) # 3は破壊されたとき
if not virtual:
if tmp.card_category == "Creature":
mylogger.info("Player {}'s {}(location_id={}) is dead".format(location[0] + 1, tmp.name, location[1]))
else:
mylogger.info("Player {}'s {} is broken".format(location[0] + 1,
tmp.name))
lastword_len = len(tmp.lastword_ability)
#for i in range(lastword_len):
# self.stack.appendleft((tmp.lastword_ability[i], location[0], tmp.get_copy()))
card_copy = tmp.get_copy()
[self.stack.appendleft((tmp.lastword_ability[i], location[0], card_copy))
for i in range(lastword_len)]
tmp.is_in_field = False
tmp.is_in_graveyard = True
self.graveyard.append(tmp.card_category, tmp.card_id, location[0])
def return_card_to_hand(self, target_location, virtual=False):
assert len(self.card_location[target_location[0]]) > target_location[1]
tmp = self.card_location[target_location[0]][target_location[1]]
if KeywordAbility.BANISH_WHEN_LEAVES.value in tmp.ability:
self.banish_card(target_location, virtual=virtual)
return
self.card_num[target_location[0]] -= 1
card_id = tmp.card_id
card_category = tmp.card_category
if not virtual:
mylogger.info("Player {}'s {} return to hand".format(target_location[0] + 1,
tmp.name))
del self.card_location[target_location[0]][target_location[1]]
card = None
if card_category == "Creature":
card = Creature(card_id)
elif card_category == "Amulet":
card = Amulet(card_id)
if card is not None:
#self.players[target_location[0]].hand.append(card)
self.players[target_location[0]].append_cards_to_hand([card])
def banish_card(self, location, virtual=False):
assert self.card_location[location[0]][location[1]] is not None
if not virtual:
mylogger.info(
"{}(location_id={}) is banished".format(self.card_location[location[0]][location[1]].name, location[1]))
del self.card_location[location[0]][location[1]]
self.card_num[location[0]] -= 1
def transform_card(self, location, card=None, virtual=False):
assert self.card_location[location[0]][location[1]] is not None and card is None
if not virtual:
mylogger.info(
"{}(location_id={}) is transformed into {}".format(self.card_location[location[0]][location[1]].name,
location[1], card.name))
self.card_location[location[0]][location[1]] = card
def show_field(self):
for i in range(2):
print("player", i + 1, "'s field")
side_len = len(self.card_location[i])
for j in range(side_len):
print(j, ": ", self.card_location[i][j])
print("\n")
def attack_to_follower(self, attack, defence, field, virtual=False):
#assert attack[1] < len(self.card_location[attack[0]]) and defence[1] < len(self.card_location[defence[0]]),\
# "{},{} {},{}".format(attack[1],len(self.card_location[attack[0]]),defence[1],len(self.card_location[defence[0]]),self.show_field())
attacking_follower = self.card_location[attack[0]][attack[1]]
defencing_follower = self.card_location[defence[0]][defence[1]]
#assert attacking_follower.can_attack_to_follower() and defencing_follower.can_be_attacked(), \
# "\nattack:{} defence:{}\n{}\n{}".format(attacking_follower.can_attack_to_follower(),
# defencing_follower.can_be_attacked(),attacking_follower,defencing_follower,self.show_field())
attacking_follower.current_attack_num += 1
if not virtual:
mylogger.info("Player {}'s {} attacks Player {}'s {}".format(attack[0] + 1, attacking_follower.name
, defence[0] + 1, defencing_follower.name))
#for ability in attacking_follower.in_battle_ability:
# ability(self, self.players[attack[0]], self.players[defence[0]], attacking_follower, defencing_follower,
# situation_num=[0, 1, 3], virtual=virtual)
[battle_ability_dict[ability_id](self, self.players[attack[0]], self.players[defence[0]], attacking_follower, defencing_follower,
situation_num=[0, 1, 3], virtual=virtual) for ability_id in attacking_follower.in_battle_ability]
#for ability in defencing_follower.in_battle_ability:
# ability(self, self.players[defence[0]], self.players[attack[0]], defencing_follower, attacking_follower,
# situation_num=[3], virtual=virtual)
[battle_ability_dict[ability_id](self, self.players[defence[0]], self.players[attack[0]], defencing_follower, attacking_follower,
situation_num=[3], virtual=virtual) for ability_id in defencing_follower.in_battle_ability]
self.stack_num += 1
self.state_log.append([State_Code.ATTACK_TO_FOLLOWER.value, attack[0], attacking_follower, defencing_follower,
self.stack_num]) # 5はフォロワーに攻撃したとき
self.check_death(player_num=attack[0], virtual=virtual)
self.ability_resolution(virtual=virtual, player_num=attack[0])
if not attacking_follower.is_in_field or not defencing_follower.is_in_field:
return
if KeywordAbility.AMBUSH.value in attacking_follower.ability:
attacking_follower.ability.remove(KeywordAbility.AMBUSH.value)
amount = defencing_follower.get_damage(attacking_follower.power)
attacking_follower.get_damage(defencing_follower.power)
if KeywordAbility.DRAIN.value in attacking_follower.ability:
restore_player_life(self.players[attack[0]], virtual, num=amount)
if defencing_follower.is_in_field:
if KeywordAbility.BANE.value in attacking_follower.ability and \
KeywordAbility.CANT_BE_DESTROYED_BY_EFFECTS.value not in defencing_follower.ability: # 必殺効果処理
new_def_index = [defence[0], self.card_location[defence[0]].index(defencing_follower)]
self.remove_card(new_def_index, virtual)
if attacking_follower.is_in_field:
if KeywordAbility.BANE.value in defencing_follower.ability and \
KeywordAbility.CANT_BE_DESTROYED_BY_EFFECTS.value not in attacking_follower.ability:
new_atk_index = [attack[0], self.card_location[attack[0]].index(attacking_follower)]
self.remove_card(new_atk_index, virtual)
# self.solve_lastword_ability(virtual=virtual,player_num=attack[0])
self.ability_resolution(virtual=virtual, player_num=attack[0])
def attack_to_player(self, attacker, defence_player, visible=False, virtual=False):
attacking_follower = self.card_location[attacker[0]][attacker[1]]
#assert attacking_follower.can_attack_to_player()
attacking_follower.current_attack_num += 1
#for ability in attacking_follower.in_battle_ability:
# ability(self, self.players[attacker[0]], defence_player, attacking_follower, defence_player,
# situation_num=[0, 2], virtual=virtual)
[battle_ability_dict[ability_id](self, self.players[attacker[0]], defence_player, attacking_follower, defence_player,
situation_num=[0, 2], virtual=virtual) for ability_id in attacking_follower.in_battle_ability]
self.stack_num += 1
self.state_log.append(
[State_Code.ATTACK_TO_PLAYER.value, attacker[0], attacking_follower, self.stack_num]) # 5はプレイヤーに攻撃したとき
self.check_death(player_num=attacker[0], virtual=virtual)
self.ability_resolution(virtual=virtual, player_num=attacker[0])
if not attacking_follower.is_in_field:
return
if visible:
print("Player", attacker[0] + 1, "'s", attacking_follower.name,
"attacks directly Player", defence_player.player_num + 1)
if not virtual:
mylogger.info("Player {}'s {} attacks directly Player {}".format(attacker[0] + 1, attacking_follower.name
, 2 - attacker[0]))
if KeywordAbility.AMBUSH.value in attacking_follower.ability:
attacking_follower.ability.remove(KeywordAbility.AMBUSH.value)
amount = defence_player.get_damage(attacking_follower.power)
if KeywordAbility.DRAIN.value in attacking_follower.ability:
restore_player_life(self.players[attacker[0]], virtual, num=amount)
# self.solve_lastword_ability(virtual=virtual,player_num=attacker[0])
# self.ability_resolution(virtual=virtual,player_num=attacker[0])
if visible:
print("Player", defence_player.player_num + 1, "life: ", defence_player.life)
def start_of_turn(self, player_num, virtual=False):
self.state_log.clear()
self.reset_time_stamp()
self.stack_num += 1
self.state_log.append([State_Code.START_OF_TURN.value, player_num, self.stack_num])
self.ability_resolution(virtual=virtual, player_num=player_num)
i = 0
opponent_num = 1-player_num
while i < len(self.card_location[player_num]):
thing = self.card_location[player_num][i]
category = thing.card_category
used_ability_dict = creature_ability_dict if category == "Creature" else amulet_ability_dict
no_ability_flg = 1
for ability_id in thing.turn_start_ability:
no_ability_flg = 0
before = len(self.card_location[player_num])
used_ability_dict[ability_id](self, self.players[player_num], self.players[opponent_num], virtual, None,
thing)
after = len(self.card_location[player_num])
i += int(before <= after)
if self.check_game_end(): return
i += no_ability_flg
i = 0
while i < len(self.card_location[opponent_num]):
thing = self.card_location[opponent_num][i]
category = thing.card_category
used_ability_dict = creature_ability_dict if category == "Creature" else amulet_ability_dict
no_ability_flg = 1
for ability_id in thing.turn_start_ability:
no_ability_flg = 0
before = len(self.card_location[opponent_num])
used_ability_dict[ability_id](self, self.players[opponent_num], self.players[player_num], virtual,
None,
thing)
after = len(self.card_location[opponent_num])
i += int(before <= after)
if self.check_game_end(): return
i += no_ability_flg
self.ability_resolution(virtual=virtual, player_num=player_num)
if self.check_game_end():
return
for thing in self.card_location[player_num]:
thing.down_count(num=1, virtual=virtual)
self.check_death(player_num, virtual=virtual)
self.ability_resolution(virtual=virtual, player_num=player_num)
def end_of_turn(self, player_num, virtual=False):
self.state_log.clear()
self.reset_time_stamp()
self.stack_num += 1
self.state_log.append([State_Code.END_OF_TURN.value, player_num, self.stack_num])
self.ability_resolution(virtual=virtual, player_num=player_num)
for creature_id in self.get_creature_location()[player_num]:
creature = self.card_location[player_num][creature_id]
if creature.until_turn_end_buff != [0, 0]:
creature.power = \
max(0, creature.power - creature.until_turn_end_buff[0])
creature.buff[0] = \
max(0, creature.buff[0] - creature.until_turn_end_buff[0])
creature.until_turn_end_buff = [0, 0]
i = 0
opponent_num = 1- player_num
while i < len(self.card_location[player_num]):
thing = self.card_location[player_num][i]
category = thing.card_category
used_ability_dict = creature_ability_dict if category == "Creature" else amulet_ability_dict
no_ability_flg = 1
for ability_id in thing.turn_end_ability:
no_ability_flg = 0
before = len(self.card_location[player_num])
used_ability_dict[ability_id](self, self.players[player_num], self.players[opponent_num], virtual, None,
thing)
after = len(self.card_location[player_num])
i += int(before <= after)
if self.check_game_end(): return
i += no_ability_flg
i = 0
while i < len(self.card_location[opponent_num]):
thing = self.card_location[opponent_num][i]
category = thing.card_category
used_ability_dict = creature_ability_dict if category == "Creature" else amulet_ability_dict
no_ability_flg = 1
for ability_id in thing.turn_end_ability:
no_ability_flg = 0
before = len(self.card_location[opponent_num])
used_ability_dict[ability_id](self, self.players[opponent_num], self.players[player_num], virtual, None,
thing)
after = len(self.card_location[opponent_num])
i += int(before <= after)
if self.check_game_end(): return
i += no_ability_flg
self.ability_resolution(virtual=virtual, player_num=player_num)
self.check_death(player_num=player_num, virtual=virtual)
self.ability_resolution(virtual=virtual, player_num=player_num)
self.players_play_num = 0
def check_game_end(self):
lib_flg = False
for player in self.players:
if player.lib_out_flg:
#if len(player.deck.deck) == 0:
lib_flg = True
#player.lib_out_flg = False
break
return self.players[0].life <= 0 or self.players[1].life <= 0 or lib_flg
def untap(self, player_num):
self.turn_end = False
self.current_turn[player_num] += 1
self.turn_player_num = player_num
self.evo_flg = False
for card in self.card_location[player_num]:
card.untap()
def reset_remain_cost(self, num):
self.remain_cost[num] = int(self.cost[num])
def increment_cost(self, player_num):
if self.cost[player_num] < self.max_cost:
self.cost[player_num] += 1
self.reset_remain_cost(player_num)
def evolve(self, creature, virtual=False, target=None):
# if virtual==False:mylogger.info("evo_check")
if self.evo_flg or creature.evolved:
first = creature in self.card_location[0]
mylogger.info("first:{} policy:{}".format(first, self.players[1 - int(first)].policy.name))
self.show_field()
mylogger.info(" name:{} evolved:{} evo_flg:{} able_to_evo:{}"
.format(creature.name, creature.evolved, self.evo_flg,
self.get_able_to_evo(self.players[self.turn_player_num])))
assert False
card_index = int(creature not in self.card_location[0])
self.stack_num += 1
self.state_log.append([State_Code.EVOLVE.value, (card_index, id(creature)), self.stack_num]) # 5は進化したとき
if not virtual:
mylogger.info("{} evolve".format(creature.name))
mylogger.info("remain evo point:{}".format(self.evo_point[self.turn_player_num]))
creature.evolve(self, target, player_num=self.turn_player_num, virtual=virtual)
self.evo_flg = True
def auto_evolve(self, creature, virtual=False):
assert not creature.evolved, "Already evolved!"
card_index = int(creature not in self.card_location[0])
self.stack_num += 1
self.state_log.append([State_Code.EVOLVE.value, (card_index, id(creature)), self.stack_num]) # 5は進化したとき
if not virtual:
mylogger.info("{} evolve".format(creature.name))
creature.evolve(self, None, player_num=self.turn_player_num, virtual=virtual, auto=True)
def check_ward(self):
location = self.get_creature_location()
ans = [False, False]
for i, side in enumerate(location):
for j in side:
creature = self.card_location[i][j]
if KeywordAbility.WARD.value in creature.ability:
ans[i] = True
break
return ans
def get_creature_location(self):
ans = [[], []]
for j, thing in enumerate(self.card_location[0]):
if thing.card_category == "Creature":
ans[0].append(j)
for j, thing in enumerate(self.card_location[1]):
if thing.card_category == "Creature":
ans[1].append(j)
return ans
def update_hand_cost(self, player_num=0):
for i, card in enumerate(self.players[player_num].hand):
if card.card_class.name == "RUNE" and card.spell_boost is not None and card.cost_down:
card.cost = max(0, card.origin_cost - card.spell_boost)
if card.cost_change_ability is not None:
card.cost_change_ability(card, self, self.players[player_num])
if card.have_enhance:
enhance_flg = False
for cost in card.enhance_cost:
if cost <= self.remain_cost[player_num]:
enhance_flg = True
card.active_enhance_code = [True, cost]
if not enhance_flg:
card.active_enhance_code = [False, 0]
elif card.have_accelerate:
accelerate_flg = False
if self.remain_cost[player_num] >= card.cost:
card.active_accelerate_code = [False, 0]
continue
for cost in card.accelerate_cost:
if cost <= self.remain_cost[player_num]:
accelerate_flg = True
card.active_accelerate_code = [True, cost]
if not accelerate_flg:
card.active_accelerate_code = [False, 0]
def get_can_be_targeted(self, player_num=0):
can_be_targeted = []
opponent_side_creature = self.get_creature_location()[1 - player_num]
for ele in opponent_side_creature:
creature = self.card_location[1 - player_num][ele]
if creature.can_be_targeted():
can_be_targeted.append(ele)
return can_be_targeted
def get_can_be_attacked(self, player_num=0):
can_be_attacked = []
opponent_side_creature = self.get_creature_location()[1 - player_num]
for ele in opponent_side_creature:
creature = self.card_location[1 - player_num][ele]
if creature.can_be_attacked(): can_be_attacked.append(ele)
return can_be_attacked
def get_ward_list(self, player_num=0):
ward_list = []
can_be_attacked = self.get_can_be_attacked(player_num=player_num)
for i in can_be_attacked:
creature = self.card_location[1 - player_num][i]
if KeywordAbility.WARD.value in creature.ability:
ward_list.append(i)
return ward_list
def get_situation(self, player, opponent):
ward_list = self.get_ward_list(player_num=player.player_num)
can_be_targeted = self.get_can_be_targeted(player_num=player.player_num)
can_be_attacked = self.get_can_be_attacked(player_num=player.player_num)
regal_targets = self.get_regal_target_dict(player, opponent)
return ward_list, can_be_targeted, can_be_attacked, regal_targets
def get_regal_target_dict(self, player, opponent):
self.update_hand_cost(player_num=player.player_num)
regal_targets = {}
for i, target_card in enumerate(player.hand):
regal_targets[i] = self.get_regal_targets(target_card, target_type=1, player_num=player.player_num)
return regal_targets
def get_able_to_play(self, player, regal_targets=None):
if regal_targets is None:
regal_targets = self.get_regal_target_dict(player, self.players[1-player.player_num])
full_flg = len(self.card_location[player.player_num]) == self.max_field_num
# mylogger.info("full_flg={}".format(full_flg))
able_to_play = []
for i, hand_card in enumerate(player.hand):
if hand_card.have_enhance is True and hand_card.active_enhance_code[0] is True:
if hand_card.active_enhance_code[1] <= self.remain_cost[player.player_num]:
if hand_card.card_category == "Spell":
if hand_card.enhance_target != 0:
if regal_targets[i] != []:
able_to_play.append(i)
else:
able_to_play.append(i)
else:
if not full_flg:
able_to_play.append(i)
elif hand_card.cost <= self.remain_cost[player.player_num]:
if hand_card.card_category == "Spell":
if hand_card.have_target == 0:
able_to_play.append(i)
else:
if i in regal_targets and regal_targets[i] != []:
able_to_play.append(i)
else:
if not full_flg:
able_to_play.append(i)
elif hand_card.have_accelerate is True and hand_card.active_accelerate_code[0] is True:
if hand_card.active_accelerate_code[1] <= self.remain_cost[player.player_num]:
if hand_card.accelerate_target != 0:
if regal_targets[i] != []:
able_to_play.append(i)
else:
able_to_play.append(i)
return able_to_play
def get_able_to_attack(self, player):
able_to_attack = []
for i in self.get_creature_location()[player.player_num]:
creature = self.card_location[player.player_num][i]
if creature.can_attack_to_player():
if creature.player_attack_regulation is not None:
if player_attack_regulation[creature.player_attack_regulation](player):#creature.player_attack_regulation(player):
able_to_attack.append(i)
else:
able_to_attack.append(i)
return able_to_attack
def get_able_to_creature_attack(self, player):
able_to_creature_attack = []
for i in self.get_creature_location()[player.player_num]:
creature = self.card_location[player.player_num][i]
if creature.can_attack_to_follower():
if creature.can_only_attack_target is not None:
if creature.can_only_attack_target(self, player):
able_to_creature_attack.append(i)
else:
able_to_creature_attack.append(i)
# if creature.evolved==False:able_to_evo.append(i)
return able_to_creature_attack
def get_able_to_evo(self, player):
#print(player)
if self.current_turn[player.player_num] < self.able_to_evo_turn[player.player_num] or self.evo_point[
player.player_num] == 0 or self.evo_flg:
return []
able_to_evo = []
for i in self.get_creature_location()[player.player_num]:
creature = self.card_location[player.player_num][i]
if not creature.evolved:
able_to_evo.append(i)
return able_to_evo
def get_flag_and_choices(self, player, opponent, regal_targets):
self.update_hand_cost(player_num=player.player_num)
can_attack = True
can_play = True
can_evo = True
able_to_play = self.get_able_to_play(player, regal_targets=regal_targets)
if len(able_to_play) == 0:
can_play = False