Skip to content

Commit b9bffb9

Browse files
committed
Big Changes: Esc to Start Screen + added BoardController + Server-Client Changes + more. Still need testing
1 parent af098bd commit b9bffb9

File tree

20 files changed

+395
-201
lines changed

20 files changed

+395
-201
lines changed

swing/src/spaghetti/BotProgram.java renamed to game/src/spaghetti/BotProgram.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package spaghetti;
22

3-
import spaghetti.game.Board;
4-
import spaghetti.game.BoardListener;
5-
import spaghetti.game.Move;
3+
import spaghetti.game.*;
64

75
import java.io.*;
86

9-
public class BotProgram implements BoardListener {
7+
public class BotProgram extends BoardController {
8+
109
protected BufferedReader stdout;
1110
protected BufferedWriter stdin;
1211
protected Process process;
12+
protected boolean side;
1313
public final String cmd;
14-
public final Board board;
1514

16-
public BotProgram(String command, Board board) {
15+
public BotProgram(String name, String command, Board board) {
16+
super(name, board);
1717
System.err.println(command);
18-
this.board = board;
1918
board.addBoardListener(this);
2019

2120
cmd = command;
@@ -57,7 +56,7 @@ public void step() {
5756
}
5857
else throw new NullPointerException();
5958
} catch (NullPointerException e) {
60-
if (board.isRunning()) {
59+
if (board.getCurrentState() == BoardState.RUNNING) {
6160
System.err.println("Bot Program sends wrong move.");
6261
}
6362
process.destroy();
@@ -77,7 +76,12 @@ public void registerMove(Move m, BoardListener l) {
7776
}
7877

7978
@Override
80-
public void start() {
79+
public void announceControllers(BoardController c1, BoardController c2) {
80+
}
81+
82+
@Override
83+
public void onGameStart() {
84+
if (side) return;
8185
try {
8286
stdin.write("Start\n");
8387
stdin.flush();
@@ -104,7 +108,7 @@ public boolean isStartHandler() {
104108
}
105109

106110
@Override
107-
public String getControllerName() {
108-
return "Bot";
111+
public void setSide(boolean side) {
112+
this.side = side;
109113
}
110114
}

game/src/spaghetti/game/Board.java

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import spaghetti.utils.Triplet;
55

66
import java.util.*;
7-
import java.util.List;
87

98
/************************************************************
109
* The play function is written by Ludo Pulles in javascript
@@ -36,11 +35,11 @@ public Point copy() {
3635
public final int width, height;
3736
public final Tile[][] tiles;
3837
public final int[] scores = {50, 50};
39-
protected final List<BoardListener> listeners = new ArrayList<>();
40-
protected final BoardListener[] controllers = new BoardListener[2];
38+
protected final Set<BoardListener> listeners = new HashSet<>();
39+
protected final BoardController[] controllers = new BoardController[2];
4140
protected boolean turn = false;
42-
public boolean gameStarted = false;
4341
protected int moveCount = 0;
42+
protected BoardState currentState = BoardState.PRE_START;
4443

4544
public Board(int width, int height) {
4645
this.width = width;
@@ -51,6 +50,11 @@ public Board(int width, int height) {
5150
}
5251
}
5352

53+
public Board(int width, int height, Move[] moves) {
54+
this(width, height);
55+
for (Move m : moves) play(m, null);
56+
}
57+
5458
public void addBoardListener(BoardListener l) {
5559
listeners.add(l);
5660
}
@@ -60,32 +64,13 @@ public void removeBoardListener(BoardListener l) {
6064
if (moveCount != 0 && (controllers[0] == l || controllers[1] == l)) close();
6165
}
6266

63-
public void setControllers(BoardListener blue, BoardListener red) {
64-
controllers[0] = blue;
65-
controllers[1] = red;
66-
}
67-
68-
public void swapControllers() {
69-
BoardListener l = controllers[1];
70-
controllers[1] = controllers[0];
71-
controllers[0] = l;
72-
}
73-
74-
public Board(int width, int height, Move[] moves) {
75-
this(width, height);
76-
for (Move m : moves) play(m, null);
77-
}
78-
7967
public void close() {
68+
currentState = BoardState.OVER;
8069
for (BoardListener l : listeners) l.close();
8170
}
8271

83-
public boolean isRunning() {
84-
return moveCount != width * height;
85-
}
86-
87-
public boolean isGameStarted() {
88-
return gameStarted;
72+
public BoardState getCurrentState() {
73+
return currentState;
8974
}
9075

9176
public int getMoveCount() {
@@ -96,10 +81,18 @@ public boolean getTurn() {
9681
return turn;
9782
}
9883

99-
public BoardListener getControllerTurn() {
84+
public BoardController getControllerTurn() {
10085
return controllers[turn? 1: 0];
10186
}
10287

88+
public BoardController getController(boolean c) {
89+
return controllers[c? 1: 0];
90+
}
91+
92+
public BoardListener[] getBoardListeners() {
93+
return listeners.toArray(new BoardListener[0]);
94+
}
95+
10396
public boolean isOccupied(int row, int col) {
10497
return tiles[row][col].type != '\0';
10598
}
@@ -180,16 +173,33 @@ private void colorBoth(Position pos0, Position pos1, int color) {
180173
}
181174
}
182175

183-
public void start(boolean prePlayedMoves) {
176+
public void announceControllers(BoardController c1, BoardController c2) {
177+
for (BoardListener l : listeners) l.announceControllers(c1, c2);
178+
}
179+
180+
public void start(BoardListener startHandler, BoardController blue, BoardController red) {
181+
assert startHandler.isStartHandler();
182+
privateStart(blue, red);
183+
}
184+
185+
private void privateStart(BoardController blue, BoardController red) {
186+
controllers[0] = blue;
187+
controllers[1] = red;
188+
if (!blue.isStartHandler()) blue.setSide(false);
189+
if (!red.isStartHandler()) red.setSide(true);
190+
currentState = BoardState.RUNNING;
191+
for (BoardListener l : listeners) l.onGameStart();
192+
}
193+
194+
public void start(boolean prePlayedMoves, BoardController blue, BoardController red) {
184195
if (prePlayedMoves && width >= 4 && height >= 4) {
185196
Random rand = new Random();
186197
Move m1 = generatePreMove(rand), m2 = generatePreMove(rand);
187198
System.err.println("Pre Played Moves: " + m1 + ", " + m2);
188199
play(m1, null);
189200
play(m2, null);
190201
}
191-
gameStarted = true;
192-
controllers[0].start();
202+
privateStart(blue, red);
193203
}
194204

195205
public Move generatePreMove(Random rand) {
@@ -270,6 +280,7 @@ public void play(Move move, BoardListener player) {
270280
if (choose_best) break;
271281
}
272282

283+
if (moveCount == width * height) currentState = BoardState.OVER;
273284
turn = !turn;
274285
for (BoardListener listener : listeners) listener.registerMove(move, player);
275286
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package spaghetti.game;
2+
3+
public abstract class BoardController implements BoardListener {
4+
5+
protected final Board board;
6+
protected String name;
7+
private boolean side;
8+
9+
protected BoardController(String name, Board board) {
10+
this.name = name;
11+
this.board = board;
12+
board.addBoardListener(this);
13+
}
14+
15+
protected BoardController(Board board) {
16+
this("-", board);
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public boolean getSide() {
24+
return side;
25+
}
26+
27+
public void setSide(boolean side) { // occurs after pre played moves
28+
this.side = side;
29+
}
30+
}

game/src/spaghetti/game/BoardListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
public interface BoardListener {
44
void registerMove(Move m, BoardListener l);
5-
void start();
5+
void announceControllers(BoardController c1, BoardController c2);
6+
void onGameStart(); // occurs after setSide(side) of BoardController
67
void close();
78
boolean isStartHandler();
8-
String getControllerName();
99
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package spaghetti.game;
2+
3+
public enum BoardState {
4+
PRE_START,
5+
RUNNING,
6+
OVER
7+
}

networking/client/client.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<orderEntry type="sourceFolder" forTests="false" />
1010
<orderEntry type="module" module-name="networking" />
1111
<orderEntry type="module" module-name="game" />
12-
<orderEntry type="module" module-name="utils" />
12+
<orderEntry type="module" module-name="utils" scope="PROVIDED" />
1313
</component>
1414
</module>

networking/client/src/spaghetti/networking/client/ServerConnection.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
package spaghetti.networking.client;
22

33
import spaghetti.game.Board;
4+
import spaghetti.game.BoardController;
45
import spaghetti.game.BoardListener;
56
import spaghetti.game.Move;
6-
import spaghetti.networking.ServerCommand;
7+
import spaghetti.networking.ServerPacketType;
78

89
import java.io.*;
910
import java.net.Socket;
1011

11-
public class ServerConnection implements BoardListener, Runnable {
12+
public class ServerConnection extends BoardController implements Runnable {
1213

1314
protected boolean connected = false;
14-
public final Board board;
1515
public Socket socket;
1616
public ObjectInputStream in;
1717
public ObjectOutputStream out;
1818

1919
public ServerConnection(String address, int port, Board b) {
20+
super(b);
2021
try {
2122
socket = new Socket(address, port);
2223

2324
out = new ObjectOutputStream(socket.getOutputStream());
2425
in = new ObjectInputStream(socket.getInputStream());
2526
} catch (IOException e) {
26-
board = null;
2727
e.printStackTrace();
2828
return;
2929
}
3030

31-
board = b;
32-
b.addBoardListener(this);
31+
board.addBoardListener(this);
3332

3433
connected = true;
3534
System.out.printf("Connected to %s:%s%n", address, port);
@@ -64,8 +63,14 @@ public void registerMove(Move m, BoardListener l) {
6463
}
6564

6665
@Override
67-
public void start() {
66+
public void announceControllers(BoardController c1, BoardController c2) {
67+
if (c1 != this && c2 != this) return;
68+
send(c1 == this? c2.getName(): c1.getName());
69+
}
6870

71+
@Override
72+
public void onGameStart() {
73+
assert false;
6974
}
7075

7176
@Override
@@ -79,8 +84,8 @@ public boolean isStartHandler() {
7984
}
8085

8186
@Override
82-
public String getControllerName() {
83-
return "Server";
87+
public void setSide(boolean side) {
88+
super.setSide(side);
8489
}
8590

8691
@Override
@@ -89,20 +94,34 @@ public void run() {
8994
try {
9095
Object data = in.readObject();
9196
System.out.println("received: " + data);
92-
if (data instanceof ServerCommand) {
93-
switch ((ServerCommand) data) {
94-
case START:
95-
if (board.getControllerTurn() == this) board.swapControllers();
96-
board.getControllerTurn().start();
97+
if (data instanceof ServerPacketType) {
98+
switch ((ServerPacketType) data) {
99+
case SIDE:
100+
boolean otherSide = (boolean)in.readObject(); // false=blue true=red
101+
setSide(!otherSide);
102+
boolean started = false;
103+
for (BoardListener l : board.getBoardListeners()) {
104+
if (l instanceof BoardController && l != this) {
105+
BoardController c = (BoardController) l;
106+
board.start(this, otherSide? this: c, otherSide? c: this);
107+
started = true;
108+
break;
109+
}
110+
}
111+
if (!started) quit();
112+
break;
113+
case NAMES:
114+
String[] names = (String[])in.readObject();
115+
name = names[getSide()? 1: 0];
97116
break;
98117
case QUIT:
99118
quit();
100119
break;
101120
}
102121
} else if (data instanceof Move) {
103-
board.gameStarted = true;
104-
if (board.getMoveCount() == 2 && board.getControllerTurn() != this) board.swapControllers();
105-
board.play((Move) data, this);
122+
Move m = (Move) data;
123+
if (board.isOccupied(m.row, m.col)) quit();
124+
else board.play((Move) data, this);
106125
}
107126
} catch(IOException | ClassNotFoundException i) {
108127
i.printStackTrace();

networking/server/server.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<orderEntry type="sourceFolder" forTests="false" />
1010
<orderEntry type="module" module-name="networking" />
1111
<orderEntry type="module" module-name="game" />
12-
<orderEntry type="module" module-name="utils" />
12+
<orderEntry type="module" module-name="utils" scope="PROVIDED" />
1313
</component>
1414
</module>

0 commit comments

Comments
 (0)