move board size out of player constructor

This commit is contained in:
Luca Conte 2024-11-30 18:15:07 +01:00
parent 3eeb4d25bd
commit 2c6f9adf54
3 changed files with 10 additions and 6 deletions

View File

@ -17,7 +17,7 @@ public class GameController {
"1.1.0", OnlinePlayer_1_1_0.class
));
public void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, InetSocketAddress address) throws IOException {
public void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, InetSocketAddress address, int size) throws IOException {
AsyncSocket clientSocket;
boolean localPlayerIsServer = address.getHostName() == null;
@ -71,7 +71,7 @@ public class GameController {
OnlinePlayer onlinePlayer;
try {
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
onlinePlayer = onlinePlayerClass.getDeclaredConstructor().newInstance();
onlinePlayer = onlinePlayerClass.getDeclaredConstructor().newInstance((Integer)size, clientSocket);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to instantiate players");
@ -161,6 +161,10 @@ public class GameController {
e.printStackTrace();
throw new RuntimeException("Unable to instantiate players");
}
localPlayer.createBoard(size);
aiPlayer.createBoard(size);
startGameWithInstancedPlayers(localPlayer, aiPlayer);
}

View File

@ -2,7 +2,6 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
private AsyncSocket socket;
public OnlinePlayer(int size, AsyncSocket socket) {
super(size);
this.socket = socket;
//TODO Auto-generated constructor stub
}

View File

@ -1,5 +1,4 @@
import java.awt.*;
import java.util.List;
public abstract class Player {
protected boolean myTurn;
@ -9,7 +8,9 @@ public abstract class Player {
protected String name;
protected Board board;
public Player(int size) {
public Player() {}
public void createBoard(int size) {
this.board = new Board(size);
}