commit 156bcb4f5481901b87b39fefccacbc5e1118f26f
parent d7ed6161c63eee163c607ab54cf0f7eff2341efc
Author: A Koens <[email protected]>
Date: Tue, 7 Feb 2023 20:40:37 +0100
Some code cleanup
Diffstat:
6 files changed, 119 insertions(+), 117 deletions(-)
diff --git a/src/main/java/nl/isygameclient/Headless.java b/src/main/java/nl/isygameclient/Headless.java
@@ -16,7 +16,7 @@ import nl.isygameclient.models.Ai;
import nl.isygameclient.models.Game;
import nl.isygameclient.models.Player;
import nl.isygameclient.models.PlayerManager;
-import nl.isygameclient.models.games.othello.Othello;
+import nl.isygameclient.models.games.Othello;
import nl.isygameclient.util.DataSaver;
import nl.isygameclient.util.Vector2D;
diff --git a/src/main/java/nl/isygameclient/Tournament.java b/src/main/java/nl/isygameclient/Tournament.java
@@ -1,117 +1,118 @@
package nl.isygameclient;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-import nl.isygameclient.models.Ai;
-import nl.isygameclient.models.Game;
import nl.isygameclient.models.Player;
import nl.isygameclient.models.PlayerManager;
-import nl.isygameclient.models.games.othello.Othello;
+import nl.isygameclient.models.games.Othello;
import nl.isygameclient.network.Event;
-import nl.isygameclient.network.EventType;
import nl.isygameclient.network.GameClient;
import nl.isygameclient.util.Vector2D;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
public class Tournament {
- public static final int[][] HEURISTIC = {
- { 10, 2, 5, 5, 5, 5, 2, 10 },
- { 2, 2, 1, 1, 1, 1, 2, 2 },
- { 5, 1, 1, 1, 1, 1, 1, 5 },
- { 5, 1, 1, 1, 1, 1, 1, 5 },
- { 5, 1, 1, 1, 1, 1, 1, 5 },
- { 5, 1, 1, 1, 1, 1, 1, 5 },
- { 2, 2, 1, 1, 1, 1, 2, 2 },
- { 10, 2, 5, 5, 5, 5, 2, 10 }
- };
-
- public static void main(String[] args) throws IOException {
- // GameClient client = new GameClient("145.33.225.170", 7789);
- GameClient client = new GameClient("localhost", 7789);
-
- client.login("ITV2D1" + new Random().nextInt(9));
-
- Player dummyPlayer = new Player("network", "black") {
- public Vector2D<Integer, Integer> onPlayerTurn() { return null; }
- };
-
- Player randomPlayer = new Player("network", "black") {
- public Vector2D<Integer, Integer> onPlayerTurn() { return null; }
- };
-
- PlayerManager playerManager = new PlayerManager(0, List.of(randomPlayer, dummyPlayer));
- Othello othello = new Othello(playerManager);
- Random random = new Random();
-
- String firstPlayer = null;
-
- while (true) {
- Event event = client.event(-1);
-
- switch (event.type) {
- case MATCH:
- @SuppressWarnings("unchecked")
- var data = (Map<String, String>) event.data;
-
- String opponent = data.get("opponent");
- System.out.println("playing against " + opponent);
-
- firstPlayer = null;
- playerManager.restart();
- othello.getBoard().clear();
-
- break;
- case YOURTURN:
- if (firstPlayer == null) {
- firstPlayer = "self";
- othello.initializeBoard();
- }
- var turns = othello.getValidMoves(randomPlayer);
- System.out.println(turns);
- var turn = turns.get(random.nextInt( turns.size()));
-
- System.out.println(turn);
- client.send("move " + (turn.getY() * 8 + turn.getX()));
- // othello.move(ai, turn);
-
- break;
- case MOVE:
- if (firstPlayer == null) {
- firstPlayer = "other";
- playerManager.nextPlayer();
- othello.initializeBoard();
- }
- @SuppressWarnings("unchecked")
- var moveMap = (Map<String, String>) event.data;
-
- int move = Integer.parseInt(moveMap.get("move"));
- System.out.printf("%s did move %dx%d\n", moveMap.get("player"), move % 8, move / 8);
- othello.move(moveMap.get("player").equals(client.getName()) ? randomPlayer : dummyPlayer, new Vector2D<>(move % 8, move / 8));
-
- break;
- case WIN:
- case LOSS:
- default:
- System.out.printf("%s -> %s\n", client.getName(), event.type);
- for (int y = 0; y < 8; y++) {
- System.out.print("|");
- for (int x = 0; x < 8; x++) {
- Player p = othello.getBoard().get(new Vector2D<Integer, Integer>(x, y));
- if (p == dummyPlayer) {
- System.out.print("N|");
- } else if (p == randomPlayer) {
- System.out.print("A|");
- } else {
- System.out.print(" |");
- }
- }
- System.out.println();
- } // outcome = event;
- // return false;
- }
- }
- // client.close();
- }
+ public static final int[][] HEURISTIC = {
+ {10, 2, 5, 5, 5, 5, 2, 10},
+ {2, 2, 1, 1, 1, 1, 2, 2},
+ {5, 1, 1, 1, 1, 1, 1, 5},
+ {5, 1, 1, 1, 1, 1, 1, 5},
+ {5, 1, 1, 1, 1, 1, 1, 5},
+ {5, 1, 1, 1, 1, 1, 1, 5},
+ {2, 2, 1, 1, 1, 1, 2, 2},
+ {10, 2, 5, 5, 5, 5, 2, 10}
+ };
+
+ public static void main(String[] args) throws IOException {
+ // GameClient client = new GameClient("145.33.225.170", 7789);
+ GameClient client = new GameClient("localhost", 7789);
+
+ client.login("ITV2D1" + new Random().nextInt(9));
+
+ Player dummyPlayer = new Player("network", "black") {
+ public Vector2D<Integer, Integer> onPlayerTurn() {
+ return null;
+ }
+ };
+
+ Player randomPlayer = new Player("network", "black") {
+ public Vector2D<Integer, Integer> onPlayerTurn() {
+ return null;
+ }
+ };
+
+ PlayerManager playerManager = new PlayerManager(0, List.of(randomPlayer, dummyPlayer));
+ Othello othello = new Othello(playerManager);
+ Random random = new Random();
+
+ String firstPlayer = null;
+
+ while (true) {
+ Event event = client.event(-1);
+
+ switch (event.type) {
+ case MATCH:
+ @SuppressWarnings("unchecked")
+ var data = (Map<String, String>) event.data;
+
+ String opponent = data.get("opponent");
+ System.out.println("playing against " + opponent);
+
+ firstPlayer = null;
+ playerManager.restart();
+ othello.getBoard().clear();
+
+ break;
+ case YOURTURN:
+ if (firstPlayer == null) {
+ firstPlayer = "self";
+ othello.initializeBoard();
+ }
+ var turns = othello.getValidMoves(randomPlayer);
+ System.out.println(turns);
+ var turn = turns.get(random.nextInt(turns.size()));
+
+ System.out.println(turn);
+ client.send("move " + (turn.getY() * 8 + turn.getX()));
+ // othello.move(ai, turn);
+
+ break;
+ case MOVE:
+ if (firstPlayer == null) {
+ firstPlayer = "other";
+ playerManager.nextPlayer();
+ othello.initializeBoard();
+ }
+ @SuppressWarnings("unchecked")
+ var moveMap = (Map<String, String>) event.data;
+
+ int move = Integer.parseInt(moveMap.get("move"));
+ System.out.printf("%s did move %dx%d\n", moveMap.get("player"), move % 8, move / 8);
+ othello.move(moveMap.get("player").equals(client.getName()) ? randomPlayer : dummyPlayer, new Vector2D<>(move % 8, move / 8));
+
+ break;
+ case WIN:
+ case LOSS:
+ default:
+ System.out.printf("%s -> %s\n", client.getName(), event.type);
+ for (int y = 0; y < 8; y++) {
+ System.out.print("|");
+ for (int x = 0; x < 8; x++) {
+ Player p = othello.getBoard().get(new Vector2D<Integer, Integer>(x, y));
+ if (p == dummyPlayer) {
+ System.out.print("N|");
+ } else if (p == randomPlayer) {
+ System.out.print("A|");
+ } else {
+ System.out.print(" |");
+ }
+ }
+ System.out.println();
+ } // outcome = event;
+ // return false;
+ }
+ }
+ // client.close();
+ }
}
diff --git a/src/main/java/nl/isygameclient/controllers/games/othello/OthelloMultiPlayerController.java b/src/main/java/nl/isygameclient/controllers/games/othello/OthelloMultiPlayerController.java
@@ -38,7 +38,8 @@ public class OthelloMultiPlayerController implements GameController, Initializab
private GameClient client;
private Match match;
- private String playerSelf = "X", playerOther = "O";
+ private final String playerSelf = "X";
+ private final String playerOther = "O";
private JFXButton[][] boardButtons;
@@ -54,7 +55,7 @@ public class OthelloMultiPlayerController implements GameController, Initializab
@FXML
private GridPane boardGrid;
- private int[][] heuristics = {
+ private final int[][] heuristics = {
{4, 3, 3, 3, 3, 3, 3, 4},
{3, 2, 2, 2, 2, 2, 2, 3},
{3, 2, 1, 1, 1, 1, 2, 3},
@@ -65,7 +66,7 @@ public class OthelloMultiPlayerController implements GameController, Initializab
{4, 3, 3, 3, 3, 3, 3, 4}
};
- private boolean hasClicked = false;
+ private final boolean hasClicked = false;
private Vector2D<Integer, Integer> clickedPos;
@Override
diff --git a/src/main/java/nl/isygameclient/controllers/games/othello/OthelloSinglePlayerController.java b/src/main/java/nl/isygameclient/controllers/games/othello/OthelloSinglePlayerController.java
@@ -29,7 +29,7 @@ public class OthelloSinglePlayerController implements GameController, Initializa
@FXML
private GridPane boardGrid;
- private int[][] heuristics = {
+ private final int[][] heuristics = {
{4, 3, 3, 3, 3, 3, 3, 4},
{3, 2, 2, 2, 2, 2, 2, 3},
{3, 2, 1, 1, 1, 1, 2, 3},
@@ -40,9 +40,9 @@ public class OthelloSinglePlayerController implements GameController, Initializa
{4, 3, 3, 3, 3, 3, 3, 4}
};
- private boolean hasClicked = false;
+ private final boolean hasClicked = false;
private Vector2D<Integer, Integer> clickedPos;
- private boolean againstAi = false;
+ private final boolean againstAi = false;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
diff --git a/src/main/java/nl/isygameclient/network/GameClientBase.java b/src/main/java/nl/isygameclient/network/GameClientBase.java
@@ -12,8 +12,8 @@ public abstract class GameClientBase extends Socket {
private final OutputStream outputStream;
private final BufferedReader inputBuffer;
- private LinkedList<Event> eventQueue = new LinkedList<>();
- private LinkedList<String> eventLineQueue = new LinkedList<>();
+ private final LinkedList<Event> eventQueue = new LinkedList<>();
+ private final LinkedList<String> eventLineQueue = new LinkedList<>();
public GameClientBase(String host, int port) throws IOException {
super(host, port);
diff --git a/src/main/java/nl/isygameclient/network/GameType.java b/src/main/java/nl/isygameclient/network/GameType.java
@@ -8,7 +8,7 @@ public enum GameType {
public final String name;
public final int maxMoves;
- private GameType(String name, int maxMoves) {
+ GameType(String name, int maxMoves) {
this.name = name;
this.maxMoves = maxMoves;
}