Skip to content
Snippets Groups Projects
Commit 2dccc0de authored by Shahad's avatar Shahad
Browse files

Fix bugs in the code

parent e779d58a
Branches 1-fix-assignment-errors
No related tags found
No related merge requests found
public class DotsAndBoxesGame {
private boolean[][] horizontalLines;
private boolean[][] verticalLines;
private int gridSize;
public DotsAndBoxesGame(int gridSize) {
this.gridSize = gridSize;
horizontalLines = new boolean[gridSize + 1][gridSize];
verticalLines = new boolean[gridSize][gridSize + 1];
}
public boolean drawHorizontalLine(int row, int col) {
if (row >= 0 && row < gridSize + 1 && col >= 0 && col < gridSize && !horizontalLines[row][col]) {
horizontalLines[row][col] = true;
return true;
}
return false;
}
public boolean drawVerticalLine(int row, int col) {
if (row >= 0 && row < gridSize && col >= 0 && col < gridSize + 1 && !verticalLines[row][col]) {
verticalLines[row][col] = true;
return true;
}
return false;
}
public boolean isLineDrawn(int row, int col, LineType type) {
if (type == LineType.HORIZONTAL) {
return horizontalLines[row][col];
} else if (type == LineType.VERTICAL) {
return verticalLines[row][col];
}
return false;
}
public boolean isSquareComplete(int row, int col) {
return isLineDrawn(row, col, LineType.HORIZONTAL) &&
isLineDrawn(row + 1, col, LineType.HORIZONTAL) &&
isLineDrawn(row, col, LineType.VERTICAL) &&
isLineDrawn(row, col + 1, LineType.VERTICAL);
}
}
enum LineType {
HORIZONTAL, VERTICAL
}
......@@ -7,7 +7,7 @@ public class DotsAndBoxesGameTest {
@Test
public void testSquareComplete() {
// Given
DotsAndBoxesGame game = new DotsAndBoxesGame();
DotsAndBoxesGame game = new DotsAndBoxesGame(2);
// Assuming the following lines form a complete square
game.drawHorizontalLine(0, 0);
game.drawHorizontalLine(0, 1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment