diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index 32667af6c63a0287bd4f91c37682acb30c08e9a4..a0e915ec2ff5d993a63b610651a2b87f0cdd29d5 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -110,10 +110,12 @@ public class DotsAndBoxesGrid { if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) { return false; } + if (getHorizontal(x,y) && getHorizontal(x,y+1) && getVertical(x+1,y) && getVertical(x,y)){ + return true; + } else { + return false; + } - // A box is complete if the north and south horizontals and the east and west verticals have all been drawn. - // FIXME: You'll need to fix this code (after writing a test first). - return true; } /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ @@ -140,21 +142,26 @@ public class DotsAndBoxesGrid { throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height)); } - // FIXME: You need to throw an exception if the line was already drawn. + if(this.horizontals[x][y]){ + throw new RuntimeException("Line already drawn."); + + }else{ + this.horizontals[x][y] = true; + + // Try to claim the north or south boxes + boolean claimN = claimBox(x, y-1, player); + boolean claimS = claimBox(x, y, player); + if (claimN || claimS) { + notifyObservers(); + return true; + } else { + nextPlayer(); + notifyObservers(); + return false; + } + } - this.horizontals[x][y] = true; - // Try to claim the north or south boxes - boolean claimN = claimBox(x, y-1, player); - boolean claimS = claimBox(x, y, player); - if (claimN || claimS) { - notifyObservers(); - return true; - } else { - nextPlayer(); - notifyObservers(); - return false; - } } /** @@ -171,19 +178,23 @@ public class DotsAndBoxesGrid { throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1)); } - // You need to throw an exception if the line was already drawn. - - this.verticals[x][y] = true; - // Try to claim the north or south boxes - boolean claimE = claimBox(x, y, player); - boolean claimW = claimBox(x-1, y, player); - if (claimE || claimW) { - notifyObservers(); - return true; - } else { - nextPlayer(); - notifyObservers(); - return false; + if(this.verticals[x][y]){ + throw new RuntimeException("Line already drawn."); + + }else{ + this.verticals[x][y] = true; + + // Try to claim the north or south boxes + boolean claimN = claimBox(x, y-1, player); + boolean claimS = claimBox(x, y, player); + if (claimN || claimS) { + notifyObservers(); + return true; + } else { + nextPlayer(); + notifyObservers(); + return false; + } } }