forked from ssalabura/Rods-and-Harpoons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotArena.java
93 lines (76 loc) · 3.33 KB
/
BotArena.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
package arena;
import board.*;
import board.arranger.RatioTileChooser;
import board.arranger.RectangleTileArranger;
import board.arranger.TileArranger;
import board.arranger.TileScoreChooser;
import board.views.BoardView;
import board.views.NoneBoardView;
import game.GameManager;
import game.GameObserver;
import game.Player;
import game.controllers.BotControllerFactory;
import game.controllers.botcontrollerfactories.HardBotControllerFactory;
import game.controllers.botcontrollerfactories.MediumBotControllerFactory;
import game.threads.OnlyMainThreadRunner;
import util.sleeper.FakeSleeper;
import util.GameInfo;
import java.util.ArrayList;
/**
This class is for AI developers to test and compare their strategies, optimize hyperparameters etc.
It also demonstrates that game model is independent from view (JavaFX)
*/
public class BotArena {
final int boardSize = 8;
final int pawnsPerPlayer = 2;
public static void main(String[] args) {
final int totalRounds = 1000;
BotArena botArena = new BotArena();
//set strategies
botArena.addPlayer(new MediumBotControllerFactory(new FakeSleeper(), new OnlyMainThreadRunner()));
botArena.addPlayer(new HardBotControllerFactory(new FakeSleeper(), new OnlyMainThreadRunner()));
ArrayList<String> nicks = botArena.getNicknames();
//set statistics which will be collected after each game
StatisticsGroup statistics = new StatisticsGroup();
statistics.setPlayersNumber(nicks.size());
statistics.addStatistics(new WinStatistic(nicks)).addStatistics(new AveragePointsStatistic(nicks));
botArena.setStatistics(statistics);
for(int i=0;i<totalRounds;i++) {
botArena.newRound();
}
System.out.println(statistics.getStatsGroupedByPlayer());
}
private GameManager gameManager;
private ArrayList<BotControllerFactory> botFactories = new ArrayList<>();
private ArrayList<String> nicknames = new ArrayList<>();
private BoardView boardView = new NoneBoardView();
private Board board = new Board(boardView, boardSize);
private MoveChecker moveChecker = new StandardMoveChecker(board);
private Statistics statistics;
public ArrayList<String> getNicknames() {
return nicknames;
}
public void setStatistics(Statistics statistics) {
this.statistics = statistics;
}
public void resetBoard() {
TileScoreChooser tileScoreChooser = new RatioTileChooser(3,2,1);
TileArranger tileArranger = new RectangleTileArranger(boardSize,boardSize);
tileArranger.arrange(board,tileScoreChooser);
}
public void addPlayer(BotControllerFactory botControllerFactory) {
botFactories.add(botControllerFactory);
nicknames.add(botControllerFactory.toString());
}
public void newRound() {
resetBoard();
gameManager = new GameManager(moveChecker, board, nicknames, new ArrayList<>(botFactories), pawnsPerPlayer);
gameManager.setObserver(new GameObserver() {
@Override public void onGameOver(GameInfo gameInfo, boolean saveGame) {}
@Override public void onPlayerPointsUpdated(Player player) {}
@Override public void onGameInfoUpdated(GameInfo gameInfo) {}
});
gameManager.startGame();
statistics.collectGameResult(gameManager.getPlayers());
}
}