-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameSimulatorConsole.java
127 lines (120 loc) · 5.46 KB
/
GameSimulatorConsole.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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
class StatsContainer {
public final double whiteAverageNodes, whiteAverageTime;
public final double blackAverageNodes, blackAverageTime;
public final int whiteMaxDepth, blackMaxDepth;
public final CheckersGridAccessor whiteAccessor, blackAccessor;
public final PlayerColor winner;
StatsContainer(double whiteAverageNodes, double whiteAverageTime, int whiteMaxDepth, CheckersGridAccessor whiteAccessor,
double blackAverageNodes, double blackAverageTime, int blackMaxDepth, CheckersGridAccessor blackAccessor,
PlayerColor winner) {
this.whiteAverageNodes = whiteAverageNodes;
this.whiteAverageTime = whiteAverageTime;
this.blackAverageNodes = blackAverageNodes;
this.blackAverageTime = blackAverageTime;
this.whiteMaxDepth = whiteMaxDepth;
this.blackMaxDepth = blackMaxDepth;
this.whiteAccessor = whiteAccessor;
this.blackAccessor = blackAccessor;
this.winner = winner;
}
public static String columnsNames() {
return "whiteAverageNodes" +
"\t" + "blackAverageNodes" +
"\t" + "whiteAverageTime" +
"\t" + "blackAverageTime" +
"\t" + "whiteMaxDepth" +
"\t" + "blackMaxDepth" +
"\t" + "whiteAccessor" +
"\t" + "blackAccessor" +
"\t" + "winner";
}
@Override
public String toString() {
return whiteAverageNodes +
"\t" + blackAverageNodes +
"\t" + whiteAverageTime +
"\t" + blackAverageTime +
"\t" + whiteMaxDepth +
"\t" + blackMaxDepth +
"\t" + whiteAccessor.getClass().getName() +
"\t" + blackAccessor.getClass().getName() +
"\t" + winner;
}
}
public class GameSimulatorConsole {
public static StatsContainer runGame(Player white, Player black) {
if (white.getPlayerColor() != PlayerColor.WHITE || black.getPlayerColor() != PlayerColor.BLACK) {
throw new IllegalStateException("Wrong player colors!");
}
var grid = new CheckersGridHandler();
grid.basicSetup();
ArrayList<Move> allMoves;
while (!grid.isGameFinished()) {
allMoves = grid.getAllCurrentPossibleMoves();
System.out.println(grid);
// System.out.print("Press ENTER to continue...");
// scanner.nextLine();
System.out.println("Counting... (" + allMoves.size() + ")");
Move selectedMove;
if (PlayerColor.WHITE == grid.getCurrentPlayer()) {
selectedMove = white.getChosenMove(grid);
}
else {
selectedMove = black.getChosenMove(grid);
}
System.out.println("\n----------------------------------------");
System.out.println(grid.getCurrentPlayer() + ": " + selectedMove);
grid.executeMove(selectedMove);
}
// System.out.println("----------------------------------------\n");
System.out.println(grid);
System.out.println("WINNER: " + grid.getWinner());
System.out.println("----------------------------------------\n");
if (white instanceof Bot) {
System.out.println("Stats white bot:");
((Bot) white).printStats();
}
if (black instanceof Bot) {
System.out.println("Stats black bot:");
((Bot) black).printStats();
}
if (black instanceof Bot && white instanceof Bot) {
return new StatsContainer(
((Bot) white).getAverageNodesVisited(), ((Bot) white).getAverageTimeForMove(), ((Bot) white).maxDepth, ((Bot) white).accessor,
((Bot) black).getAverageNodesVisited(), ((Bot) black).getAverageTimeForMove(), ((Bot) black).maxDepth, ((Bot) black).accessor,
grid.getWinner());
}
if (white instanceof Bot) {
return new StatsContainer(
((Bot) white).getAverageNodesVisited(), ((Bot) white).getAverageTimeForMove(), ((Bot) white).maxDepth, ((Bot) white).accessor,
0, 0, 0, ((Bot) white).accessor,
grid.getWinner());
}
return null;
}
public static void main(String[] args) throws IOException {
// var human = new Human(PlayerColor.BLACK);
// var randomPlayer = new RandomPlayer(PlayerColor.BLACK);
// PrintWriter writer = new PrintWriter("F:\\sztuczna_inteligencja\\E3\\src\\stats\\stats_" + (new Timestamp(System.currentTimeMillis())).toString().replace(":", ".") + ".csv", "UTF-8");
// writer.println("sep=\t");
// writer.println(StatsContainer.columnsNames());
for (var depth : new int[]{7}) {
var botWhite = new BotAlphaBeta(new SimpleAccessor(), PlayerColor.WHITE, depth);
var botBlack = new BotAlphaBeta(new ComplexGridAccessor(), PlayerColor.BLACK, depth);
System.out.println("DEPTH: " + depth);
for (int i = 0; i < 30; i++) {
System.out.println("CURRENT: " + i);
var stats = runGame(botWhite, botBlack);
// writer.println(stats.toString().replace(".", ","));
}
// writer.println();
}
// writer.close();
}
}