-
Notifications
You must be signed in to change notification settings - Fork 3
/
common_logic.dlv
326 lines (241 loc) · 11.5 KB
/
common_logic.dlv
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
% ====================================================================
% COMMON LOGIC
% ====================================================================
curr_turn(T, P) :- T = #max{Turn : turn(Turn,_) }, turn(T,P).
% current player, the one that is playing and making decisions
curr_player(P) :- curr_turn(_, P).
% all other players, can be viewed as the opponents
other_player(P) :- player(P), curr_player(CP), P != CP.
curr_player_territory(T, Territory, Troops) :-
territory_control(T, Territory, Player, Troops),
curr_turn(T, Player).
other_player_territory(T, Territory, Player, Troops) :-
territory_control(T, Territory, Player, Troops),
curr_turn(T, Player1), Player != Player1.
player_territories_count(T, Player, Count) :-
curr_turn(T, _),
player(Player),
Count = #count{Territory : territory_control(T, Territory, Player, _)}.
player_troops_count(T, Player, Count) :-
curr_turn(T, _),
player(Player),
Count = #sum{Troops, Territory : territory_control(T, Territory, Player, Troops)}.
curr_player_troops_count(T, Player, Count) :-
player_troops_count(T, Player, Count),
curr_turn(T, Player).
other_player_troops_count(T, Player, Count) :-
player_troops_count(T, Player, Count),
other_player(Player),
curr_turn(T, _).
% count of territories owned by a generic player in a continent
player_territories_in_continent(T, Player, Continent, Count) :-
curr_turn(T, _),
player(Player),
continent(Continent, _),
Count = #count{Territory : territory_control(T, Territory, Player, _), territory(Territory, Continent) }.
% count of the missing territories player in a continent
player_missing_territories_in_continent(T, Player, Continent, Missing) :-
curr_turn(T, _),
player(Player),
continent_size(Continent, _, Size, _),
player_territories_in_continent(T, Player, Continent, Count),
Missing = Size - Count.
% true if player controls all the territories in a continent
player_continent_control(T, Player, Continent, Bonus) :-
curr_turn(T, _),
player(Player),
continent_size(Continent, Bonus, Size, _),
player_missing_territories_in_continent(T, Player, Continent, Count),
Count = Size.
% ______________________________ BONUS ______________________________
% bonus given by the control of continents
player_continent_bonus(T, Player, ContinentBonus) :-
curr_turn(T, _),
player(Player),
ContinentBonus = #sum{Bonus, Continent : player_continent_control(T, Player, Continent, Bonus) }.
% bonus given by the number of territories owned by a player
player_territories_bonus(T, Player, TerritoriesBonus) :-
curr_turn(T, _),
player(Player),
player_territories_count(T, Player, TerritoriesCount),
TerritoriesBonus = TerritoriesCount / 3.
% final bonus of a player
player_bonus(T, Player, Bonus) :-
curr_turn(T, _),
player(Player),
player_continent_bonus(T, Player, ContinentBonus),
player_territories_bonus(T, Player, TerritoriesBonus),
Bonus = ContinentBonus + TerritoriesBonus.
% _____________________________________________________________________
% count of territories adjacent to a territory owned by the same player
adjacent_friendly_territories(T, Territory, Player, N) :-
territory_control(T, Territory, Player, _),
N = #count{T1 : territory_control(T, T1, Player, _), connection(Territory, T1)}.
% count of territories adjacent to a territory owned by different players
adjacent_enemy_territories(T, Territory, PlayerOwner, N) :-
territory_control(T, Territory, PlayerOwner, _),
N = #count{T1 :
territory_control(T, T1, Player, _),
connection(Territory, T1),
PlayerOwner != Player
}.
% count of territories adjacent to a territory owned by a specific EnemyPlayer
adjacent_enemy_player_territories(T, Territory, PlayerOwner, EnemyPlayer, N) :-
territory_control(T, Territory, PlayerOwner, _),
player(EnemyPlayer),
N = #count{T1 : territory_control(T, T1, EnemyPlayer, _), connection(Territory, T1)},
PlayerOwner != EnemyPlayer.
% sum of troops adjacent to a territory owned by the same player
adjacent_friendly_troops(T, Territory, Player, N) :-
territory_control(T, Territory, Player, _),
N = #sum{Troops, T1 : territory_control(T, T1, Player, Troops), connection(Territory, T1)}.
% sum of enemy troops of adjacent to a territory
adjacent_enemy_troops(T, Territory, PlayerOwner, N) :-
territory_control(T, Territory, PlayerOwner, _),
N = #sum{Troops, T1 :
territory_control(T, T1, Player, Troops),
connection(Territory, T1),
PlayerOwner != Player
}.
adjacent_enemy_player_troops(T, Territory, PlayerOwner, Player, N) :-
territory_control(T, Territory, PlayerOwner, _),
player(Player),
N = #sum{Troops, T1 : territory_control(T, T1, Player, Troops), connection(Territory, T1)},
PlayerOwner != Player.
difference_troops_territory(T, Territory, PlayerOwner, Diff) :-
territory_control(T, Territory, PlayerOwner, Friendly),
adjacent_enemy_troops(T, Territory, PlayerOwner, Enemies),
Diff = Friendly - Enemies.
difference_max_enemy_troops_territory(T, Territory, PlayerOwner, Diff) :-
fronteer_territory(T, Territory, PlayerOwner),
territory_control(T, Territory, PlayerOwner, Friendly),
Enemies = #max{Troops, T1 :
territory_control(T, T1, PlayerEnemy, Troops),
connection(Territory, T1),
PlayerEnemy != PlayerOwner},
Diff = Friendly - Enemies.
difference_max_enemy_troops_territory(T, Territory, Player, Troops) :-
internal_territory(T, Territory, Player),
territory_control(T, Territory, Player, Troops).
difference_troops_territory_inverted(T, Territory, PlayerOwner, Diff) :-
territory_control(T, Territory, PlayerOwner, Friendly),
adjacent_enemy_troops(T, Territory, PlayerOwner, Enemies),
Diff = Enemies - Friendly.
fronteer_territory(T, Territory, Player) :-
adjacent_enemy_territories(T, Territory, Player, N),
N > 0.
internal_territory(T, Territory, Player) :-
adjacent_enemy_territories(T, Territory, Player, N),
N = 0.
fronteer_territory_count(T, Player, Count):-
curr_turn(T, Player),
#count{Territory : fronteer_territory(T, Territory, Player)} = Count.
% ______________________________ ISLANDS ______________________________
% territory_island(T, Id, Territory, Player).
island_id(T, Id, Player) :- territory_island(T, Id, _, Player).
territory_island_count(T, Id, Player, Count) :-
island_id(T, Id, Player),
Count = #count{Territory : territory_island(T, Id, Territory, Player)}.
territory_island_internal(T, Id, Player, Territory) :-
territory_island(T, Id, Territory, Player),
internal_territory(T, Territory, Player).
island_internal_troops(T, Id, Player, Territory, Troops) :-
territory_island_internal(T, Id, Player, Territory),
territory_control(T, Territory, Player, Troops).
territory_island_fronteer(T, Id, Player, Territory) :-
territory_island(T, Id, Territory, Player),
fronteer_territory(T, Territory, Player).
island_fronteer_troops(T, Id, Player, Territory, Troops) :-
territory_island_fronteer(T, Id, Player, Territory),
territory_control(T, Territory, Player, Troops).
island_with_internal_territories(T, Id, Player) :-
territory_island_internal(T, Id, Player, _).
island_without_internal_territories(T, Id, Player) :-
island_id(T, Id, Player),
not island_with_internal_territories(T, Id, Player).
territory_island_internal_count(T, Id, Player, 0) :-
island_without_internal_territories(T, Id, Player).
territory_island_internal_count(T, Id, Player, Count) :-
island_id(T, Id, Player),
Count = #count{Territory : territory_island_internal(T, Id, Player, Territory) }.
territory_island_fronteer_count(T, Id, Player, Count) :-
island_id(T, Id, Player),
Count = #count{Territory : territory_island_fronteer(T, Id, Player, Territory) }.
% non ci sono territori di un continente che sono fuori dall'isola'
continent_not_in_island(T, Id, Player, Continent, Bonus) :-
continent(Continent, Bonus),
territory(Territory, Continent),
island_id(T, Id, Player),
not territory_island(T, Id, Territory, Player).
continent_in_island(T, Id, Player, Continent, Bonus) :-
continent(Continent, Bonus),
island_id(T, Id, Player),
not continent_not_in_island(T, Id, Player, Continent, Bonus).
% bigger is better
% indicates how much the island is worth to the player
island_value(T, Id, Player, Value) :-
island_id(T, Id, Player),
territory_island_internal_count(T, Id, Player, TIIC),
territory_island_fronteer_count(T, Id, Player, TIFC),
% max_continent_bonus(MaxContinentBonus),
ContinentBonus = #sum{Bonus, Continent : continent_in_island(T, Id, Player, Continent, Bonus) },
% constants
territory_island_internal_weight(TIIW),
territory_island_fronteer_weight(TIFW),
territory_island_continent_bonus_weight(TICBW),
% formula
Value = (TIIC * TIIW) +
(TIFC * TIFW) +
(ContinentBonus * TICBW).
% smaller is better
island_value_index(T, Id, Player, ValueIndex) :-
island_value(T, Id, Player, Value),
ValueIndex = -Value.
best_island_value(T, Id, Player, Value) :-
island_value(T, Id, Player, Value),
Value = #max{ValueIndex1, Id1 : island_value(T, Id1, Player, ValueIndex1)}.
% territory_island(T, Id, Territory, Player)
island_weakest_fronteer_troops_diff(T, Id, Player, MaxDiff) :-
island_id(T, Id, Player),
% I take the minimum Diff, which is (probably) the smallest negative number
MaxDiff = #min{Diff, Territory :
difference_troops_territory(T, Territory, Player, Diff),
territory_island(T, Id, Territory, Player)
}.
island_weakest_fronteer(T, Id, Player, Territory, TroopsDiff) :-
island_weakest_fronteer_troops_diff(T, Id, Player, TroopsDiff),
difference_troops_territory(T, Territory, Player, TroopsDiff).
island_strongest_internal_troops_diff(T, Id, Player, MaxDiff) :-
island_id(T, Id, Player),
MaxDiff = #max{Diff, Territory :
difference_troops_territory(T, Territory, Player, Diff),
territory_island(T, Id, Territory, Player)
}.
island_strongest_internal(T, Id, Player, Territory, TroopsDiff) :-
island_strongest_internal_troops_diff(T, Id, Player, TroopsDiff),
difference_troops_territory(T, Territory, Player, TroopsDiff).
% -----------------------------------------------------------------------
adjacent_available_troops_different_player(T, Territory, Player, AvailableTroops) :-
curr_turn(T, _),
player(Player),
territory_control(T, Territory, _, _),
AvailableTroops = #sum{ AvailableTroops1, Territory1 :
territory_control(T, Territory1, Player1, Troops),
connection(Territory, Territory1),
Player1 != Player,
AvailableTroops1 = Troops - 1
}.
adjacent_territories_different_player(T, Territory, Player, Territories) :-
curr_turn(T, _),
player(Player),
territory_control(T, Territory, _, _),
Territories = #count{ Territory1 :
territory_control(T, Territory1, Player1, Troops),
connection(Territory, Territory1),
Player1 != Player
}.
% troops on a territory - the available troops from other players that can attack territory
territory_adjecent_available_troops_diff(T, Territory, Player, TroopsDiff) :-
territory_control(T, Territory, Player, Troops),
adjacent_available_troops_different_player(T, Territory, Player, AvailableEnemyTroops),
TroopsDiff = Troops - AvailableEnemyTroops.