Alex Hixon avatar Alex Hixon committed 3c9efe9

All tests passed. YOU ARE AWESOME!

Some refactoring needs to be done, and some debugging stuff taken out, but it's 2:17AM and I WANT TO GO TO BED! :(

Comments (0)

Files changed (7)

src/model/Baseline.java

+package model;
+
+public class Baseline {
+    private enum LineParameter {
+        X,
+        Y
+    }
+
+    private final int x;
+    private final int y;
+
+    private final LineParameter location;
+
+    public Baseline (Direction startDirection) {
+        if (startDirection == Direction.DOWN) {
+            x = (Board.WIDTH - 1) / 2;
+            y = Board.HEIGHT - 1;
+            location = LineParameter.Y;
+        } else if (startDirection == Direction.UP) {
+            // starting from bottom (so middle of bottom row)
+            x = (Board.WIDTH - 1) / 2;
+            y = 0;
+            location = LineParameter.Y;
+        } else if (startDirection == Direction.RIGHT) {
+            // starting from left (so middle of left row)
+            x = 0;
+            y = (Board.HEIGHT - 1) / 2;
+            location = LineParameter.X;
+        } else {
+            // starting from right (middle of right row)
+            x = Board.WIDTH - 1;
+            y = (Board.HEIGHT - 1) / 2;
+            location = LineParameter.X;
+        }
+    }
+
+    public BoardCoordinate getCenterCoordinate() {
+        return new BoardCoordinate(x, y);
+    }
+
+    public boolean hasCoordinateOnBaseline (BoardCoordinate coordinate) {
+        if (this.location == LineParameter.X) {
+            return coordinate.getX() == this.x;
+        } else {
+            return coordinate.getY() == this.y;
+        }
+    }
+
+    @Override
+    public String toString() {
+        if (this.location == LineParameter.X) {
+            return "x = " + this.x;
+        } else {
+            return "y = " + this.y;
+        }
+    }
+}

src/model/BaselineTest.java

+package model;
+
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+
+public class BaselineTest {
+
+    @Test
+    public void testOnBaselineHorizontal () {
+        Baseline baseline = new Baseline(Direction.UP);
+        BoardCoordinate b1 = new BoardCoordinate(3, 0);
+        BoardCoordinate b2 = new BoardCoordinate(0, 0);
+        BoardCoordinate b3 = new BoardCoordinate(8, 0);
+
+        assertTrue (baseline.hasCoordinateOnBaseline(b1));
+        assertTrue (baseline.hasCoordinateOnBaseline(b2));
+        assertTrue (baseline.hasCoordinateOnBaseline(b3));
+
+        BoardCoordinate not1 = new BoardCoordinate(3, 4);
+        BoardCoordinate not2 = new BoardCoordinate(0, 5);
+        assertFalse (baseline.hasCoordinateOnBaseline(not1));
+        assertFalse (baseline.hasCoordinateOnBaseline(not2));
+    }
+}

src/model/Board.java

         }
     }
 
+    /**
+     * Creates a deep copy of a Board.
+     * @param other The Board you want this to be a copy of.
+     */
+    public Board (Board other) {
+        // THIS ASSUMES THAT THE BOARD HAS NO BLACK HOLES????
+        this ();
+
+        // now copy the structure and the player locations
+        for (int x = 0; x < WIDTH; x++) {
+            for (int y = 0; y < HEIGHT; y++) {
+                // first copy where the broken links lie
+                for (Direction d : Direction.values()) {
+                    if (other.tiles[x][y].getAdjacentLink(d) == null) {
+                        this.tiles[x][y].breakAdjacent(d);
+                    }
+                }
+
+                // and copy the tile contents
+                this.tiles[x][y].setPlayer(other.tiles[x][y].getPlayer());
+            }
+        }
+    }
+
     public boolean placeWall (BoardCoordinate coordinate, WallDirection wallDirection) {
         Tile start = tiles[coordinate.getX()][coordinate.getY()];
         boolean canBuild = true;
         return (b != null && a.getAdjacentLink(dir) != null && b.getAdjacentLink(dir) != null);
     }
 
-    public static BoardCoordinate makeStartCoordinateFromDirection (Direction d) {
-        switch (d) {
-            case DOWN:
-                // starting from top (so middle of top row)
-                return new BoardCoordinate((WIDTH - 1) / 2, HEIGHT - 1);
-            case UP:
-                // starting from bottom (so middle of bottom row)
-                return new BoardCoordinate((WIDTH - 1) / 2, 0);
-            case RIGHT:
-                // starting from left (so middle of left row)
-                return new BoardCoordinate(0, (HEIGHT - 1) / 2);
-            case LEFT:
-                // starting from right (middle of right row)
-                return new BoardCoordinate(WIDTH - 1, (HEIGHT - 1) / 2);
-        }
-
-        return null;
-    }
-
     public Tile getTile (BoardCoordinate c) {
         return this.tiles[c.getX()][c.getY()];
     }

