Commits

Alex Hixon  committed 18f8592

Fixed wall building logic. Passes all tests except for checking if there's a way to get to the base line.

  • Participants
  • Parent commits ecb34cd

Comments (0)

Files changed (6)

File src/model/Board.java

     }
 
     public boolean placeWall (BoardCoordinate coordinate, WallDirection wallDirection) {
-        Tile a = tiles[coordinate.getX()][coordinate.getY()];
-        Tile b = null;
-        Direction checkDirection = null;
-
-        switch (wallDirection) {
-            case HORIZONTAL:
-                b = a.getAdjacentLink(Direction.RIGHT);
-                checkDirection = Direction.UP;
-                break;
-            case VERTICAL:
-                b = a.getAdjacentLink(Direction.UP);
-                checkDirection = Direction.RIGHT;
-                break;
-        }
+        Tile start = tiles[coordinate.getX()][coordinate.getY()];
+        boolean canBuild = true;
 
         // can't start build on the outer top rows/columns of the board (requires 4 tiles)
         if (coordinate.getX() == (WIDTH - 1) || coordinate.getY() == (HEIGHT - 1)) {
             return false;
         }
 
-        // actually place the wall if we can
-        if (checkTilePairForWall (a, b, checkDirection)) {
-            a.breakAdjacent(checkDirection);
-            b.breakAdjacent(checkDirection);
-            return true;
+        Tile right, above;
+        Direction a, b;
+
+        if (wallDirection == WallDirection.HORIZONTAL) {
+            // make sure there's no wall already directly on top
+            canBuild &= (start.getAdjacentLink(Direction.UP) != null);
+            right = this.getTile(start.getCoordinate().add(Direction.RIGHT));
+            canBuild &= (right.getAdjacentLink(Direction.UP) != null);
+
+            above = this.getTile(start.getCoordinate().add(Direction.UP));
+            // check perpendicular to see if we can build on a T
+            // if BOTH are blocked then we get an X which we is invalid
+            canBuild &= (start.getAdjacentLink(Direction.RIGHT) != null || above.getAdjacentLink(Direction.RIGHT) != null);
+
+            if (canBuild) {
+                start.breakAdjacent(Direction.UP);
+                right.breakAdjacent(Direction.UP);
+            }
         } else {
-            return false;
+            // make sure there's no wall already directly on top
+            canBuild &= (start.getAdjacentLink(Direction.RIGHT) != null);
+            above = this.getTile(start.getCoordinate().add(Direction.UP));
+            canBuild &= (above.getAdjacentLink(Direction.RIGHT) != null);
+
+            right = this.getTile(start.getCoordinate().add(Direction.RIGHT));
+            // check perpendicular to see if we can build on a T
+            // if BOTH are blocked then we get an X which we is invalid
+            canBuild &= (start.getAdjacentLink(Direction.UP) != null || right.getAdjacentLink(Direction.UP) != null);
+
+            if (canBuild) {
+                start.breakAdjacent(Direction.RIGHT);
+                above.breakAdjacent(Direction.RIGHT);
+            }
         }
+
+        return canBuild;
     }
 
     private boolean checkTilePairForWall (Tile a, Tile b, Direction dir) {

File src/model/BoardCoordinate.java

     }
 
     public BoardCoordinate add (Direction d) {
-        return new BoardCoordinate(this.add(d));
+        return new BoardCoordinate (super.add(d));
     }
 
     private boolean isInBounds () {
         return (this.x >= 0 && this.y >= 0 && this.x < Board.WIDTH && this.y < Board.HEIGHT);
     }
+
+    public boolean isEdge() {
+        return (this.x == 0 || this.x == (Board.WIDTH - 1) || this.y == 0 || this.y == (Board.HEIGHT - 1));
+    }
 }

File src/model/Direction.java

         // FIXME: exception would be nice?
         return null;
     }
+
+    public static Direction[] getPerpendicular (Direction d) {
+        if (d == UP || d == DOWN) {
+            return new Direction[]{ LEFT, RIGHT };
+        } else {
+            return new Direction[]{ UP, DOWN };
+        }
+    }
 }

File src/model/Game.java

         this.currentPlayer = players.peek ();
     }
 
-    /**
-     * Places a wall on the board
-     * @param coordinate the bottom left of the four squares to build a wall
-     * @param direction
-     * @return
-     */
     public boolean placeWall (BoardCoordinate coordinate, WallDirection direction) {
         if (this.currentPlayer.getWallCount() >= MAX_PLAYER_WALL_COUNT) {
             return false;
         }
 
+        // TODO: need to make sure that a path is still left open to goal line
+
         boolean success = board.placeWall (coordinate, direction);
         if (success) this.currentPlayer.incrementWallCount();
         return success;
         return this.board.getTile (this.currentPlayer.getCurrentCoordinate());
     }
 
