Commit 195fe892 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

refactored win condition methods, added docs

parent 20394c11
......@@ -117,21 +117,45 @@ public abstract class MNKGame {
* @return true if there are k in a row such that the line intersects (x, y), false otherwise
*/
private boolean checkWinCondition(int x, int y) {
int count = 0;
Piece current = grid.get(x, y);
if(current==Piece.NONE)
return false;
// System.out.println();
// System.out.println("Currently at " + x + "," + y + " value " + current);
// System.out.println();
if(checkHorizontal(x, y, current))
return true;
if(checkVertical(x, y, current))
return true;
if(checkDiagonalTLBR(x, y, current))
return true;
if(checkDiagonalTRBL(x, y, current))
return true;
System.out.println();
System.out.println("Currently at " + x + "," + y + " value " + current);
System.out.println();
return false;
}
//horizontal
for (int i = x-k; i<x+k; i++) {
if(i <0 || i>=getWidth())
/**
* Checks for k pieces in a row diagonally around from the pivot point (x, y)
* such that for x in X and y in Y this method checks the following positions
* (x, y)
* (x-1, y+1)
* (x-2, y+3)
* ..
*
* @param x the x-coordinate
* @param y the y-coordinate
* @param current the value of the piece at (x,y)
* @return true if a k-row was found, false otherwise
*/
private boolean checkDiagonalTRBL(int x, int y, Piece current) {
int count = 0;
for (int i = x+k, j=y-k; i>x-k && j<y+k; i--, j++) {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
continue;
System.out.println("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count);
if(grid.get(i, y) != current) {
System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
}
else {
......@@ -140,14 +164,30 @@ public abstract class MNKGame {
if(count==k)
return true;
}
return false;
}
count = 0;
//vertical
for (int j = y-k; j<y+k; j++) {
if(j <0 || j>=getHeight())
/**
* Checks for k pieces in a row diagonally around from the pivot point (x, y)
* such that for i in X and j in Y this method checks the following positions
* (x, y)
* (x+1, y+1)
* (x+2, y+3)
* ..
*
* @param x the x-coordinate
* @param y the y-coordinate
* @param current the value of the piece at (x,y)
* @return true if a k-row was found, false otherwise
*/
private boolean checkDiagonalTLBR(int x, int y, Piece current) {
int count = 0;
for (int i = x-k, j=y-k; i<x+k && j<y+k; i++, j++) {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
continue;
System.out.println("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
if(grid.get(x, j) != current) {
System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
}
else {
......@@ -156,15 +196,23 @@ public abstract class MNKGame {
if(count==k)
return true;
}
return false;
}
count = 0;
//diagonal (0,0) -> (l, l) where l := min(m, n)
for (int i = x-k, j=y-k; i<x+k && j<y+k; i++, j++) {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
/**
* Checks for k pieces in a row along the y-axis from the pivot point (x, y)
* @param x the x-coordinate
* @param y the y-coordinate
* @param current the value of the piece at (x,y)
* @return true if a k-row was found, false otherwise
*/
private boolean checkVertical(int x, int y, Piece current) {
int count = 0;
for (int j = y-k; j<y+k; j++) {
if(j <0 || j>=getHeight())
continue;
System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
System.out.println("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
if(grid.get(x, j) != current) {
count = 0;
}
else {
......@@ -173,15 +221,23 @@ public abstract class MNKGame {
if(count==k)
return true;
}
return false;
}
count = 0;
//diagonal (0,n) -> (m, 0) where l := min(m, n)
for (int i = x+k, j=y-k; i>x-k && j<y+k; i--, j++) {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight())
/**
* Checks for k pieces in a row along the x-axis from the pivot point (x, y)
* @param x the x-coordinate
* @param y the y-coordinate
* @param current the value of the piece at (x,y)
* @return true if a k-row was found, false otherwise
*/
private boolean checkHorizontal(int x, int y, Piece current) {
int count = 0;
for (int i = x-k; i<x+k; i++) {
if(i <0 || i>=getWidth())
continue;
System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
System.out.println("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count);
if(grid.get(i, y) != current) {
count = 0;
}
else {
......
Markdown is supported
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