Commit 7af07451 authored by rbirch4's avatar rbirch4
Browse files

Merged issue branch to main. Fixed (hopefully) merge conflicts.

parents 5e698563 912741b6
......@@ -110,12 +110,88 @@ public class DotsAndBoxesGrid {
if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
return false;
}
boolean boxFlag = false;
switch (boundaryLine(x, y)) {
case 1:
//left vertical boundary
if (getHorizontal(x, y) && getHorizontal(x, (y+1)) && getVertical((x+1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
case 2:
//right vertical boundary
if (getHorizontal((x-1), y) && getHorizontal((x-1), (y+1)) && getVertical((x-1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
case 3:
//top of grid
if (getHorizontal(x, (y+1)) && getVertical(x, y) && getVertical((x+1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
case 4:
//bottom of grid
if (getHorizontal(x, (y-1)) && getVertical(x, y) && getVertical((x+1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
default:
//not a boundary
if (getHorizontal(x, y) && getHorizontal(x, (y+1)) && getVertical(x, y) && getVertical((x+1), y)) {
//south box
boxFlag = true;
}
else {
boxFlag = false;
}
break;
}
// 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;
//Return true if box complete, false if not.
return boxFlag;
}
public int boundaryLine(int x, int y) {
int boundaryFlag = 0;
if (x == 0) {
boundaryFlag = 1;
//left vertical boundary.
}
else if (x == (width-1)) {
boundaryFlag = 2;
//right vertical boundary
}
else if (y == 0) {
boundaryFlag = 3;
//top of grid
//u, l, r
}
else if (y == height) {
boundaryFlag = 4;
//bottom of grid
//d, l, r
}
else {
return boundaryFlag;
}
return boundaryFlag;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
private boolean claimBox(int x, int y, int p) {
if (boxComplete(x, y)) {
......@@ -139,22 +215,27 @@ public class DotsAndBoxesGrid {
if (y >= height || y < 0) {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height));
}
if (this.horizontals[x][y] == true) {
throw new IllegalStateException(String.format("Warning: This line has already been drawn by a player!"));
}
else {
this.horizontals[x][y] = true;
//Uses the coords from what was just drawn above to see if the drawn line will complete either a top or bottom box. Checking done through boxComplete called in claimBox.
boolean claimN = claimBox(x, y-1, player);
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
// FIXME: You need to throw an exception if the line was already drawn.
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;
}
}
/**
......@@ -170,22 +251,26 @@ public class DotsAndBoxesGrid {
if (y >= height - 1 || y < 0) {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1));
}
if (this.verticals[x][y] == true) {
throw new IllegalStateException(String.format("This line has already been drawn by another player!"));
}
else {
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;
}
}
// 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;
}
}
public boolean gameComplete() {
......
package dotsandboxes;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/** Our main class that launches the app. */
public class Main extends Application {
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 2);
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Dots and Boxes");
// FIXME: Update this label to show your name and student number - DONE.
Label label = new Label("Name: Ryan Birch 220219214");
BorderPane borderPane = new BorderPane();
borderPane.setBottom(label);
Scene scene = new Scene(borderPane, 600, 400);
DotsAndBoxesUI dbUi = new DotsAndBoxesUI(grid);
borderPane.setCenter(dbUi.anchorPane);
borderPane.setTop(dbUi.label);
primaryStage.setScene(scene);
primaryStage.show();
// This sets what to do when we close the main window.
// Notice that we are using a "lambda function" (i.e., an anonymously defined function defined within the
// call to setOnCloseRequest). These are very useful in GUI code and we'll probably see a lot of them in the
// project.
primaryStage.setOnCloseRequest((evt) -> System.exit(0));
}
}
package dotsandboxes;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/** Our main class that launches the app. */
public class Main extends Application {
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 2);
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Dots and Boxes");
// FIXME: Update this label to show your name and student number - DONE.
Label label = new Label("Name: Ryan Birch 220219214");
BorderPane borderPane = new BorderPane();
borderPane.setBottom(label);
Scene scene = new Scene(borderPane, 600, 400);
DotsAndBoxesUI dbUi = new DotsAndBoxesUI(grid);
borderPane.setCenter(dbUi.anchorPane);
borderPane.setTop(dbUi.label);
primaryStage.setScene(scene);
primaryStage.show();
// This sets what to do when we close the main window.
// Notice that we are using a "lambda function" (i.e., an anonymously defined function defined within the
// call to setOnCloseRequest). These are very useful in GUI code and we'll probably see a lot of them in the
// project.
primaryStage.setOnCloseRequest((evt) -> System.exit(0));
}
}
package dotsandboxes;
import org.junit.jupiter.api.*;
//import jdk.jfr.Timestamp;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
......@@ -28,4 +31,52 @@ public class DotsAndBoxesGridTest {
}
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public void testBoxCompleteCorrect() {
//Draw an arbitrary line that obviously does not complete a box - only one is required because it clearly does not complete a box.
/*Call the boxComplete method to try to validate whether a box has been drawn. It will always return true.
*We want to assert FALSE becuase we technically want the method to return false, but the method will ways return true resulting in a fail.
*We know that this is correct becuase we know for a fact that the box has NOT been completed, yet the method will return true and the test will fail
*because technically we want it to return FALSE.
*/
//Imagine that only a single arbitrary line exists on a dots and boxes grid of any size. Then, we know for sure that the box has not been drawn and
//should return false. Not dependent on a dots and boxes object existing as method is recursively called when "drawing" a line but is not yet complete.
int knownIncompleteX = 1;
int knownIncompleteY = 1;
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(4, 4, 1);
boolean incompleteBox = test3by3.drawHorizontal(knownIncompleteX, knownIncompleteY, 1);
boolean boxCompleteResult = test3by3.boxComplete(knownIncompleteX, knownIncompleteY);
logger.info(boxCompleteResult);
assertFalse(boxCompleteResult);
}
@Test
public void testNoDrawOverwriteHorz() {
//Create a dots and boxes grid - a 3x3 will do.
//Create an example player.
//Draw a horizontal line from 1, 2 to 2,2.
//Draw the above line again and assert that it throws an IllegalStateException.
int horzX = 1;
int horzY = 2;
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(4, 4, 1);
boolean initialLine = test3by3.drawHorizontal(horzX, horzY, 1);
assertThrows(IllegalStateException.class, () -> test3by3.drawHorizontal(horzX, horzY, 1));
}
@Test
public void testNoDrawOverwriteVert() {
//Create a dots and boxes grid - a 3x3 will do.
//Create an example player.
//Draw a horizontal line from 1, 2 to 2,2.
//Draw the above line again and assert that it throws an IllegalStateException.
int vertX = 1;
int vertY = 1;
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(4, 4, 1);
boolean initialLine = test3by3.drawVertical(vertX, vertY, 1);
assertThrows(IllegalStateException.class, () -> test3by3.drawVertical(vertX, vertY, 1));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment