hanze/game-client

src/main/java/nl/isygameclient/Headless.java in modern-design
Repositories | Summary | Log | Files

Headless.java (3402B) download


 1package nl.isygameclient;
 2
 3import nl.isygameclient.models.Ai;
 4import nl.isygameclient.models.Game;
 5import nl.isygameclient.models.Player;
 6import nl.isygameclient.models.PlayerManager;
 7import nl.isygameclient.models.games.Othello;
 8import nl.isygameclient.util.Vector2D;
 9import java.util.*;
10
11public class Headless {
12
13    private static final String JSON_FILENAME = "heuristics.json";
14
15    public static void main(String[] args) {
16        var heuristics = Ai.loadHeuristics(JSON_FILENAME);
17        if (heuristics == null) {
18            System.err.println("No heuristics in file.");
19            return;
20        }
21
22        Map<String, Game> games = new HashMap<>();
23
24        // Create Matches
25        for (Map.Entry<String, int[][]> heuristic1 : heuristics.entrySet()) {
26            for (Map.Entry<String, int[][]> heuristic2 : heuristics.entrySet()) {
27
28                ArrayList<Player> players = new ArrayList<>();
29                players.add(new Ai("ai1{ " + heuristic1.getKey() + "}", "black", heuristic1.getValue()));
30                players.add(new Ai("ai2{ " + heuristic2.getKey() + "}", "white", heuristic2.getValue()));
31
32                var playerManager = new PlayerManager(0, players);
33                Othello othello = new Othello(playerManager);
34                games.put("ai1: " + heuristic1.getKey() + "; against " + "ai2: " + heuristic2.getKey(), othello);
35            }
36        }
37
38        for (Map.Entry<String, Game> game : games.entrySet()) {
39            // add threading.
40            simulate(game.getKey(), game.getValue());
41        }
42    }
43
44    private static void simulate(String name, Game game) {
45        var playerManager = game.getPlayerManager();
46        Map<Player, ArrayList<Long>> playersTimePerMoves = new HashMap<>();
47        Map<Player, LinkedList<Vector2D<Integer, Integer>>> movesMade = new HashMap<>();
48
49        System.out.println("Starting game: " + name);
50        while (!game.isGameOver()) {
51            var currentPlayer = playerManager.getCurrentPlayer();
52            if (game.getValidMoves(currentPlayer).isEmpty()) {
53                playerManager.nextPlayer();
54                continue;
55            }
56
57
58            final long startGameTime = System.currentTimeMillis();
59            var move = currentPlayer.onPlayerTurn();
60            game.move(currentPlayer, move);
61            final long endTime = System.currentTimeMillis();
62
63            movesMade.computeIfAbsent(currentPlayer, k -> new LinkedList<>()).add(move);
64            playersTimePerMoves.computeIfAbsent(currentPlayer, k -> new ArrayList<>()).add((endTime - startGameTime));
65        }
66
67        System.out.println("Is game draw: " + game.isDraw());
68        System.out.println("Game winners: " + game.getWinners().toString());
69        System.out.println("Moves made: " + movesMade);
70        long totalGameTime = 0;
71        for (Map.Entry<Player, ArrayList<Long>> playerTimePerMoves: playersTimePerMoves.entrySet()) {
72            var player = playerTimePerMoves.getKey();
73            var moveTimes = playerTimePerMoves.getValue();
74            System.out.println(player + "'s Time per moves: " + moveTimes);
75            var optional = moveTimes.stream().reduce(Long::sum);
76            if (optional.isPresent()) {
77                totalGameTime += optional.get();
78            }
79        }
80        System.out.println("Total game time in milliseconds: " + totalGameTime);
81        System.out.println();
82    }
83}