-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamRank3.java
243 lines (207 loc) · 8.44 KB
/
TeamRank3.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
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
public class TeamRank3 extends Player {
// for DoubleAgent
private Handshaker shaker;
boolean opponentHadWinningPosition; //set to true if opponent can force a win at any point in the match.
int trust;
public byte[] bestMoveBytesRealist, scoreWhiteBytesRealist, scoreBlackBytesRealist;
public byte[] bestMoveBytesCooperative, scoreWhiteBytesCooperative, scoreBlackBytesCooperative;
public int BETRAYAL_DELTA = 1;
public int COOPERATION_DELTA = 1;
public int IRRATIONALITY_DELTA = 2;
public int SUBOPTIMALITY_DELTA = 1;
int isOpponentMonkey = 0;
boolean isMonkey = false;
boolean isOpponentNihilist = false;
boolean isOpponentOptimist = false;
boolean isOpponentPessimist = false;
boolean isOpponentQueller = false;
boolean isOpponentRealist = false;
boolean isOpponentScrapper = false;
boolean isOpponentTruster = false;
boolean isOpponentUtilitarian = false;
boolean opponentCanCaptureKing = false;
boolean opponentCanCaptureRook = false;
int matchNum;
public TeamRank3(int maxNumMoves) {
TeamRational teamRationalRealist = TeamRational.createRealist(maxNumMoves);
/*TeamRationalRealist has the following beliefs as P1:
* P1\P2 | W | L |
* -------------------
* W | 2,2 | 3,0 |
* -------------------
* L | 0,3 | 1,1 |
* -------------------*/
// Take the data that we need from teamRationalRealist:
bestMoveBytesRealist = teamRationalRealist.bestMoveBytes;
scoreWhiteBytesRealist = teamRationalRealist.scoreWhiteBytes;
scoreBlackBytesRealist = teamRationalRealist.scoreBlackBytes;
// then teamRationalRealist will be deconstructed.
TeamRational teamRationalCooperative = TeamRational.createCooperative(maxNumMoves);
/*TeamCooperative has the following beliefs as P1:
* P1\P2 | W | L |
* -------------------
* W | 3,3 | 2,0 |
* -------------------
* L | 0,2 | 1,1 |
* -------------------*/
// Take the data that we need from teamRationalCooperative:
bestMoveBytesCooperative = teamRationalCooperative.bestMoveBytes;
scoreWhiteBytesCooperative = teamRationalCooperative.scoreWhiteBytes;
scoreBlackBytesCooperative = teamRationalCooperative.scoreBlackBytes;
// then teamRationalCooperative will be deconstructed.
shaker = Handshaker.createHandshakeAccepter();
}
public void prepareForSeries() {
trust = 1;
isOpponentMonkey = 0;
matchNum = 0;
isMonkey = false;
shaker.handshakePrepareForSeries();
}
public void prepareForMatch() {
BoardPosition boardPosition;
opponentHadWinningPosition = false;
matchNum += 1;
// Take note if opponent starts in winning position:
if (myColour == BLACK) {
boardPosition = toBoardPosition();
if (scoreBlackBytesRealist[boardPosition.toInt()] == 0) {
opponentHadWinningPosition = true;
}
}
shaker.handshakePrepareForMatch(toBoardPosition());
}
public void receiveMatchOutcome(int matchOutcome) {
//Convert to a more reasonable format first:
int myMatchPayoff = outcomeToPayoff(matchOutcome);
trust = updateTrust(trust, myMatchPayoff);
//System.out.println("Match "+matchNum+" This is a monkey: "+isMonkey+" Monkey score is "+isOpponentMonkey);
shaker.handshakeReceiveMatchOutcome(matchOutcome, toBoardPosition());
}
public int updateTrust(int trust, int myMatchPayoff) {
if (trust>0) { // I trusted you! Let's see how you did:
if (myMatchPayoff < 2) {
// I didn't take your king? I trust you less now.
return trust - BETRAYAL_DELTA;
} else if (myMatchPayoff == 3) {
// I tried to tie, but I won!!! I don't trust that you know what you're doing.
//isOpponentMonkey += 2;
return trust - IRRATIONALITY_DELTA;
}
} else if (opponentHadWinningPosition && myMatchPayoff == 2) {
// I didn't trust you. I'm very picky about restoring trust!
// You gave up a win for a tie? I trust you more now.
return trust + COOPERATION_DELTA;
} else if (opponentHadWinningPosition && myMatchPayoff != 2) {
// I don't believe that you needed to let me win.
//isOpponentMonkey += 4;
return trust - SUBOPTIMALITY_DELTA;
}
return trust;
}
public void updateOpponentCanCaptureKingAndRook() {
// their king captures our king
if(theirKingIsAlive && myKingIsAlive){
if ((theirKingRow == myKingRow && Math.abs(theirKingColumn - myKingColumn)==1) ||
(theirKingColumn == myKingColumn && Math.abs(theirKingRow - myKingRow)==1) ||
(Math.abs(theirKingColumn - myKingColumn)==1 && Math.abs(theirKingRow - myKingRow)==1)) {
opponentCanCaptureKing = true;
}
}
// their rook captures our king
if(theirRookIsAlive && myKingIsAlive) {
if((theirRookRow==myKingRow && myRookRow != myKingRow) ||
(theirRookColumn==myKingColumn && myRookColumn != myKingColumn)) {
opponentCanCaptureKing = true;
}
}
// their king captures our rook
if(theirKingIsAlive && myRookIsAlive) {
if ((theirKingRow == myRookRow && Math.abs(theirKingColumn - myRookColumn)==1) ||
(theirKingColumn == myRookColumn && Math.abs(theirKingRow - myRookRow)==1) ||
(Math.abs(theirKingColumn - myRookColumn)==1 && Math.abs(theirKingRow - myRookRow)==1)) {
opponentCanCaptureRook = true;
}
}
// their rook captures our rook
if(theirRookIsAlive && myRookIsAlive) {
if((theirRookRow==myRookRow && theirRookRow != theirKingRow) ||
(theirRookColumn==myRookColumn && theirRookColumn != theirKingColumn)) {
opponentCanCaptureRook = true;
}
}
}
// Against DoubleAgent ONLY!!!
public MoveDescription chooseMove() {
BoardPosition currentBoardPosition = toBoardPosition();
// update shaker with opponent move
shaker.updateTheirMove(currentBoardPosition);
MoveDescription myMove;
if (shaker.shouldSendHandshakeMove()) {
myMove=Handshaker.getHandshakeMove(currentBoardPosition);
} else {
myMove = internalChooseMove();
}
shaker.receiveMyMove(myMove);
return myMove;
}
// double check if this is working against the other players
// the original chooseMove: now this is for all the non-doubleAgent player
public MoveDescription internalChooseMove() {
BoardPosition boardPosition = toBoardPosition();
int currentPlayerColour = (boardPosition.numMovesPlayed % 2 == 0) ? WHITE : BLACK;
opponentHadWinningPosition = updateOpponentHadWinningPosition(boardPosition, currentPlayerColour);
updateOpponentCanCaptureKingAndRook();
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);
if (opponentCanCaptureKing && !myKingIsAlive) {
opponentCanCaptureKing = false;
if (trust < 0) {
//System.out.println("He ate my king! in Match "+matchNum+" Monkey score added! because trust is "+trust);
isOpponentMonkey += 4;
}
}
if (opponentCanCaptureRook && !myRookIsAlive) {
opponentCanCaptureRook = false;
if (trust < 0) {
//System.out.println("He ate my rook! in Match "+matchNum+" Monkey score added! because trust is "+trust);
isOpponentMonkey += 6;
}
}
if(isOpponentMonkey >= 20) isMonkey = true;
//if (bestScoreRealist==2 || (isMonkey == true && matchNum <= 12)) {
if (bestScoreRealist==2) {
//System.out.println("Monkey score is "+isOpponentMonkey);
// If the move forces a tie, play it in all cases (to be safe):
return nodeRealist.bestMove;
} else if (trust > 0 && bestScoreCooperative == 3) {
// If trust remains, play cooperatively for a tie:
return nodeCooperative.bestMove;
} else {
// In all other cases, play realistically:
return nodeRealist.bestMove;
}
}
public boolean updateOpponentHadWinningPosition(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 (opponentHadWinningPosition || bestScoreRealist == 0) {
return true;
}
return false;
}
}