Commits

Alex Hixon committed c4364ae

Refactor to use Moves API, cleaned up some internal Game functions.

  • Participants
  • Parent commits 60ce8a4

Comments (0)

Files changed (5)

src/model/Game.java

         this.currentPlayer = players.peek ();
     }
 
-    public boolean placeWall (BoardCoordinate coordinate, WallDirection direction) {
-        if (this.currentPlayer.getWallCount() >= MAX_PLAYER_WALL_COUNT) {
-            return false;
+    public boolean moveIsValid (Move move) {
+        boolean valid = false;
+
+        if (move.getType() == MoveType.PLACE_WALL) {
+            // haven't exceeded wall limit
+            if (this.currentPlayer.getWallCount() < MAX_PLAYER_WALL_COUNT) {
+                Board testBoard = new Board (this.board);
+
+                if (testBoard.placeWall (move.getCoordinate(), move.getWallDirection())) {
+                    boolean hadOpenGoal = true;
+                    // make sure that a path is still left open to goal line for ALL players
+                    for (Player p : players) {
+                        if (!this.hasPathToBaseline (testBoard, p.getCurrentCoordinate (), p.getFinishBaseline ())) {
+                            hadOpenGoal = false;
+                            break;
+                        }
+                    }
+
+                    valid = hadOpenGoal;
+                }
+            }
+        } else {
+            LinkedList<BoardCoordinate> possible = this.getPossibleCoordinates();
+            valid = possible.contains (move.getCoordinate());
         }
 
-        Board modified = new Board (this.board);
-        if (modified.placeWall (coordinate, direction)) {
-            // make sure that a path is still left open to goal line for ALL players
-            for (Player p : players) {
-                if (!this.hasPathToBaseline (modified, p.getCurrentCoordinate (), p.getFinishBaseline ())) {
-                    return false;
-                }
-            }
+        return valid;
+    }
 
-            this.board = modified;
+    public void makeMove (Move move) {
+        if (move.getType() == MoveType.PLACE_WALL) {
+            this.board.placeWall(move.getCoordinate(), move.getWallDirection());
             this.currentPlayer.incrementWallCount();
         } else {
-            return false;
+            Tile destinationTile = this.board.getTile (move.getCoordinate());
+            Tile currentTile = this.getPlayerTile();
+
+            currentTile.setPlayer(null);
+            destinationTile.setPlayer(this.currentPlayer);
+            this.currentPlayer.setCurrentCoordinate (destinationTile.getCoordinate());
         }
 
-        return true;
     }
 
     private boolean hasPathToBaseline (Board board, BoardCoordinate currentCoordinate, Baseline finishBaseline) {
-        ArrayList<Tile> seen = new ArrayList<Tile>();    // FIXME: should probably use TreeSet instead to be faster
-        Queue<Tile> queue = new LinkedList<Tile>();
+        ArrayList<Tile> seen = new ArrayList<Tile>(); // using hashset makes no difference?
+        Deque<Tile> queue = new LinkedList<Tile>();
 
         queue.add(board.getTile(currentCoordinate));
 
         while (!queue.isEmpty()) {
-            Tile current = queue.remove();
+            Tile current = queue.removeLast();
             seen.add(current);
+
             if (finishBaseline.hasCoordinateOnBaseline(current.getCoordinate())) {
-                System.out.println("Found way to get from " + currentCoordinate + " to " + finishBaseline + " at " + current.getCoordinate());
                 return true;
             }
 
                 Tile other = current.getAdjacentLink(d);
 
                 if (other != null && !seen.contains(other)) {
-                    queue.add(other);
+                    queue.addLast(other);
                 }
             }
         }
 
-        System.out.println("WELP, no way to the end!");
         return false;
     }
 
         return this.board.getTile (this.currentPlayer.getCurrentCoordinate());
     }
 
-    public boolean move (BoardCoordinate coordinate) {
-        LinkedList<BoardCoordinate> possible = this.getPossibleMoves();
-        if (possible.contains(coordinate)) {
-            Tile destinationTile = this.board.getTile(coordinate);
-            Tile currentTile = this.getPlayerTile();
-
-            currentTile.setPlayer(null);
-            destinationTile.setPlayer(this.currentPlayer);
-            this.currentPlayer.setCurrentCoordinate (destinationTile.getCoordinate());
-            return true;
-        } else {
-            return false;
-        }
-    }
-
-    public LinkedList<BoardCoordinate> getPossibleMoves () {
+    public LinkedList<BoardCoordinate> getPossibleCoordinates () {
         LinkedList<BoardCoordinate> possible = new LinkedList<BoardCoordinate>();
         Tile currentTile = this.getPlayerTile();
         for (Direction d : Direction.values()) {

src/model/Move.java

 package model;
 
 public class Move {
+    private final MoveType type;
+    private final BoardCoordinate coordinate;
+    private WallDirection wallDirection = null;
 
+    public Move (MoveType type, BoardCoordinate coordinate) {
+        this.type = type;
+        this.coordinate = coordinate;
+    }
+
+    public Move (MoveType type, BoardCoordinate coordinate, WallDirection wallDirection) {
+        this (type, coordinate);
+        this.wallDirection = wallDirection;
+    }
+
+    public BoardCoordinate getCoordinate() {
+        return coordinate;
+    }
+
+    public MoveType getType() {
+        return type;
+    }
+
+    public WallDirection getWallDirection() {
+        return wallDirection;
+    }
+
+    public static Move translate (String move) {
+        if (move.length() == 3) {
+            // want to place a fence
+            WallDirection placement;
+            if (move.substring(2).equals("h")) {
+                placement = WallDirection.HORIZONTAL;
+            } else {
+                placement = WallDirection.VERTICAL;
+            }
+
+            BoardCoordinate location = new BoardCoordinate(move.substring(0, 2));
+            return new Move (MoveType.PLACE_WALL, location, placement);
+        } else {
+            return new Move (MoveType.MOVE_PLAYER, new BoardCoordinate(move));
+        }
+    }
 }

src/model/MoveType.java

+package model;
+
+public enum MoveType {
+    PLACE_WALL,
+    MOVE_PLAYER
+}

src/tests/GameTest.java

         this.game = new Game(p);
     }
 
-    @Test
+    /*@Test
     public void testDuplicateWall () {
         BoardCoordinate pos = new BoardCoordinate(3, 4);
         assertTrue (game.placeWall (pos, WallDirection.HORIZONTAL));
         BoardCoordinate pos = new BoardCoordinate(3, 4);
         assertTrue (game.placeWall (pos, WallDirection.HORIZONTAL));
         assertFalse (game.placeWall(pos, WallDirection.VERTICAL));
-    }
+    }*/
 
     /*@Test
     public void placeInvalidWall () {

src/tests/Validator.java

 	 * @return validity of the list of moves
 	 */
 	public boolean check(List<String> moves) {
-		for (String move: moves) {
+		for (String moveString: moves) {
             boolean success;
-            if (move.length() == 3) {
-                // want to place a fence
-                WallDirection placement;
-                if (move.substring(2).equals("h")) {
-                    placement = WallDirection.HORIZONTAL;
-                } else {
-                    placement = WallDirection.VERTICAL;
-                }
-
-                BoardCoordinate location = new BoardCoordinate(move.substring(0, 2));
-                success = game.placeWall(location, placement);
-            } else {
-                success = this.game.move(new BoardCoordinate(move));
-            }
-
-            // break out immediately if something went wrong (eg TEST FAILED)
-            if (!success) {
-                System.out.println("========== MOVE " + move + " INVALID, RETURNING ==========");
+            Move move = Move.translate(moveString);
+            if (!this.game.moveIsValid(move)) {
                 return false;
             }
 
-            // next player's go
+            this.game.makeMove(move);
             this.game.endTurn();
-            ConsoleUI.printBoard(this.game.getBoard());
         }
 
-        // if we got here, all moves were sweet - VERY NAICE
-        System.out.println("========== GAME VALID, RETURNING ==========");
 		return true;
 	}