src/model/Coordinate.java

     public Coordinate subtract (Coordinate other) {
         return new Coordinate(this.x - other.x, this.y - other.y);
     }
+
+
 }
 

src/model/Game.java

 package model;
 
-import java.util.LinkedList;
+import view.ConsoleUI;
+
+import java.util.*;
 
 public class Game {
     public final static int MAX_PLAYER_WALL_COUNT = 10;
             return false;
         }
 
-        // TODO: need to make sure that a path is still left open to goal line
+        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;
+                }
+            }
 
-        boolean success = board.placeWall (coordinate, direction);
-        if (success) this.currentPlayer.incrementWallCount();
-        return success;
+            this.board = modified;
+            this.currentPlayer.incrementWallCount();
+        } else {
+            return false;
+        }
+
+        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>();
+
+        queue.add(board.getTile(currentCoordinate));
+
+        while (!queue.isEmpty()) {
+            Tile current = queue.remove();
+            seen.add(current);
+            if (finishBaseline.hasCoordinateOnBaseline(current.getCoordinate())) {
+                System.out.println("Found way to get from " + currentCoordinate + " to " + finishBaseline + " at " + current.getCoordinate());
+                return true;
+            }
+
+            for (Direction d : Direction.values()) {
+                Tile other = current.getAdjacentLink(d);
+
+                if (other != null && !seen.contains(other)) {
+                    queue.add(other);
+                }
+            }
+        }
+
+        System.out.println("WELP, no way to the end!");
+        return false;
     }
 
     private Tile getPlayerTile () {
 
                 // make sure we're not trying to move off the board
                 // TODO: write a test for this guy
-                if (destinationTile.getCoordinate().add(d).isEdge ()) {
+                if (destinationTile.getCoordinate().add(d).isEdge()) {
                     continue;
                 }
 
                         // 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());
+                            possible.add(otherTile.getCoordinate());
                         }
                     }
                 } else {

src/model/Player.java

     private int wallCount;
 
     private BoardCoordinate currentCoordinate;
-    private final BoardCoordinate startCoordinate;
-    private final BoardCoordinate finishCoordinate;
+    private final Baseline startBaseline;
+    private final Baseline finishBaseline;
 
     private PlayerInfo info;
 
     public Player (PlayerInfo info) {
-        this.startCoordinate = Board.makeStartCoordinateFromDirection (info.getDirection());
-        this.finishCoordinate = Board.makeStartCoordinateFromDirection (Direction.getInverse (info.getDirection()));
+        this.startBaseline = new Baseline(info.getDirection()) ;
+        this.finishBaseline = new Baseline (Direction.getInverse (info.getDirection()));
 
-        this.currentCoordinate = this.startCoordinate;
+        System.out.println("Going " + info.getDirection());
+        System.out.println("Starting on " + startBaseline.getCenterCoordinate());
+        System.out.println("Finishing on " + finishBaseline.getCenterCoordinate());
+
+
+        this.currentCoordinate = this.startBaseline.getCenterCoordinate ();
         this.info = info;
     }
 
         this.currentCoordinate = currentCoordinate;
     }
 
-    public BoardCoordinate getStartCoordinate () {
-        return startCoordinate;
-    }
-
-    public BoardCoordinate getFinishCoordinate() {
-        return finishCoordinate;
+    public Baseline getFinishBaseline() {
+        return finishBaseline;
     }
 
     public PlayerInfo getInfo() {

src/model/Wall.java

-package model;
-
-import java.util.Set;
-
-public class Wall {
-    private Player player;
-    private Set<Tile> tiles;
-
-    public Wall (Player p, Set<Tile> tiles) {
-        this.player = p;
-        this.tiles = tiles;
-    }
-
-    public Set<Tile> getTiles () {
-        return tiles;
-    }
-
-    public Player getPlayer () {
-        return player;
-    }
-
-    @Override
-    public boolean equals (Object other) {
-        if (other == this) return true;
-        if (! (other instanceof Wall)) return false;
-
-        // now we know we're a Wall, check all the tiles!
-        return (((Wall)other).tiles.equals (this.tiles));
-    }
-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.