Commit 49cc2733 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

refactored code for checking winner + wincondition

parent 9f5902ce
...@@ -65,7 +65,7 @@ public abstract class MNKGame { ...@@ -65,7 +65,7 @@ public abstract class MNKGame {
grid.set(x, y, currentPlayer); grid.set(x, y, currentPlayer);
if(getWinner(x, y)!=Piece.NONE) { if(checkWinCondition(x, y)) {
winnerPiece = currentPlayer; winnerPiece = currentPlayer;
} }
...@@ -76,11 +76,15 @@ public abstract class MNKGame { ...@@ -76,11 +76,15 @@ public abstract class MNKGame {
return winnerPiece!=Piece.NONE; return winnerPiece!=Piece.NONE;
} }
public Piece getWinner(int x, int y) { public Piece getWinner() {
return winnerPiece;
}
private boolean checkWinCondition(int x, int y) {
int count = 0; int count = 0;
Piece current = grid.get(x, y); Piece current = grid.get(x, y);
if(current==Piece.NONE) if(current==Piece.NONE)
return Piece.NONE; return false;
System.out.println(); System.out.println();
System.out.println("Currently at " + x + "," + y + " value " + current); System.out.println("Currently at " + x + "," + y + " value " + current);
...@@ -98,7 +102,7 @@ public abstract class MNKGame { ...@@ -98,7 +102,7 @@ public abstract class MNKGame {
count++; count++;
} }
if(count==k) if(count==k)
return current; return true;
} }
count = 0; count = 0;
...@@ -114,7 +118,7 @@ public abstract class MNKGame { ...@@ -114,7 +118,7 @@ public abstract class MNKGame {
count++; count++;
} }
if(count==k) if(count==k)
return current; return true;
} }
count = 0; count = 0;
...@@ -131,7 +135,7 @@ public abstract class MNKGame { ...@@ -131,7 +135,7 @@ public abstract class MNKGame {
count++; count++;
} }
if(count==k) if(count==k)
return current; return true;
} }
count = 0; count = 0;
...@@ -148,9 +152,9 @@ public abstract class MNKGame { ...@@ -148,9 +152,9 @@ public abstract class MNKGame {
count++; count++;
} }
if(count==k) if(count==k)
return current; return true;
} }
return Piece.NONE; return false;
} }
@Override @Override
......
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