hanze/game-client

auto play games (b1ab911bf44138182274c52e33aa46cfedad6bd8)
Repositories

commit b1ab911bf44138182274c52e33aa46cfedad6bd8
parent 5f90da3a5288bb0717fc5181e1ae6d306ab79265
Author: Merlijn <[email protected]>
Date:   Sat, 21 Jan 2023 23:30:21 +0100

auto play games

Diffstat:
Asrc/main/java/nl/isygameclient/DataSaver.java92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main/java/nl/isygameclient/Headless.java89++++++++++++++++++++++++++-----------------------------------------------------
Msrc/main/java/nl/isygameclient/Multithreading.java9++++++---
3 files changed, 127 insertions(+), 63 deletions(-)

diff --git a/src/main/java/nl/isygameclient/DataSaver.java b/src/main/java/nl/isygameclient/DataSaver.java @@ -0,0 +1,92 @@ +package nl.isygameclient; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Random; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class DataSaver { + private String path; + private File file; + + public DataSaver(String filename) { + this.createFile(filename); + + } + + public DataSaver() { + this.createFile(this.randomString()); + } + + public void createFile(String filename) { + this.path = "./data/temp/" + filename + ".csv"; + this.file = new File(this.path); + try { + this.file.createNewFile(); + } catch (IOException ex) { + System.out.print("Could not create file."); + } + + this.saveData("Is game draw? | Game winners? | Moves made?"); + } + + public String randomString() { + String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + int length = 7; + + for (int i = 0; i < length; i++) { + int index = random.nextInt(alphabet.length()); + char randomChar = alphabet.charAt(index); + sb.append(randomChar); + } + return sb.toString(); + } + + public void saveData(String data) { + try { + BufferedWriter output = new BufferedWriter(new FileWriter(this.path, true)); + output.write(data); + output.newLine(); + output.close(); + } catch (IOException ex) { + System.out.print("Invalid Path"); + } + } + + public void mergeFiles() { + Set<String> files = this.getTempFiles(this.path); + + assert files != null; + for (String file : files) { + try { + BufferedReader bf = new BufferedReader(new FileReader(file)); + String line = bf.readLine(); + + while (line != null) { + this.saveData(line); + line = bf.readLine(); + } + bf.close(); + } catch (IOException ignore) { + } + } + } + + private Set<String> getTempFiles(String dir) { + try (Stream<Path> stream = Files.list(Paths.get(dir))) { + return stream + .filter(file -> !Files.isDirectory(file)) + .map(Path::getFileName) + .map(Path::toString) + .collect(Collectors.toSet()); + } catch (IOException ignored) { + return null; + } + } +} diff --git a/src/main/java/nl/isygameclient/Headless.java b/src/main/java/nl/isygameclient/Headless.java @@ -9,32 +9,39 @@ import nl.isygameclient.models.PlayerManager; import nl.isygameclient.models.games.othello.Othello; import nl.isygameclient.util.Vector2D; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.NoSuchFileException; -import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; public class Headless implements Runnable { private Thread t; + public final int numOfGames; private static final String JSON_FILENAME = "heuristics.json"; + private DataSaver dataSaver; public static void main(String[] args) { new Headless().run(); } - private static void simulate(String name, Game game) { + public Headless() { + this(1); + } + + public Headless(int numOfGames) { + this.numOfGames = numOfGames; + } + + private void simulate(String name, Game game) { var playerManager = game.getPlayerManager(); Map<Player, ArrayList<Long>> playersTimePerMoves = new HashMap<>(); Map<Player, LinkedList<Vector2D<Integer, Integer>>> movesMade = new HashMap<>(); - DataSaver dataSaver = new DataSaver(); System.out.println("Starting game: " + name); while (!game.isGameOver()) { @@ -57,7 +64,7 @@ public class Headless implements Runnable { System.out.println("Is game draw: " + game.isDraw()); System.out.println("Game winners: " + game.getWinners().toString()); System.out.println("Moves made: " + movesMade); - dataSaver.saveData(game.isDraw() + " | " + game.getWinners().toString() + " | " + movesMade); + this.dataSaver.saveData(game.isDraw() + " | " + game.getWinners().toString() + " | " + movesMade); long totalGameTime = 0; for (Map.Entry<Player, ArrayList<Long>> playerTimePerMoves : playersTimePerMoves.entrySet()) { @@ -91,6 +98,8 @@ public class Headless implements Runnable { @Override public void run() { var heuristics = loadHeuristics(); + this.dataSaver = new DataSaver(); + if (heuristics == null) { System.err.println("No heuristics in file."); return; @@ -98,17 +107,19 @@ public class Headless implements Runnable { Map<String, Game> games = new HashMap<>(); - // Create Matches - for (Map.Entry<String, int[][]> heuristic1 : heuristics.entrySet()) { - for (Map.Entry<String, int[][]> heuristic2 : heuristics.entrySet()) { + for (int i = 0; i < this.numOfGames; i++) { + // Create Matches + for (Map.Entry<String, int[][]> heuristic1 : heuristics.entrySet()) { + for (Map.Entry<String, int[][]> heuristic2 : heuristics.entrySet()) { - ArrayList<Player> players = new ArrayList<>(); - players.add(new Ai("ai1{ " + heuristic1.getKey() + "}", "black", heuristic1.getValue())); - players.add(new Ai("ai2{ " + heuristic2.getKey() + "}", "white", heuristic2.getValue())); + ArrayList<Player> players = new ArrayList<>(); + players.add(new Ai("game: " + i + "ai1{ " + heuristic1.getKey() + "}", "black", heuristic1.getValue())); + players.add(new Ai("game: " + i + "ai2{ " + heuristic2.getKey() + "}", "white", heuristic2.getValue())); - var playerManager = new PlayerManager(0, players); - Othello othello = new Othello(playerManager); - games.put("ai1: " + heuristic1.getKey() + "; against " + "ai2: " + heuristic2.getKey(), othello); + var playerManager = new PlayerManager(0, players); + Othello othello = new Othello(playerManager); + games.put("game: " + i + " ai1: " + heuristic1.getKey() + "; against " + "ai2: " + heuristic2.getKey(), othello); + } } } @@ -125,45 +136,3 @@ public class Headless implements Runnable { } } } - -class DataSaver { - private final String path; - private final File file; - - public DataSaver() { - this.path = "./data/temp/" + this.randomString() + ".csv"; - this.file = new File(this.path); - try { - this.file.createNewFile(); - } catch (IOException ex) { - System.out.print("Could not create file."); - } - - this.saveData("Is game draw? | Game winners? | Moves made?"); - } - - public String randomString() { - String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - StringBuilder sb = new StringBuilder(); - Random random = new Random(); - int length = 7; - - for (int i = 0; i < length; i++) { - int index = random.nextInt(alphabet.length()); - char randomChar = alphabet.charAt(index); - sb.append(randomChar); - } - return sb.toString(); - } - - public void saveData(String data) { - try { - BufferedWriter output = new BufferedWriter(new FileWriter(this.path, true)); - output.write(data); - output.newLine(); - output.close(); - } catch (IOException ex) { - System.out.print("Invalid Path"); - } - } -} diff --git a/src/main/java/nl/isygameclient/Multithreading.java b/src/main/java/nl/isygameclient/Multithreading.java @@ -7,12 +7,15 @@ public class Multithreading { ArrayList<Headless> threaths = new ArrayList<>(); int threads = 1; for (int i = 0; i < threads; i++) { - threaths.add(i, new Headless()); + threaths.add(i, new Headless(2)); threaths.get(i).start(); } } +} - private void mergeFiles(int threads) { - +class MergeFiles { + public static void main(String args[]) { + DataSaver dataSaver = new DataSaver("../Data"); + dataSaver.mergeFiles(); } } \ No newline at end of file