-
Notifications
You must be signed in to change notification settings - Fork 1
/
rulePlayer.xc
86 lines (83 loc) · 2.95 KB
/
rulePlayer.xc
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
#include <state.xh>
#include <driver.xh>
#include <players.xh>
#include <stdlib.h>
#include <stdbool.h>
Player makeRulePlayer() {
return (Player){"rule", lambda (State s, const Hand h, const Hand hands[], const Hand discard, const unsigned handSizes[], TurnInfo turn, vector<Action> actions) -> unsigned {
PlayerId p = turn.player;
// Finish if possible
for (unsigned i = 0; i < actions.size; i++) {
match (actions[i]) {
Play(_, ms) -> {
if (query MS is ms, member(MoveDirect(Out(_), Finish(_, _)), MS) {}) {
return i;
}
}
}
}
// Move out if possible
for (unsigned i = 0; i < actions.size; i++) {
match (actions[i]) {
Play(_, ms) -> {
if (query MS is ms, St(_, false, B, _) is s, P is p,
I is (P * SECTOR_SIZE), \+ mapContains(B, Out(I), P),
member(MoveOut(_), MS) {}) {
return i;
}
if (query MS is ms, St(NP, true, B, _) is s, P1 is p, P2 is (partner(NP, P1)),
member(MoveOut(P), MS), I is (P * SECTOR_SIZE),
\+ mapContains(B, Out(I), P1), \+ mapContains(B, Out(I), P2) {}) {
return i;
}
}
}
}
// Advance within the finish block if possible
for (unsigned i = 0; i < actions.size; i++) {
match (actions[i]) {
Play(_, ms) -> {
if (query MS is ms, member(MoveDirect(_, Finish(_, _)), MS) {}) {
return i;
}
}
}
}
// Kill another player's piece if possible
for (unsigned i = 0; i < actions.size; i++) {
match (actions[i]) {
Play(_, ms) -> {
if (query MS is ms, St(_, false, B, _) is s, P1 is p,
member(MoveDirect(_, X), MS),
mapContains(B, X, P2), P1 =\= P2 {}) {
return i;
}
if (query MS is ms, St(NP, true, B, _) is s, P1 is p,
member(MoveDirect(_, X), MS),
mapContains(B, X, P2), P1 =\= P2,
P1 =\= (partner(NP, P1)) {}) {
return i;
}
}
}
}
// Move a piece that isn't home if possible
for (unsigned i = 0; i < actions.size; i++) {
match (actions[i]) {
Play(_, ms) -> {
if (query MS is ms, St(_, false, _, _) is s, P is p,
I is (P * SECTOR_SIZE), \+ member(MoveDirect(Out(I), _), MS) {}) {
return i;
}
if (query MS is ms, St(NP, true, _, _) is s, P1 is p, P2 is (partner(NP, P1)),
I1 is (P1 * SECTOR_SIZE), I2 is (P2 * SECTOR_SIZE),
\+ member(MoveDirect(Out(I1), _), MS), \+ member(MoveDirect(Out(I2), _), MS) {}) {
return i;
}
}
}
}
return rand() % actions.size;
}, lambda (State s, TurnInfo turn, Action action) -> void {}
};
}