-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamMoreMoreTFT.java
108 lines (84 loc) · 3.69 KB
/
TeamMoreMoreTFT.java
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
public class TeamMoreMoreTFT extends Player {
boolean opponentCanWin;
int trust;
public byte[] bestMoveBytesRealist;
public byte[] scoreWhiteBytesRealist;
public byte[] scoreBlackBytesRealist;
public byte[] bestMoveBytesCooperative;
public byte[] scoreWhiteBytesCooperative;
public byte[] scoreBlackBytesCooperative;
public final int BETRAYAL_DELTA = 3;
public final int COOPERATION_DELTA = 1;
public TeamMoreMoreTFT(int maxNumMoves) {
TeamRational teamRationalRealist = TeamRational.createRealist(maxNumMoves);
TeamRational teamRationalCooperative = TeamRational.createCooperative(maxNumMoves);
bestMoveBytesRealist = teamRationalRealist.bestMoveBytes;
scoreWhiteBytesRealist = teamRationalRealist.scoreWhiteBytes;
scoreBlackBytesRealist = teamRationalRealist.scoreBlackBytes;
bestMoveBytesCooperative = teamRationalCooperative.bestMoveBytes;
scoreWhiteBytesCooperative = teamRationalCooperative.scoreWhiteBytes;
scoreBlackBytesCooperative = teamRationalCooperative.scoreBlackBytes;
opponentCanWin = false;
trust = 1;
}
public void prepareForSeries() {
trust = 1;
}
public void prepareForMatch() {
BoardPosition boardPosition;
// Initial belief about whether opponent can win; if they can but a tie
// happens, we increase trust.
opponentCanWin = false; // The only case we can determine right now is
// if the opponent is white:
if (myColour == BLACK) {
boardPosition = toBoardPosition();
if (scoreBlackBytesRealist[boardPosition.toInt()] == 0) {
opponentCanWin = true;
}
}
}
public void receiveMatchOutcome(int matchOutcome) {
int matchPayoff = outcomeToPayoff(matchOutcome);
trust = updateTrust(trust, matchPayoff);
}
public int updateTrust(int trust, int matchPayoff) {
if (matchPayoff < 2) {
// I didn't take your king? I don't trust you anymore.
return trust - BETRAYAL_DELTA;
} else if (opponentCanWin && matchPayoff == 2) {
// You gave up a win? I trust you more now.
return trust + COOPERATION_DELTA;
}
return trust;
}
public MoveDescription chooseMove() {
BoardPosition boardPosition = toBoardPosition();
int currentPlayerColour = (boardPosition.numMovesPlayed % 2 == 0) ? WHITE : BLACK;
opponentCanWin = updateOpponentCanWin(boardPosition, currentPlayerColour);
return bestMoveFromTrust(boardPosition, currentPlayerColour);
}
public MoveDescription bestMoveFromTrust(BoardPosition boardPosition, int currentPlayerColour) {
TeamRational.Node nodeRealist = new TeamRational.Node(bestMoveBytesRealist[boardPosition.toInt()],
scoreWhiteBytesRealist[boardPosition.toInt()], scoreBlackBytesRealist[boardPosition.toInt()]);
int bestScoreRealist = nodeRealist.getScore(currentPlayerColour);
TeamRational.Node nodeTruster = new TeamRational.Node(bestMoveBytesCooperative[boardPosition.toInt()],
scoreWhiteBytesCooperative[boardPosition.toInt()], scoreBlackBytesCooperative[boardPosition.toInt()]);
int bestScoreTruster = nodeTruster.getScore(currentPlayerColour);
// If you cannot force a tie, and it is still possible to tie, and trust
// remains, then play trustingly:
if (bestScoreRealist != 2 && bestScoreTruster == 3 && trust > 0) {
return nodeTruster.bestMove;
} else { // In all other cases, play realistically:
return nodeRealist.bestMove;
}
}
public boolean updateOpponentCanWin(BoardPosition boardPosition, int currentPlayerColour) {
TeamRational.Node nodeRealist = new TeamRational.Node(bestMoveBytesRealist[boardPosition.toInt()],
scoreWhiteBytesRealist[boardPosition.toInt()], scoreBlackBytesRealist[boardPosition.toInt()]);
int bestScoreRealist = nodeRealist.getScore(currentPlayerColour);
if (!opponentCanWin && bestScoreRealist == 0) {
return true;
}
return false;
}
}