-    public boolean move (Direction dir) {
-        // check if there's a wall there first
-        Tile currentTile = this.getPlayerTile();
-        Tile destinationTile = currentTile.getAdjacentLink(dir);
+    public boolean move (BoardCoordinate coordinate) {
+        LinkedList<BoardCoordinate> possible = this.getPossibleMoves();
+        if (possible.contains(coordinate)) {
+            Tile destinationTile = this.board.getTile(coordinate);
+            Tile currentTile = this.getPlayerTile();
 
-        // if we couldn't find an unbroken link to the tile (ie no wall) then we obviously can't move there
-        if (destinationTile == null) return false;
-
-        // make sure nobody else is on there
-        if (destinationTile.getPlayer() != null) return false;
-
-        // cool, update where everyone is
-        currentTile.setPlayer(null);
-        destinationTile.setPlayer(this.currentPlayer);
-        this.currentPlayer.setCurrentCoordinate (destinationTile.getCoordinate());
-
-        return true;
-    }
-
-    public boolean move (BoardCoordinate coordinate) {
-        // work out where we are relative to where we want to go
-        Direction dir = getRelativeDirection (this.currentPlayer.getCurrentCoordinate(), coordinate);
-        if (dir != null) {
-            // go and move in that direction
-            return move (dir);
+            currentTile.setPlayer(null);
+            destinationTile.setPlayer(this.currentPlayer);
+            this.currentPlayer.setCurrentCoordinate (destinationTile.getCoordinate());
+            return true;
         } else {
             return false;
         }
     }
 
-    public static Direction getRelativeDirection (BoardCoordinate from, BoardCoordinate to){
-        Coordinate ans = from.subtract(to);
+    public LinkedList<BoardCoordinate> getPossibleMoves () {
+        LinkedList<BoardCoordinate> possible = new LinkedList<BoardCoordinate>();
+        Tile currentTile = this.getPlayerTile();
+        for (Direction d : Direction.values()) {
+            Tile destinationTile = currentTile.getAdjacentLink(d);
 
-        // TODO: don't test distance in direction method!
-        // FIXME; don't return null if it's more than 0, if we shouldn't worry about the actual distance involved
-        // THIS IS HERE FOR MOVE AND POTTER'S STUPID VALIDATOR
+            // if we couldn't find an unbroken link to the tile (ie no wall) then we obviously can't move there
+            if (destinationTile == null) {
+                continue;
+            }
 
-        if (ans.getY() == 1 && ans.getX() == 0) {
-            return Direction.DOWN;
-        } else if (ans.getY() == -1 && ans.getX() == 0) {
-            return Direction.UP;
-        } else if (ans.getX() == 1 && ans.getY() == 0) {
-            return Direction.LEFT;
-        } else if (ans.getX() == -1 && ans.getY() == 0) {
-            return Direction.RIGHT;
+            // check if there's another player where they want to move
+            if (destinationTile.getPlayer() != null) {
+                // see if there's a fence behind that player
+                Tile behind = destinationTile.getAdjacentLink(d);
+
+                // make sure we're not trying to move off the board
+                // TODO: write a test for this guy
+                if (destinationTile.getCoordinate().add(d).isEdge ()) {
+                    continue;
+                }
+
+                if (behind == null) {
+                    // can place pawn either to left or right of other player
+                    Direction[] perpendicular = Direction.getPerpendicular(d);
+                    for (Direction other : perpendicular) {
+                        // if there's a tile to the left/right of the one we're going for
+                        Tile otherTile = destinationTile.getAdjacentLink(other);
+                        if (otherTile != null) {
+                            possible.add (otherTile.getCoordinate());
+                        }
+                    }
+                } else {
+                    // no fence, can move behind
+                    possible.add(behind.getCoordinate());
+                }
+            } else {
+                // no player on this tile, go nuts!
+                possible.add (destinationTile.getCoordinate());
+            }
         }
 
-        return null;
+        return possible;
     }
 
+    public Board getBoard() {
+        return board;
+    }
 }

File src/model/GameTest.java

         assertTrue (game.placeWall (pos, WallDirection.VERTICAL));
         assertFalse (game.placeWall(pos, WallDirection.VERTICAL));
 
-        ConsoleUI.printBoard(game.board);
+        //ConsoleUI.printBoard(game.board);
     }
 
     @Test
         assertTrue(game.placeWall(pos, Direction.RIGHT)) ;
     }*/
 
-    @Test
+    /*@Test
     public void moveTest () {
         assertTrue (game.move(Direction.DOWN));
         System.out.println(game.currentPlayer.getCurrentCoordinate());
 
         BoardCoordinate invalid = new BoardCoordinate("f7");
         assertFalse (game.move(invalid));
-    }
+    }*/
 }

File src/model/PotterTests/Validator.java

 package model.PotterTests;
 
 import model.*;
+import view.ConsoleUI;
 
 import java.util.LinkedList;
 import java.util.List;
             if (move.length() == 3) {
                 // want to place a fence
                 WallDirection placement;
-                if (move.substring(2) == "h") {
+                if (move.substring(2).equals("h")) {
                     placement = WallDirection.HORIZONTAL;
                 } else {
                     placement = WallDirection.VERTICAL;
 
             // break out immediately if something went wrong (eg TEST FAILED)
             if (!success) {
+                System.out.println("========== MOVE " + move + " INVALID, RETURNING ==========");
                 return false;
             }
 
             // next player's go
             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;
 	}