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 {
throw new IllegalArgumentException("Cannot add a piece to this position");
if(grid.get(x, y) != Piece.NONE)
throw new IllegalArgumentException("Position is already filled");
grid.set(x, y, currentPlayer);
if(checkWinCondition(x, y)) {
winnerPiece = currentPlayer;
}
currentPlayer = currentPlayer.next();
}
......@@ -102,14 +102,14 @@ public abstract class MNKGame {
public boolean hasWinner() {
return winnerPiece!=Piece.NONE;
}
/**
* @return the piece that won the game
*/
public Piece getWinner() {
return winnerPiece;
}
/**
* Checks for k in a row such that the line intersects (x, y)
* @param x the x coordinate
......@@ -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
*/
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();
//horizontal
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;
}
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;
return false;
}
/**
* 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("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
//vertical
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;
}
else {
count++;
}
if(count==k)
return true;
}
}
else {
count++;
}
if(count==k)
return true;
}
return false;
}
/**
* 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("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
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())
continue;
System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
}
else {
count++;
}
if(count==k)
return true;
}
}
else {
count++;
}
if(count==k)
return true;
}
return false;
}
/**
* 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("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
if(grid.get(x, j) != current) {
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())
continue;
System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
if(grid.get(i, j) != current) {
count = 0;
}
else {
count++;
}
if(count==k)
return true;
}
}
else {
count++;
}
if(count==k)
return true;
}
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
public String toString() {
String r = "";
......@@ -204,7 +260,7 @@ public abstract class MNKGame {
}
return r;
}
private String debugToString(int x, int y) {
System.out.println("x="+x+", y="+y);
String r = "";
......@@ -220,5 +276,5 @@ public abstract class MNKGame {
}
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