44import spaghetti .utils .Triplet ;
55
66import 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 }
0 commit comments