minor fixes and improvements

This commit is contained in:
Dorian Zedler 2020-03-04 14:19:37 +01:00
parent 0bc6d3e6f7
commit 8fb61a6173
3 changed files with 39 additions and 8 deletions

View file

@ -49,6 +49,8 @@ public class ConnectFour extends JFrame implements ActionListener {
private GameType gameType; private GameType gameType;
String serverIp;
enum GameType { enum GameType {
Local, RemoteServer, RemoteClient Local, RemoteServer, RemoteClient
} }
@ -133,13 +135,13 @@ public class ConnectFour extends JFrame implements ActionListener {
this.players[0] = new LocalPlayer(this.buttonRow, null); this.players[0] = new LocalPlayer(this.buttonRow, null);
try { try {
this.players[1] = new RemotePlayerClient(this.buttonRow, "localhost", this.players[0]); this.players[1] = new RemotePlayerClient(this.buttonRow, this.serverIp, this.players[0]);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// do not add the listener of the local player here, as the client player will // do not add the listener of the local player here, as the client player will
// handle its moves in order to make shure they are legal // handle its moves in order to make sure they are legal
this.players[1].addActionListener(this); this.players[1].addActionListener(this);
break; break;
@ -246,6 +248,7 @@ public class ConnectFour extends JFrame implements ActionListener {
if (!res) { if (!res) {
// beep in case of error // beep in case of error
Toolkit.getDefaultToolkit().beep(); Toolkit.getDefaultToolkit().beep();
return;
} }
if (this.gameType != GameType.RemoteClient && this.gameState.equals(GameState.Running)) if (this.gameType != GameType.RemoteClient && this.gameState.equals(GameState.Running))
@ -349,8 +352,10 @@ public class ConnectFour extends JFrame implements ActionListener {
game.startNewGame(GameType.Local); game.startNewGame(GameType.Local);
else if (args[0].equals("server")) else if (args[0].equals("server"))
game.startNewGame(GameType.RemoteServer); game.startNewGame(GameType.RemoteServer);
else if (args[0].equals("client")) else if (args[0].equals("client")) {
game.serverIp = args[1];
game.startNewGame(GameType.RemoteClient); game.startNewGame(GameType.RemoteClient);
}
else else
System.out.println("Usage: java ConnectFour.java [server|client]"); System.out.println("Usage: java ConnectFour.java [server|client]");
} }

View file

@ -70,6 +70,33 @@ public class Plate {
return Plate.getValue(this.type); return Plate.getValue(this.type);
} }
/**
* Function to get the type of the plate as string
*
* @return "yellow" or "red"
*/
public String getName() {
return Plate.getName(this.type);
}
/**
* Function to get the type of the plate as string
*
* @param type plate type
*
* @return "yellow" or "red"
*/
public static String getName(PlateType type) {
switch (type) {
case O:
return "yellow";
case X:
return "red";
default:
return "";
}
}
/** /**
* Function to get a PlateType as int * Function to get a PlateType as int
* *
@ -105,10 +132,10 @@ public class Plate {
float[] hsb = new float[3]; float[] hsb = new float[3];
switch (type) { switch (type) {
case O: case O:
Color.RGBtoHSB(249, 227, 6, hsb); Color.RGBtoHSB(249, 227, 6, hsb);
break; break;
case X: case X:
Color.RGBtoHSB(246, 33, 61, hsb); Color.RGBtoHSB(246, 33, 61, hsb);
break; break;
} }

View file

@ -108,8 +108,7 @@ public abstract class Player {
* @return player name * @return player name
*/ */
public String getName() { public String getName() {
Color thisColor = Plate.getColor(this.usingPlateType); return Plate.getName(this.usingPlateType);
return thisColor.equals(Color.BLACK) ? "Black":"Red";
} }
/** /**