-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChineseCheckerTest.java
More file actions
161 lines (132 loc) · 5.26 KB
/
Copy pathChineseCheckerTest.java
File metadata and controls
161 lines (132 loc) · 5.26 KB
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
import java.util.*;
public class ChineseCheckerTest {
static Random rand = new Random(System.currentTimeMillis());
public static void main(String[] args) throws CloneNotSupportedException {
final int maximumExpansionCount = 100000;
AdversarialSearch search = new AdversarialGreedySearch();
//AdversarialSearch search = new AlphaBetaPruning(1);
//BoardGameAgent agent1 = new ChineseCheckerDuckAgent(maximumExpansionCount);
//BoardGameAgent agent1 = new ChineseCheckerIdiotAgent(maximumExpansionCount);
//BoardGameAgent agent1 = new ChineseCheckerImbecileAgent(maximumExpansionCount);
BoardGameAgent agent1 = new ChineseCheckerMoronAgent(maximumExpansionCount);
//BoardGameAgent agent2 = new ChineseCheckerIdiotAgent(maximumExpansionCount);
//BoardGameAgent agent2 = new ChineseCheckerImbecileAgent(maximumExpansionCount);
//BoardGameAgent agent2 = new ChineseCheckerMoronAgent(maximumExpansionCount);
BoardGameAgent agent2 = new ChineseCheckerDuckAgent(maximumExpansionCount);
if (agent1 instanceof ChineseCheckerIdiotAgent && agent2 instanceof ChineseCheckerIdiotAgent) {
System.err.println("Watching two idiots playing a game is silly!");
return;
}
int boardSize = 8;
ChineseCheckerState initialState = new ChineseCheckerState(boardSize);
System.out.println("Game Board");
System.out.println("----------");
System.out.println(initialState);
System.out.println();
int agent1WinCount = 0;
int agent2WinCount = 0;
int gamePlayCount = 0;
System.out.println("Player 1 : P1 = " + agent1);
System.out.println("Player 2 : P2 = " + agent2);
System.out.println();
System.out.println("Playing with shuffled boards:");
int tryCount = 0;
int playCount = 100;
while (gamePlayCount < playCount) {
System.out.print(".");
if ((++tryCount) % 25 == 0) {
if (gamePlayCount > 0) {
System.out.print(", [P1 " + getPercentage(agent1WinCount, gamePlayCount) + "%] versus");
System.out.print(" [P2 " + getPercentage(agent2WinCount, gamePlayCount) + "%]");
}
System.out.println();
}
int maximumRandomMoveCount = 10;
int randomMoveCount = rand.nextInt(maximumRandomMoveCount + 1);
BoardState shuffledBoard = makeRandomMove(initialState, randomMoveCount);
if (shuffledBoard != null) {
BoardState switchedGameBoard = shuffledBoard.getSwitchedBoard();
BoardState endGame = playGame(shuffledBoard, search, agent1, agent2);
if (endGame != null) {
if (endGame.wins(Player.One)) {
//System.out.print(shuffledBoard);
//System.out.println("-- player 1 wins");
//System.out.println(endGame);
agent1WinCount++;
gamePlayCount++;
}
if (endGame.wins(Player.Two)) {
//System.out.print(shuffledBoard);
//System.out.println("-- player 2 wins");
//System.out.println(endGame);
agent2WinCount++;
gamePlayCount++;
}
}
endGame = playGame(switchedGameBoard, search, agent1, agent2);
if (endGame != null) {
if (endGame.wins(Player.One)) {
//System.out.print(switchedGameBoard);
//System.out.println("__ player 1 wins __ (switched)");
//System.out.println(endGame);
agent1WinCount++;
gamePlayCount++;
}
if (endGame.wins(Player.Two)) {
//System.out.print(switchedGameBoard);
//System.out.println("__ player 2 wins __ (switched)");
//System.out.println(endGame);
agent2WinCount++;
gamePlayCount++;
}
}
}
}
if (gamePlayCount > 0) {
System.out.println("\n");
System.out.println(agent1 + " (player 1) wins " + getPercentage(agent1WinCount, gamePlayCount) + "%");
System.out.println(agent2 + " (player 2) wins " + getPercentage(agent2WinCount, gamePlayCount) + "%");
}
}
static double getPercentage(int count, int totalCount)
{
double percentage = (count / (double)totalCount * 100.0);
percentage = (int)(percentage * 100)/100.0;
return percentage;
}
static BoardState playGame(BoardState gameBoard, AdversarialSearch adversarialSearch, BoardGameAgent agent1, BoardGameAgent agent2)
{
BoardState currentBoard = gameBoard;
int maximumTurnCount = 1000;
for (int i=0; i<maximumTurnCount; i++) {
// player 1
currentBoard = adversarialSearch.getNextMove(currentBoard, agent1, Player.One).nextMove;
if (currentBoard == null || currentBoard.isTerminal()) {
return currentBoard;
}
// player 2
currentBoard = adversarialSearch.getNextMove(currentBoard, agent2, Player.Two).nextMove;
if (currentBoard == null || currentBoard.isTerminal()) {
return currentBoard;
}
}
return null;
}
static BoardState makeRandomMove(BoardState boardState, int randomMoveCount) throws CloneNotSupportedException
{
BoardState shuffledBoard = boardState.clone();
for (int i=0; i<randomMoveCount; i++) {
for (Player player : Player.values()) {
List<BoardState> successors = shuffledBoard.getSuccessors(player);
if (successors.isEmpty()) {
return null;
}
else {
int moveIndex = rand.nextInt(successors.size());
shuffledBoard = successors.get(moveIndex);
}
}
}
return shuffledBoard;
}
}