-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamTitForTat.java
128 lines (104 loc) · 4.42 KB
/
TeamTitForTat.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
public class TeamTitForTat extends Player {
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 TeamTitForTat(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.
}
public void prepareForSeries() {
trust = 1;
}
public void prepareForMatch() {
BoardPosition boardPosition;
opponentHadWinningPosition = false;
// Take note if opponent starts in winning position:
if (myColour == BLACK) {
boardPosition = toBoardPosition();
if (scoreBlackBytesRealist[boardPosition.toInt()] == 0) {
opponentHadWinningPosition = true;
}
}
}
public void receiveMatchOutcome(int matchOutcome) {
//Convert to a more reasonable format first:
int myMatchPayoff = outcomeToPayoff(matchOutcome);
trust = updateTrust(trust, myMatchPayoff);
}
public int updateTrust(int trust, int myMatchPayoff) {
if (myMatchPayoff < 2) {
// I didn't take your king? I trust you less now.
return trust - BETRAYAL_DELTA;
} else if (opponentHadWinningPosition && myMatchPayoff >= 2) {
// You gave up a win for a tie/loss? 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;
opponentHadWinningPosition = updateOpponentHadWinningPosition(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 nodeCooperative = new TeamRational.Node(
bestMoveBytesCooperative[boardPosition.toInt()],
scoreWhiteBytesCooperative[boardPosition.toInt()],
scoreBlackBytesCooperative[boardPosition.toInt()]);
int bestScoreCooperative = nodeCooperative.getScore(currentPlayerColour);
if (bestScoreRealist==2) {
// 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;
}
}