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

refactored win condition methods, added docs

parent 20394c11
...@@ -86,13 +86,13 @@ public abstract class MNKGame { ...@@ -86,13 +86,13 @@ public abstract class MNKGame {
throw new IllegalArgumentException("Cannot add a piece to this position"); throw new IllegalArgumentException("Cannot add a piece to this position");
if(grid.get(x, y) != Piece.NONE) if(grid.get(x, y) != Piece.NONE)
throw new IllegalArgumentException("Position is already filled"); throw new IllegalArgumentException("Position is already filled");
grid.set(x, y, currentPlayer); grid.set(x, y, currentPlayer);
if(checkWinCondition(x, y)) { if(checkWinCondition(x, y)) {
winnerPiece = currentPlayer; winnerPiece = currentPlayer;
} }
currentPlayer = currentPlayer.next(); currentPlayer = currentPlayer.next();
} }
...@@ -102,14 +102,14 @@ public abstract class MNKGame { ...@@ -102,14 +102,14 @@ public abstract class MNKGame {
public boolean hasWinner() { public boolean hasWinner() {
return winnerPiece!=Piece.NONE; return winnerPiece!=Piece.NONE;
} }
/** /**
* @return the piece that won the game * @return the piece that won the game
*/ */
public Piece getWinner() { public Piece getWinner() {
return winnerPiece; return winnerPiece;
} }
/** /**
* Checks for k in a row such that the line intersects (x, y) * Checks for k in a row such that the line intersects (x, y)
* @param x the x coordinate * @param x the x coordinate
...@@ -117,82 +117,138 @@ public abstract class MNKGame { ...@@ -117,82 +117,138 @@ public abstract class MNKGame {
* @return true if there are k in a row such that the line intersects (x, y), false otherwise * @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) { private boolean checkWinCondition(int x, int y) {
int count = 0; Piece current = grid.get(x, y);
Piece current = grid.get(x, y); if(current==Piece.NONE)
if(current==Piece.NONE) return false;
return false; // System.out.println();
// System.out.println("Currently at " + x + "," + y + " value " + current);
System.out.println(); // System.out.println();
System.out.println("Currently at " + x + "," + y + " value " + current); if(checkHorizontal(x, y, current))
System.out.println(); return true;
if(checkVertical(x, y, current))
//horizontal return true;
for (int i = x-k; i<x+k; i++) { if(checkDiagonalTLBR(x, y, current))
if(i <0 || i>=getWidth()) return true;
continue; if(checkDiagonalTRBL(x, y, current))
System.out.println("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count); return true;
if(grid.get(i, y) != current) {
count = 0; return false;
} }
else {
count++; /**
} * Checks for k pieces in a row diagonally around from the pivot point (x, y)
if(count==k) * such that for x in X and y in Y this method checks the following positions
return true; * (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("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0; count = 0;
//vertical }
for (int j = y-k; j<y+k; j++) { else {
if(j <0 || j>=getHeight()) count++;
continue; }
System.out.println("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count); if(count==k)
if(grid.get(x, j) != current) { return true;
count = 0; }
} return false;
else { }
count++;
} /**
if(count==k) * Checks for k pieces in a row diagonally around from the pivot point (x, y)
return true; * 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("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0; 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++) { else {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight()) count++;
continue; }
System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count); if(count==k)
return true;
if(grid.get(i, j) != current) { }
count = 0; return false;
} }
else {
count++; /**
} * Checks for k pieces in a row along the y-axis from the pivot point (x, y)
if(count==k) * @param x the x-coordinate
return true; * @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("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
if(grid.get(x, j) != current) {
count = 0; 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++) { else {
if(i <0 || j <0 || i>=getWidth() || j>=getHeight()) count++;
continue; }
System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count); if(count==k)
return true;
if(grid.get(i, j) != current) { }
count = 0;
}
else {
count++;
}
if(count==k)
return true;
}
return false; return false;
} }
/**
* 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("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count);
if(grid.get(i, y) != current) {
count = 0;
}
else {
count++;
}
if(count==k)
return true;
}
return false;
}
@Override @Override
public String toString() { public String toString() {
String r = ""; String r = "";
...@@ -204,7 +260,7 @@ public abstract class MNKGame { ...@@ -204,7 +260,7 @@ public abstract class MNKGame {
} }
return r; return r;
} }
private String debugToString(int x, int y) { private String debugToString(int x, int y) {
System.out.println("x="+x+", y="+y); System.out.println("x="+x+", y="+y);
String r = ""; String r = "";
...@@ -220,5 +276,5 @@ public abstract class MNKGame { ...@@ -220,5 +276,5 @@ public abstract class MNKGame {
} }
return r; return r;
} }
} }
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