diff --git a/src/game/Game.java b/src/game/Game.java index 7412fd5..4a22ab4 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -110,31 +110,120 @@ public class Game { } } - private int crawl(int i, int j) { - int symbol = this.board[i][j]; - if (i > 0) { - if (j > 0) { - - } - } - } - /** * */ private void checkState() { - outer:for (int i = 0; i < this.board.length; i++) { - for (int j = 0; j < this.board[i].length; j++) { - if (crawl(i, j) >= 4) { - log("We have a winner!"); + // --- CHECK WIN --- + outer:for (int i = 0; i < GAME_COLUMNS; i++) { + int count1 = 0; + int count2 = 0; + for (int j = 0; j < GAME_ROWS; j++) { + if (this.board[i][j] == 1) { + count1++; + count2 = 0; + } else if (this.board[i][j] == 2) { + count1 = 0; + count2++; + } else { + count1 = 0; + count2 = 0; + } + if (count1 >= 4) { + log("Player 1 won!"); + this.gameOn = false; + break outer; + } + if (count2 >= 4) { + log("Player 2 won!"); + this.gameOn = false; break outer; } } } + outer:for (int j = 0; j < GAME_ROWS; j++) { + int count1 = 0; + int count2 = 0; + for (int i = 0; i < GAME_COLUMNS; i++) { + if (this.board[i][j] == 1) { + count1++; + count2 = 0; + } else if (this.board[i][j] == 2) { + count1 = 0; + count2++; + } else { + count1 = 0; + count2 = 0; + } + if (count1 >= 4) { + log("Player 1 won!"); + this.gameOn = false; + break outer; + } + if (count2 >= 4) { + log("Player 2 won!"); + this.gameOn = false; + break outer; + } + } + } + boolean reverse = false; + int count1 = 0; + int count2 = 0; + int posX = 1, posY = -1; + int addX = -1, addY = 1; + outer:while (posX != GAME_COLUMNS -1 || posY != GAME_ROWS - 1) { + posX += addX; + posY += addY; + if (posX > GAME_COLUMNS - 1) { + posY += 2; + posX = GAME_COLUMNS - 1; + reverse = true; + } else if (posY > GAME_ROWS - 1) { + posX += 2; + posY = GAME_ROWS - 1; + reverse = true; + } else if (posX < 0) { + posX = 0; + reverse = true; + } else if (posY < 0) { + posY = 0; + reverse = true; + } + if (reverse) { + addX *= -1; + addY *= -1; + count1 = 0; + count2 = 0; + reverse = false; + } + if (this.board[posX][posY] == 1) { + count1++; + count2 = 0; + } else if (this.board[posX][posY] == 2) { + count1 = 0; + count2++; + } else { + count1 = 0; + count2 = 0; + } + if (count1 >= 4) { + log("Player 1 won!"); + this.gameOn = false; + break outer; + } + if (count2 >= 4) { + log("Player 2 won!"); + this.gameOn = false; + break outer; + } + } + // TODO: Finish other diagonals + // --- CHECK DRAW --- boolean draw = true; - outer:for (int i = 0; i < this.board.length; i++) { - for (int j = 0; j < this.board[i].length; j++) { + outer:for (int i = 0; i < GAME_COLUMNS; i++) { + for (int j = 0; j < GAME_ROWS; j++) { if (this.board[i][j] == 0) { draw = false; break outer; @@ -153,8 +242,8 @@ public class Game { */ private int[][] copyBoard() { int[][] ret = new int[GAME_COLUMNS][GAME_ROWS]; - for (int i = 0; i < this.board.length; i++) { - for (int j = 0; j < this.board[i].length; j++) { + for (int i = 0; i < GAME_COLUMNS; i++) { + for (int j = 0; j < GAME_ROWS; j++) { ret[i][j] = this.board[i][j]; } }