Skip to content
Snippets Groups Projects
Commit 4883d091 authored by kalpana acharya's avatar kalpana acharya
Browse files

assignment errors has been fixed

parent 13c3b093
Branches
No related tags found
No related merge requests found
package dotsandboxes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
......@@ -50,6 +49,7 @@ public class DotsAndBoxesGrid {
final int players;
private int player = 1;
public int getPlayer() {
return player;
}
......@@ -98,7 +98,6 @@ public class DotsAndBoxesGrid {
return boxOwners[x][y];
}
/**
* Checks whether a box has been fully drawn (all four sides)
* @param x coordinate of the left side of the box
......@@ -110,9 +109,13 @@ public class DotsAndBoxesGrid {
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;
// Check if all sides of the box are drawn
boolean top = horizontals[x][y];
boolean bottom = horizontals[x][y + 1];
boolean left = verticals[x][y];
boolean right = verticals[x + 1][y];
return top && bottom && left && right;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -139,7 +142,10 @@ 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.
// Throw an exception if the line was already drawn
if (horizontals[x][y]) {
throw new IllegalStateException("This horizontal line is already drawn.");
}
this.horizontals[x][y] = true;
......@@ -170,10 +176,14 @@ 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.
// Throw an exception if the line was already drawn
if (verticals[x][y]) {
throw new IllegalStateException("This vertical line is already drawn.");
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
// Try to claim the east or west boxes
boolean claimE = claimBox(x, y, player);
boolean claimW = claimBox(x - 1, y, player);
if (claimE || claimW) {
......@@ -184,7 +194,6 @@ public class DotsAndBoxesGrid {
notifyObservers();
return false;
}
}
public boolean gameComplete() {
......@@ -192,6 +201,4 @@ public class DotsAndBoxesGrid {
// The grid is complete if "for all rows, all the boxes in that row have a non-zero owner"
return Arrays.stream(boxOwners).allMatch((row) -> Arrays.stream(row).allMatch((owner) -> owner > 0));
}
}
......@@ -32,19 +32,19 @@ public class DotsAndBoxesGridTest {
logger.info("Testing square completion logic");
// Create a new game grid
DotsAndBoxesGrid grid = new DotsAndBoxesGrid();
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2);
// Draw lines to form a square
grid.drawLine(0, 0, 0, 1); // Top horizontal line
grid.drawLine(0, 0, 1, 0); // Left vertical line
grid.drawLine(0, 1, 1, 1); // Bottom horizontal line
grid.drawLine(1, 0, 1, 1); // Right vertical line
grid.drawHorizontal(0, 0, 1); // Top horizontal line
grid.drawVertical(0, 0, 1); // Left vertical line
grid.drawHorizontal(0, 1, 1); // Bottom horizontal line
grid.drawVertical(1, 0, 1); // Right vertical line
// Test if the square at (0, 0) is complete
boolean isComplete = grid.isSquareComplete(0, 0);
boolean isComplete = grid.boxComplete(0, 0);
// The bug is that isSquareComplete always returns true
assertFalse(isComplete, "The square completion check should fail because the method is broken.");
// The test should pass now that the logic is fixed
assertTrue(isComplete, "The square at (0, 0) should be complete.");
}
@Test
......@@ -52,14 +52,22 @@ public class DotsAndBoxesGridTest {
logger.info("Testing drawing the same line twice throws exception");
// Create a new game grid
DotsAndBoxesGrid grid = new DotsAndBoxesGrid();
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2);
// Draw a line once
grid.drawLine(0, 0, 0, 1);
grid.drawHorizontal(0, 0, 1);
// Attempt to draw the same line again
assertThrows(IllegalStateException.class, () -> {
grid.drawLine(0, 0, 0, 1);
}, "Drawing the same line twice should throw IllegalStateException.");
grid.drawHorizontal(0, 0, 1);
}, "Drawing the same horizontal line twice should throw IllegalStateException.");
// Draw a vertical line once
grid.drawVertical(0, 0, 1);
// Attempt to draw the same vertical line again
assertThrows(IllegalStateException.class, () -> {
grid.drawVertical(0, 0, 1);
}, "Drawing the same vertical line twice should throw IllegalStateException.");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment