-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_gr_42.py
1136 lines (900 loc) · 34.3 KB
/
ai_gr_42.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 -*-
import math
ants_type = {}
# other functions
def check_ennemy_ants_near_allies(ant_structure, main_structure, team):
"""Check if an ennemy ants is near a specified ally. Close means less than 5 cells away (return number)
Parameter
----------
ant_structure: structure containing all the ants (list)
main_structure: main structure of the game board (list)
team: ally team number (int)
Return
-------
close_e_ant: List of ant id close to each ally ant (dict)
specification: Maxime Dufrasne (v.Lia 18/4/21)
implementation: Maxime Dufrasne, Youlan Collard (v.1 29/4/21)
"""
close_e_ant = {}
for ant in ant_structure:
close_e_ant[ant['id']] = []
ant_pos = (ant['pos_y'], ant['pos_x'])
for y in range(-5, 6):
for x in range(-5, 6):
potential_ant_id = main_structure[ant_pos[0] + y][ant_pos[1] + x]['ant']
if potential_ant_id != None:
ant_dict = return_ant_by_id(ant_structure, potential_ant_id)
if ant_dict['team'] != team:
close_e_ant[ant['id']].append(ant_dict)
return close_e_ant
def compute_danger(anthill_structure, ant_structure, team):
"""Compute the current level of danger.
Parameters
-----------
anthill_structure: list of 2 elements containing the anthills information (list)
ant_structure: structure containing all the ants (list)
Return
------
danger: A number who determine the danger level (int)
specification: Maxime Dufrasne (v.1 18/4/21)
implementation: Martin Buchet Maxime Dufrasne (v.1 28/4/21 )
"""
#Need correct values
danger = 0
e_average_dist = e_average_dist_from_a_base(ant_structure, anthill_structure, team)
if e_average_dist <= 5:
danger += 10
elif e_average_dist > 5 and e_average_dist <= 10:
danger += 7.5
else:
danger += 5
a_average_dist = a_average_dist_from_a_base(ant_structure, anthill_structure, team)
if a_average_dist <= 5:
danger -= 2.5
elif a_average_dist > 5 and a_average_dist <= 10:
danger += 3.5
else:
danger += 7.5
ennemies, allies = seperate_ally_and_ennemy_ants(ant_structure, team)
ally_average_level = compute_average_level_ant(allies)
ennemy_average_level = compute_average_level_ant(ennemies)
difference_average_level = ennemy_average_level - ally_average_level
if difference_average_level > 1:
danger += 15
elif difference_average_level > 0 and difference_average_level < 1:
danger += 7.5
elif difference_average_level < 0 and difference_average_level > -1:
danger -= 7.5
elif difference_average_level < -1:
danger -= 15
difference_number_ants = len(ennemies) - len(allies)
if difference_number_ants > 3:
danger += 15
elif difference_number_ants > 0:
danger += 7.5
elif difference_number_ants < 0 and difference_number_ants > -3:
danger -= 7.5
elif difference_number_ants < -3:
danger -= 15
return danger
def compute_average_level_ant(ant_list):
"""Compute the average level of a list of ant.
Parameters
----------
ant_list: the list to compute (list)
Returns
-------
average_level: The average level of the list (float)
Version
-------
specification: Youlan Collard (v.1)
implementation: Youlan Collard (v.1) Liam Letot (v.2 07/05/21)
"""
average_level = 0
if len(ant_list) != 0:
for ant in ant_list:
average_level += ant['level']
average_level = average_level / len(ant_list)
return average_level
def e_average_dist_from_a_base(ant_structure, anthill_structure, team):
"""Compute average distance of ennemies ants from allie base
Parameters
----------
ant_structure: structure containing all the ants (list)
anthill_structure: list of 2 elements containing the anthills information (list)
team: the digit of the team we're in (int)
Return
------
e_average_dist: Ennemies ants average distance from allie base
Version
-------
specification: Maxime Dufrasne (v.1 22/4/21)
implementation: Martin Buchet (v.1 24/4/21)
"""
ants = seperate_ally_and_ennemy_ants(ant_structure, team)
enemies = ants[0]
#list of all the distances between ennemies and our base
e_dist_list = []
total = 0
for ant in enemies:
ally_anthill = anthill_structure[team - 1]
dist = compute_distance((ally_anthill['pos_y'], ally_anthill['pos_x']), (ant['pos_y'], ant['pos_x']))
e_dist_list.append(dist)
for each in range(0, len(e_dist_list)):
total += e_dist_list[each]
e_average_dist = total / len(e_dist_list)
return e_average_dist
def a_average_dist_from_a_base(ant_structure, anthill_structure, team):
"""Compute average distance of allies ants from allie base
Parameters
----------
ant_structure: structure containing all the ants (list)
anthill_structure: list of 2 elements containing the anthills information (list)
Return
------
a_average_dist: Allies ants average distance from allie base
Version
-------
specification: Maxime Dufrasne (v.1 22/4/21 )
implementation: Maxime Dufrasne (v.1 24/4/21)
"""
ants = seperate_ally_and_ennemy_ants(ant_structure, team)
a_dist_list = []
total = 0
allies = ants[1]
for ant in allies:
ally_anthill = anthill_structure[team - 1]
dist = compute_distance((ally_anthill['pos_y'], ally_anthill['pos_x']), (ant['pos_y'], ant['pos_x']))
a_dist_list.append(dist)
for each in range(0, len(a_dist_list)):
total += a_dist_list[each]
a_average_dist = total / len(a_dist_list)
return a_average_dist
def compute_defense_ants(anthill_structure, ant_structure, team):
"""Compute the number of ants in defense for each team.
Parameters
-----------
anthill_structure: list of 2 elements containing the anthills information (list)
ant_structure: structure containing all the ants (list)
team: team number of our ai (int)
Return
------
defense_ants: List of ants considered in defense for each team (dict)
specification: Maxime Dufrasne (v.1 18/4/21)
implementation: Youlan Collard (v.1)
"""
ennemies, allies = seperate_ally_and_ennemy_ants(ant_structure, team)
for anthill in anthill_structure:
if anthill['team'] == team:
ally_group = generate_defense_group(anthill, allies)
else:
ennemy_group = generate_defense_group(anthill, ennemies)
other_team = get_ennemy_team(team)
return {team: ally_group, other_team: ennemy_group}
def generate_defense_group(anthill, ant_list):
"""Generate a list of ants close to the specified anthill given the anthill and the ants posessed by this anthill
Parameters
----------
anthill: anthill to check (dict)
ant_list: ants list possessed by this anthill
Returns
-------
ant_group: group of ants considered in defense (list)
Version
-------
specification: Youlan Collard (v.1)
implementation: Youlan Collard (v.1)
"""
group = []
anthill_pos = (anthill['pos_y'], anthill['pos_x'])
for ant in ant_list:
ant_pos = (ant['pos_y'], ant['pos_x'])
if compute_distance(anthill_pos, ant_pos) < 6:
group.append(ant)
return group
def compute_fight_worth(ennemy_ants, ally_ants, ant_structure):
"""Calculate the rentability of a particular fight.
Parameters
----------
ennemy_ants: list of all ennemy ants (list)
ally_ants: list of all ally ants (list)
ant_structure: the structure containing the ants (list)
Return
------
fight_point: worth point of the combat (float)
Version
-------
specification: Martin Buchet (v.1 19/04/21)
implementation: Liam Letot (v.1 20/04/21)
"""
ennemy_clods = 0
ally_clods = 0
ennemy_hp = 0
ally_hp = 0
ennemy_average_level = compute_average_level_ant(ennemy_ants)
ally_average_level = compute_average_level_ant(ally_ants)
ennemy_number = len(ennemy_ants)
ally_number = len(ally_ants)
for ennemy in ennemy_ants:
ennemy_hp += ennemy['health']
if ennemy['carrying'] == True:
ennemy_clods += 1
for ally in ally_ants:
ally_hp += ally['hp']
if ally['carrying'] == True:
ally_clods += 1
ennemy_value = (ennemy_number / (ennemy_clods +1)) + 1
ally_value = (ally_number / (ally_clods +1)) + 1
ennemy_lose= (ally_number * ally_average_level)
ally_lose= (ennemy_number * ennemy_average_level)
ally_worth = ally_hp - (ally_lose / ally_value)
ennemy_worth = ennemy_hp - (ennemy_lose / ennemy_value)
worth = ally_worth - ennemy_worth
return worth
def generate_ants_e_group(ant_structure, team):
"""Genreate a list of ants close to each other. (ennemies)
Parameters
----------
ant_structure: the structure containing the ants (list)
team : your team number (int)
Return
------
close_ant: a list which contain group of ennemies ants (list)
Version
-------
specification: Liam Letot (v.1 19/04/21)
implementation: Youlan Collard (v.1)
"""
ennemies, allies = seperate_ally_and_ennemy_ants(ant_structure, team)
groups = []
for ant in ennemies:
current_group = []
for ant_to_check in ennemies:
if ant['id'] != ant_to_check['id']:
ant_pos = (ant['pos_y'], ant['pos_x'])
ant_to_check_pos = (ant_to_check['pos_y'], ant_to_check['pos_x'])
if compute_distance(ant_pos, ant_to_check_pos) <= 5:
current_group.append(ant['id'])
groups.append(current_group)
already_seen = []
groups_not_duplicated = []
for group in groups:
duplicates_in_this_group = False
index = 0
while not duplicates_in_this_group and len(group) < index:
ant_id = group[index]
if not ant_id in already_seen:
already_seen.append(ant)
else:
duplicates_in_this_group = True
index += 1
if not duplicates_in_this_group:
group.append(groups_not_duplicated)
return groups_not_duplicated
def generate_ants_a_group(ant_structure, team):
"""Genreate a list of ants close to each other. (ally)
Parameters
----------
ant_structure: the structure containing the ants (list)
team : your team number (int)
Return
------
close_ant: a list which contain group of ally ants (list)
Version
-------
specification: Liam Letot (v.1 19/04/21)
implementation: Liam Letot (v.1 03/05/21)
"""
ennemies, allies = seperate_ally_and_ennemy_ants(ant_structure, team)
groups = []
for ant in allies:
current_group = []
for ant_to_check in allies:
if ant['id'] != ant_to_check['id']:
ant_pos = (ant['pos_y'], ant['pos_x'])
ant_to_check_pos = (ant_to_check['pos_y'], ant_to_check['pos_x'])
if compute_distance(ant_pos, ant_to_check_pos) <= 5:
current_group.append(ant['id'])
groups.append(current_group)
already_seen = []
groups_not_duplicated = []
for group in groups:
duplicates_in_this_group = False
index = 0
while not duplicates_in_this_group and len(group) < index:
ant_id = group[index]
if not ant_id in already_seen:
already_seen.append(ant)
else:
duplicates_in_this_group = True
index += 1
if not duplicates_in_this_group:
group.append(groups_not_duplicated)
return groups_not_duplicated
def get_distance_from_base_to_closest_clod(main_structure, anthill_structure, team):
"""Get the distance from the ennemies base to the closest mud.
parameters
----------
main_structure: main structure of the game board (list)
anthill_structure: list of 2 elements containing the anthills information (list)
team: your team number (int)
returns
-------
distance: the distance from ennemies base to the closest mud [x,y] (list)
closest_clod: coordinate of the closest mud (list)
Version
-------
specification: Liam Letot (v.1 19/04/21)
implementation: Liam Letot (v.1 19/04/21)
"""
anthill_pos = (anthill_structure[team - 1]['pos_y'], anthill_structure[team - 1]['pos_x'])
distance = 100
for y in range(len(main_structure)):
for x in range(len(main_structure[y])):
if main_structure[y][x]['clod'] != None:
clod = (y,x)
dist = compute_distance(anthill_pos, clod)
if dist < distance:
distance = dist
closest_clod = clod
return distance, closest_clod
def compute_clods_steal_time(ant_structure, main_structure, ant_id, anthill_structure, team):
"""Compute how long it will take to steal an ennemy's mud.
parameter
---------
ant_structure : structure containing all the ants (list)
main_structure: main structure of the game board (list)
ant_id: the id of the ant who want to steal
anthill_structure: list of 2 elements containing the anthills information (list)
team: team : your team number (int)
return
------
steal_time: the number of turn to steal a mud (int)
Version
-------
specification: Liam Letot (v.1 19/04/21)
implementation: Martin Buchet (v.1 23/04/21)
"""
ant = return_ant_by_id(ant_structure, ant_id)
ennemy_team = get_ennemy_team(team)
ennemy_anthill = anthill_structure[ennemy_team - 1]
return compute_distance((ant['pos_y'], ant['pos_x']), (ennemy_anthill['pos_y'], ennemy_anthill['pos_x']))
def get_closest_clod(ant, main_structure, pos_to_ignore=[]):
"""Get the position of the closest mud from an ally ant.
Parameters
----------
ant: ant to search from (dict)
main_structure: main structure of the game board (list)
pos_to_ignore: pos to ignore (list)
Returns
-------
closest_clod: position of the closest clod (tupple)
distance: distance in cells from the ant (int)
Version
-------
specification: Maxime Dufrasne (v.1 18/4/21)
implementation: Liam Letot (v.1 20/04/21)
"""
ant_pos = (ant['pos_y'], ant['pos_x'])
distance = 100
for y in range(len(main_structure)):
for x in range(len(main_structure[0])):
if main_structure[y][x]['clod'] != None and not (y, x) in pos_to_ignore:
clod= (y,x)
dist = compute_distance(ant_pos, clod)
if dist < distance:
distance = dist
closest_clod = clod
return closest_clod
def seperate_ally_and_ennemy_ants(ant_structure, team):
"""Creates two list with the allies and ennemies ants.
Parameters
----------
ant_structure: list of all the ants (list)
team: which team are we, 1 or 2 (int)
Returns
-------
enemy_ants: list of all the enemy ants (list)
ally_ants: list of all allied ants (list)
Version
-------
specification: Martin Buchet (v.1 19/04/21)
implementation: Youlan Collard
"""
allies = []
enemies = []
for ant in ant_structure:
if ant['team'] == team:
allies.append(ant)
else:
enemies.append(ant)
return enemies, allies
def get_distance_between_anthills(anthill_structure):
"""Returns the distance between both anthills
Parameters
----------
anthill_structure: structure containing both anthills (dict)
Returns
-------
distance: distance in cells (int)
Version
-------
specification: Youlan Collard (v.1 30/04/21)
implementation: Youlan Collard
"""
pos = []
for anthill in anthill_structure:
pos.append((anthill['pos_y'], anthill['pos_x']))
return int(math.dist(pos[0], pos[1]))
def compute_ennemies_ants_near_anthill(anthill_structure, team, ant_structure):
"""Compute the number of ennemies near anthills
Parameters
----------
anthill_structure: structure containing both anthills (list)
team: team number of our ai (int)
ant_structure: structure containing all the ants (list)
Returns
-------
ennemy_number: number of ennemies close to anthills (int)
Version
-------
specification: Youlan Collard (v.1)
implementation: Youlan Collard (v.1)
"""
ally_anthill = anthill_structure[team - 1]
ally_anthill_pos = (ally_anthill['pos_y'], ally_anthill['pos_x'])
ennemies, allies = seperate_ally_and_ennemy_ants(ant_structure, team)
ennemy_number = 0
for ant in ennemies:
ant_pos = (ant['pos_y'], ant['pos_x'])
if compute_distance(ally_anthill_pos, ant_pos) <= 8:
ennemy_number += 1
return ennemy_number
def define_ants_type(allies, enemies, main_structure, danger, anthill_structure, ant_structure, team):
"""Define the type of each ally ants (attack, collect, stealer, defense).
Parameters
----------
allies: list of all allied ants (list)
enemies: list of all enemy ants (list)
main_structure: main structure of the game board (list)
danger: current danger value of the game (int)
anthill_structure: list of 2 elements containing the anthills information (list)
ant_structure: structure containing all the ants (list)
team: team number of our ai (int)
Returns
-------
updated_allied_ants: list of all allied ants with their defined types (dict)
Version
-------
specification: Martin Buchet (v.1 19/04/21)
implementation: Martin Buchet (v.1 27/04/21)
"""
updated_allied_ants = {}
defense_ants = compute_defense_ants(anthill_structure, ant_structure, team)
ennemy_team = get_ennemy_team(team)
for ant in allies:
if ant['level'] == 3:
updated_allied_ants[ant['id']] = 'attack'
if len(allies) <= 2:
for ant in allies:
updated_allied_ants[ant['id']] = 'collect'
elif (len(defense_ants[ennemy_team]) >= len(defense_ants['team'])) or danger >= 30:
for ant in defense_ants[team]:
updated_allied_ants[ant['id']] = 'defense'
elif len(defense_ants[ennemy_team]) < len(defense_ants[team]):
for ant in defense_ants[team]:
updated_allied_ants[ant['id']] = 'stealer'
if len(defense_ants[ennemy_team]) > len(allies)/2:
for ant_id in updated_allied_ants:
if updated_allied_ants[ant_id] == 'attack':
updated_allied_ants[ant_id] = 'collect'
return updated_allied_ants
def define_action_for_ant(main_structure, ant_structure, anthill_structure, ants, team):
"""Define the action a particular ant will do this turn.
Parameters
----------
main_structure: main structure of the game board (list)
ant_structure: structure containing all ants (list)
anthill_structure: anthills (list)
ant: specified ant (dict)
team: team of our ai (int)
Returns
-------
order: order to execute (dict)
Version
-------
specification: Youlan Collard (v.1 19/04/21)
"""
collectors = []
attackers = []
defensers = []
stealers = []
for ant in ants:
ant_type = ants_type[ant['id']]
if ant_type == 'collect':
collectors.append(ant)
elif ant_type == 'attack':
attackers.append(ant)
elif ant_type == 'defenser':
defensers.append(ant)
elif ant_type == 'stealer':
stealers.append(ant)
collectors_order_list = define_collect_order(main_structure, anthill_structure, collectors, team)
attackers_order_list = define_attack_order(main_structure, ant_structure, anthill_structure, ants, team)
defensers_order_list = define_defense_order(ant_structure, anthill_structure, defensers, team)
stealers_order_list = define_stealer_order(main_structure, anthill_structure, stealers, team)
return collectors_order_list + attackers_order_list + defensers_order_list + stealers_order_list
def get_closest_clod_space_from_ally_anthill(main_structure, ant, anthill):
"""Get the closest empty space for a clod near anthill.
Parameters
----------
main_structure: main structure of the game board (list)
ant: ant who needs to go to the space (dict)
anthill: ally anthill (dict)
Returns
-------
space_pos: position of the empty space (tupple)
Version
-------
specificaiton:
implementation:
"""
distance = 1000
space_pos_saved = ()
anthill_pos = (anthill['pos_y'], anthill['pos_x'])
ant_pos = (ant['pos_y'], ant['pos_x'])
for y in range(-1, 2):
for x in range(-1, 2):
space_pos = (anthill_pos[0] + y, anthill_pos[1] + x)
if main_structure[space_pos[0]][space_pos[1]]['clod'] == None:
dist = compute_distance(ant_pos, space_pos)
if dist < distance:
distance = dist
space_pos_saved = space_pos
return space_pos_saved
def define_collect_order(main_structure, anthill_structure, ants, team):
"""Define the order to give to a collector ant
Parameters
----------
main_structure: main structure of the game board (list)
anthill_structure: structure containing the anthills (list)
ants: ants to which give the order (list)
team: team number of our ai (int)
Returns
-------
order_list: dictionnary describing the order (list)
Version
-------
specification: Youlan Collard
"""
order_list = []
already_taken_clods = []
for ant in ants:
order = {'origin': (ant['pos_y'], ant['pos_x'])}
ant_pos = (ant['pos_y'], ant['pos_x'])
if not ant['carrying']:
clod_pos = get_closest_clod(ant, main_structure, already_taken_clods)
already_taken_clods.append(clod_pos)
if not (ant['pos_y'] == clod_pos[0] and ant['pos_x'] == clod_pos[1]):
target = go_in_direction_of_target(ant_pos, (clod_pos[0], clod_pos[1]))
order['target'] = target
order['type'] = 'move'
else:
order['target'] = None
order['type'] = 'lift'
else:
ally_anthill = anthill_structure[team - 1]
closer_empty_space = get_closest_clod_space_from_ally_anthill(main_structure, ant, ally_anthill)
if not (ant['pos_y'] == closer_empty_space[0] and ant['pos_x'] == closer_empty_space[1]):
target = go_in_direction_of_target(ant_pos, closer_empty_space)
order['target'] = target
order['type'] = 'move'
else:
order['target'] = None
order['type'] = 'drop'
order_list.append(order)
return order_list
def define_defense_order(ant_structure, anthill_structure, ants, team):
"""Define the order to give to a defense ant
Parameters
----------
ant_structure: structure containing all the ants
anthill_structure: structure containing the anthill
ants: ants to which give the order (list)
team: team number of our ai (int)
Returns
-------
order_list: dictionnary describing the order (list)
Version
-------
specification: Youlan Collard
"""
order_list = []
ally_anthill = anthill_structure[team - 1]
ally_anthill_pos = (ally_anthill['pos_y'], ally_anthill['pos_x'])
for ant in ants:
order = {}
order['origin'] = (ant['pos_y'], ant['pos_x'])
closest_ennemy_ant_pos, distance = get_closest_ant_of_specified_team(ant_structure, ant, team, 'ennemy')
if distance <= 3:
order['type'] = 'attack'
order['target'] = closest_ennemy_ant_pos
elif distance > 3 and distance < 5:
order['type'] = 'move'
order['target'] = go_in_direction_of_target(order['origin'], closest_ennemy_ant_pos)
elif compute_distance(order['origin'], ally_anthill_pos) > 3:
order['type'] = 'move'
order['target'] = go_in_direction_of_target(order['origin'], ally_anthill_pos)
else:
order['type'] = 'move'
order['target'] = go_in_direction_of_target(order['origin'], closest_ennemy_ant_pos)
order_list.append(order)
return order_list
def get_closest_ant_of_specified_team(ant_structure, ally_ant, team, side):
"""Get the closest ennemy ant from an ant
Parameters
----------
ant_structure: structure containing all the ants (list)
ally_ant: ally ant wishing to get the closest ennemy (dict)
team: team number of our ai (int)
side: side you wish the closest ant to be [ennemy, ally] (str)
Returns
-------
ant_pos: position of the ennemy ant (tupple)
Version
-------
specification: Youlan Collard
"""
ennemies, allies = seperate_ally_and_ennemy_ants(ant_structure, team)
distance = 1000
ant_pos_to_return = None
ally_ant_pos = (ally_ant['pos_y'], ally_ant['pos_x'])
if side == 'ennemy':
for ant in ennemies:
ennemy_pos = [ant['pos_y'], ant['pos_x']]
dist = compute_distance(ally_ant_pos, ennemy_pos)
if dist < distance:
distance = dist
ant_pos_to_return = ennemy_pos
else:
for ant in allies:
ally_pos = (ant['pos_y'], ant['pos_x'])
dist = compute_distance(ally_ant_pos, ally_pos)
if dist < distance:
distance = dist
ant_pos_to_return = ally_pos
return distance, ant_pos_to_return
def define_attack_order(main_structure,ant_structure, anthill_structure, ants, team):
"""Define the order to give to an attack ant
Parameters
----------
main_structure: main structure of the game board (list)
ant_structure: structure containing all ants (list)
anthill_structure: structure containing the anthills (list)
ants: ants to which give the order (list)
team: team number of our ai (int)
Returns
-------
order_list: dictionnary describing the order (list)
Version
-------
specification: Liam Letot (v.1 19/04/21)
implementation: Liam Letot (v.1 27/04/21)
"""
ally_group = generate_ants_a_group(ant_structure, team)
ennemy_group = check_ennemy_ants_near_allies(ant_structure, main_structure, team)
ennemy_ant, ally_ant = seperate_ally_and_ennemy_ants(ant_structure, team)
order_list = []
ant_group = []
for ant in ants:
origin_pos = [ant['pos_y'], ant['pos_x']]
ennemy_life= 100
for group in ally_group:
for solo in group:
if solo == ant['id']:
ant_group = group
if compute_fight_worth(ennemy_group[ant['id']], ant_group, ant_structure) > 0:
order_type = 'attack'
for ant_bad in ennemy_group[ant['id']]:
if ant_bad['health'] < ennemy_life and ant_bad['health'] > 0:
bad_id =ant_bad['id']
for ennemy in ennemy_ant:
if bad_id == ennemy['id']:
ennemy['health'] -= ant['level']
target_pos = [ennemy['pos_y'],ennemy['pos_x']]
else:
order_type = 'move'
dist, target_pos = get_closest_ant_of_specified_team(ant_structure, ant, team, 'ally')
if dist == 0:
dist, target_pos =get_closest_ant_of_specified_team(ant_structure, ant, team, 'ennemy')
order = {}
order['type']= order_type
order['origin']= origin_pos
order['target']= target_pos
order_list.append(order)
return order_list
def define_stealer_order(main_structure, anthill_structure, ants, team):
"""Define the order to give to a stealer ant
Parameters
----------
main_structure: main structure contaning the game board (list)
anthill_structure: anthill structure containing the anthills (list)
ants: ants to which give the order (list)
team: team number of our ai (int)
Returns
-------
order_list: dictionnary describing the order (list)
Version
-------
specification: Youlan Collard, Maxime Dufrasne (v.1 27/4/21)
implementation: Martin Buchet, Maxime Dufrasne (v.1 01/05/21)
"""
order_list = []
enemy_team = get_ennemy_team(team)
clod_pos = get_distance_from_base_to_closest_clod(main_structure, anthill_structure, enemy_team)
ennemy_anthill = anthill_structure[enemy_team - 1]
ennemy_anthill_pos = (ennemy_anthill['pos_y'], ennemy_anthill['pos_x'])
ally_anthill = anthill_structure[team - 1]
ally_anthill_pos = (ally_anthill['pos_y'], ally_anthill['pos_x'])
for ant in ants:
order = {}
order['origin'] = [ant['pos_y'], ant['pos_x']]
if not ant['carrying']:
if not (ant['pos_y'] == clod_pos[0] and ant['pos_x'] == clod_pos[1]):
order['target'] = go_in_direction_of_target(order['origin'], clod_pos)
order['type'] = 'move'
else:
order['target'] = None
order['type'] = 'lift'
else:
if compute_distance(order['origin'], ennemy_anthill_pos) == 1:
delta_y = ant['pos_y'] - ennemy_anthill_pos[0]
delta_x = ant['pos_x'] - ennemy_anthill_pos[1]
if delta_y > 0:
target_y = ant['pos_y'] + 1
elif delta_y < 0:
target_y = ant['pos_y'] - 1
elif delta_y == 0:
target_y = ant['pos_y']
if delta_x > 0:
target_x = ant['pos_x'] + 1
elif delta_x < 0:
target_x = ant['pos_y'] - 1
elif delta_x == 0:
target_x = ant['pos_x']
order['target'] =[ target_y, target_x]
order['type'] = 'move'
else:
order['target'] = None
order['type'] = 'drop'
order_list.append(order)
return order_list
def generate_order(order):
"""Generate a valid order in the form of a string.
Parameters
----------
order: dict order generated by previous function (dict)
Returns
-------
orders: valid order (str)
Version
-------
spécification: Martin Buchet (v.1 19/04/21)
implementation: Liam Letot (v.1 21/04/21)
"""
ant_pos_y = order['origin'][0]
ant_pos_x = order['origin'][1]
if order['type']== ('move' or 'attack'):
target_pos_y = order['target'][0]
target_pos_x = order['target'][1]
orders = str(ant_pos_y + 1) + '-' + str(ant_pos_x +1)
if order['type'] == 'drop':
orders += ':drop '
elif order['type'] == 'lift':