/
rules.pl
62 lines (48 loc) · 2.47 KB
/
rules.pl
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
min_list([H], H).
min_list([H|T], M2) :-
min_list(T, M), M2 is min(H, M).
max_list([H], H).
max_list([H|T], M2) :-
max_list(T, M), M2 is max(H, M).
indexOf([Element|_], Element, 1). % We found the element
indexOf([_|Tail], Element, Index) :-
indexOf(Tail, Element, Index1), % Check in the tail of the list
Index is Index1 + 1. % and increment the resulting index
plus_minus_goals(Game, Line, Score) :-
scoredGoal(game(Game), line(Line), GoalsScored, GoalsReceived),
Score is GoalsScored - GoalsReceived.
received_goals(Game, Score) :-
findall(ReceivedGoal, scoredGoal(game(Game), _, _, ReceivedGoal), ReceivedGoals),
sumlist(ReceivedGoals, Score).
best_offense_game(Line, BestOffenseGame) :-
findall(ScoredGoals, scoredGoal(_, line(Line), ScoredGoals, _), ScoredGoalsForGames),
max_list(ScoredGoalsForGames, MaxScoredGoals),
scoredGoal(game(BestOffenseGame), line(Line), MaxScoredGoals, _).
best_defense_game(Line, BestDefenseGame) :-
findall(ReceivedGoals, scoredGoal(_, line(Line), _, ReceivedGoals), ReceivedGoalsForGames),
min_list(ReceivedGoalsForGames, MinReceivedGoals),
scoredGoal(game(BestDefenseGame), line(Line), _, MinReceivedGoals).
best_goalie_game(BestDefenseGame) :-
findall(Game, scoredGoal(game(Game), _, _, _), GamesAsList),
list_to_set(GamesAsList, GamesAsSet),
maplist(received_goals, GamesAsSet, ReceivedGoalsForGames),
min_list(ReceivedGoalsForGames, MinReceivedGoals),
indexOf(ReceivedGoalsForGames, MinReceivedGoals, Index),
atom_concat(g, Index, BestDefenseGame).
best_plus_minus_game(Line, BestPlusMinusGame) :-
findall(PlusMinus, plus_minus_goals(_, Line, PlusMinus), PlusMinusForGames),
max_list(PlusMinusForGames, MaxPlusMinus),
indexOf(PlusMinusForGames, MaxPlusMinus, Index),
atom_concat(g, Index, BestPlusMinusGame).
line_setup_offense(Line, Players) :-
best_offense_game(Line, BestOffenseGame),
findall(Player, player_plays(player(Player), position(forward), game(BestOffenseGame), line(Line)), Players).
line_setup_defense(Line, Players) :-
best_defense_game(Line, BestDefenseGame),
findall(Player, player_plays(player(Player), position(defender), game(BestDefenseGame), line(Line)), Players).
line_setup_goalie(Line, Player) :-
best_goalie_game(BestGoalieGame),
player_plays(player(Player), position(goalie), game(BestGoalieGame), line(Line)).
line_setup_plus_minus(Line, Player) :-
best_plus_minus_game(Line, BestPlusMinusGame),
player_plays(player(Player), position(_), game(BestPlusMinusGame), line(Line)).