Commit 7411f39e authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

fixed bug, currently runs correctly

parent 0de098d1
......@@ -8,6 +8,7 @@ public abstract class MNKGame {
private int k;
private Grid<Piece> grid;
private Piece currentPlayer;
private Piece winnerPiece;
enum Piece { NONE, BLACK, WHITE;
......@@ -28,6 +29,7 @@ public abstract class MNKGame {
this.k = k;
grid = new Grid<MNKGame.Piece>(width, height, Piece.NONE);
currentPlayer = Piece.BLACK;
winnerPiece = Piece.NONE;
}
......@@ -63,26 +65,32 @@ public abstract class MNKGame {
grid.set(x, y, currentPlayer);
if(getWinner(x, y)!=Piece.NONE) {
winnerPiece = currentPlayer;
}
currentPlayer = currentPlayer.next();
}
public boolean hasWinner() {
return getWinner()!=Piece.NONE;
return winnerPiece!=Piece.NONE;
}
public Piece getWinner() {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
public Piece getWinner(int x, int y) {
int count = 0;
Piece current = grid.get(x, y);
if(current==Piece.NONE)
continue;
return Piece.NONE;
//System.out.println();
//System.out.println("Currently at " + x + "," + y + " value " + current);
//System.out.println();
//horizontal
for (int i = x-k+1; i<=x+k-1; i++) {
System.out.println("Looking at \n" + debugToString(i, y));
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;
}
......@@ -95,10 +103,10 @@ public abstract class MNKGame {
count = 0;
//vertical
for (int j = y-k+1; j >=0 && j<=y+k-1; j++) {
System.out.println("Looking at \n" + debugToString(x, j));
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;
}
......@@ -111,10 +119,10 @@ public abstract class MNKGame {
count = 0;
//diagonal (0,0) -> (l, l) where l := min(m, n)
for (int i = x-k+1, j=y-k+1; i<=x+k-1 && j<=y+k-1; i++, j++) {
System.out.println("Looking at \n" + debugToString(i, j));
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;
......@@ -126,11 +134,12 @@ public abstract class MNKGame {
return current;
}
count = 0;
//diagonal (0,n) -> (m, 0) where l := min(m, n)
for (int i = x+k-1, j=y-k+1; i>=x-k+1 && j<=y+k-1; i--, j++) {
System.out.println("Looking at \n" + debugToString(i, j));
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;
......@@ -141,10 +150,6 @@ public abstract class MNKGame {
if(count==k)
return current;
}
}
}
return Piece.NONE;
}
......@@ -160,7 +165,8 @@ public abstract class MNKGame {
return r;
}
public String debugToString(int x, int y) {
private String debugToString(int x, int y) {
//System.out.println("x="+x+", y="+y);
String r = "";
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
......
......@@ -209,7 +209,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
private void updateMessage() {
if (game.hasWinner())
message.setText("The game is over! The winner is " + game.getWinner().name().toLowerCase());
message.setText("The game is over!");// The winner is " + game.getWinner().name().toLowerCase());
else
message.setText("Playing");
}
......
......@@ -63,7 +63,7 @@ class TicTacToeTest {
game.putPieceAt(1, 1); //o
game.putPieceAt(1, 0); //x
game.putPieceAt(2, 2); //o
assertTrue(game.hasWinner());
assertTrue(game.hasWinner(), ""+game);
}
@Test
......@@ -113,7 +113,7 @@ class TicTacToeTest {
@Test
void testHasNoWinnerDiagonalTopRDownLFirstPlayer() {
/*
* Checks that the following board has a winner
* Checks that the following board does not have a winner
*
* - - -
* |x x o|
......@@ -143,9 +143,9 @@ class TicTacToeTest {
* Checks that the following board has a winner
*
* - - -
* |o o o|
* |x x |
* |x o|
* |o x x|
* |o x |
* |o o|
* - - -
*/
......@@ -155,7 +155,7 @@ class TicTacToeTest {
game.putPieceAt(0, 1); //o
game.putPieceAt(2, 0); //x
game.putPieceAt(0, 2); //o
assertTrue(game.hasWinner());
assertTrue(game.hasWinner(), ""+game);
}
}
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