-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamTFTADC.java
133 lines (103 loc) · 4.97 KB
/
TeamTFTADC.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
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
public class TeamTFTADC extends Player {
boolean opponentCanWin;
int trust;
static boolean isTrustModeOn = false;
public byte[] bestMoveBytesRealist;
public byte[] scoreWhiteBytesRealist;
public byte[] scoreBlackBytesRealist;
public byte[] bestMoveBytesCooperative;
public byte[] scoreWhiteBytesCooperative;
public byte[] scoreBlackBytesCooperative;
public byte[] bestMoveBytesTruster;
public byte[] scoreWhiteBytesTruster;
public byte[] scoreBlackBytesTruster;
public final int BETRAYAL_DELTA = 6;
public final int COOPERATION_DELTA = 1;
public TeamTFTADC(int maxNumMoves) {
TeamRational teamRationalRealist = TeamRational.createRealist(maxNumMoves);
TeamRational teamRationalCooperative = TeamRational.createCooperative(maxNumMoves);
TeamRational teamRationalTruster = TeamRational.createTruster(maxNumMoves);
bestMoveBytesRealist = teamRationalRealist.bestMoveBytes;
scoreWhiteBytesRealist = teamRationalRealist.scoreWhiteBytes;
scoreBlackBytesRealist = teamRationalRealist.scoreBlackBytes;
bestMoveBytesCooperative = teamRationalCooperative.bestMoveBytes;
scoreWhiteBytesCooperative = teamRationalCooperative.scoreWhiteBytes;
scoreBlackBytesCooperative = teamRationalCooperative.scoreBlackBytes;
bestMoveBytesTruster = teamRationalTruster.bestMoveBytes;
scoreWhiteBytesTruster = teamRationalTruster.scoreWhiteBytes;
scoreBlackBytesTruster = teamRationalTruster.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);
isTrustModeOn = (theirRookIsAlive && !theirKingIsAlive && myKingIsAlive && myRookIsAlive);
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 nodeCooperative = new TeamRational.Node(bestMoveBytesCooperative[boardPosition.toInt()],
// scoreWhiteBytesCooperative[boardPosition.toInt()], scoreBlackBytesCooperative[boardPosition.toInt()]);
// int bestScoreCooperative = nodeCooperative.getScore(currentPlayerColour);
TeamRational.Node nodeCooperative = new TeamRational.Node(bestMoveBytesCooperative[boardPosition.toInt()],
scoreWhiteBytesCooperative[boardPosition.toInt()], scoreBlackBytesCooperative[boardPosition.toInt()]);
int bestScoreCooperative = nodeCooperative.getScore(currentPlayerColour);
TeamRational.Node nodeTruster = new TeamRational.Node(bestMoveBytesTruster[boardPosition.toInt()],
scoreWhiteBytesTruster[boardPosition.toInt()], scoreBlackBytesTruster[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:
//System.out.println(bestScoreRealist+" "+bestScoreCooperative+" "+bestScoreTruster);
if (bestScoreRealist != 2 && bestScoreCooperative == 3 && trust > 0) {
return nodeCooperative.bestMove;
} else if (bestScoreRealist != 2 && bestScoreCooperative == 10 && isTrustModeOn && 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;
}
}