diff --git a/TicTacToe - MinMax/src/Board.java b/TicTacToe - MinMax/src/Board.java index e7746ad..264e8fb 100644 --- a/TicTacToe - MinMax/src/Board.java +++ b/TicTacToe - MinMax/src/Board.java @@ -104,7 +104,7 @@ public class Board extends JPanel implements ActionListener { public void run() { //check if game state evaluation needs to be done if (isChanged(oldPlayfield)) { - gameWon = game.checkWin(); + gameWon = game.checkWin(game.getPlayfield()); //repaint board if not won if (!gameWon) { repaint(); diff --git a/TicTacToe - MinMax/src/Game.java b/TicTacToe - MinMax/src/Game.java index 289ecbb..bfc302b 100644 --- a/TicTacToe - MinMax/src/Game.java +++ b/TicTacToe - MinMax/src/Game.java @@ -1,4 +1,5 @@ import javax.swing.*; +import java.util.Arrays; public class Game { @@ -24,71 +25,20 @@ public class Game { } public void computersTurn(){ - place(findBestMove(playfield), -1); + } - private int minmax(int[] board, int depth, boolean isMaximizing){ - int score = getBoardValue(); - - if (boardValue == 10 || boardValue == -10){ - return score; - } - - if (emptyTiles() == 0){ - return 0; - } - - if (isMaximizing){ - int best = -1000; - for (int i = 0; i < board.length; i++){ - if (board[i] == 0){ - board[i] = 1; - best = Math.max(score, minmax(board, depth + 1, false)) - depth; - board[i] = 0; - } - } - return best; - } else { - int best = 1000; - for (int i = 0; i < board.length; i++){ - if (board[i] == 0){ - board[i] = -1; - best = Math.min(score, minmax(board, depth + 1, true)) + depth; - board[i] = 0; - } - } - return best; - } - } - - private int findBestMove(int[] board){ - int bestVal = -1000; - int index = -1; - - for (int i = 0; i < board.length; i++){ - if (board[i] == 0){ - board[i] = 1; - int moveVal = minmax(board, 0, false); - board[i] = 0; - if (moveVal > bestVal){ - index = i; - bestVal = moveVal; - } - } - } - return index; - } - - public boolean checkWin() { + public boolean checkWin(int[] board) { //only check if winning is possible if (emptyTiles() < 5) { + System.out.println(boardValue); for (int i = 0; i < 3; i++) { //horizontal - if ((playfield[i] == playfield[i + 3] && playfield[i] != 0) && (playfield[i] == playfield[i + 6])) { + if ((board[i] == board[i + 3] && board[i] != 0) && (board[i] == board[i + 6])) { winningX1 = 75; winningX2 = 825; winningY1 = winningY2 = i * 300 + 150; - if (playfield[i] == 1){ + if (board[i] == 1){ boardValue = 10; } else { boardValue = -10; @@ -96,11 +46,11 @@ public class Game { return true; } //vertical - else if ((playfield[i * 3] == playfield[i * 3 + 1] && playfield[i * 3] != 0) && (playfield[i * 3] == playfield[i * 3 + 2])) { + else if ((board[i * 3] == board[i * 3 + 1] && board[i * 3] != 0) && (board[i * 3] == board[i * 3 + 2])) { winningY1 = 75; winningY2 = 825; winningX1 = winningX2 = i * 300 + 150; - if (playfield[i * 3] == 1){ + if (board[i * 3] == 1){ boardValue = 10; } else { boardValue = -10; @@ -109,19 +59,19 @@ public class Game { } } //diagonal - if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){ + if ((board[2] == board[4] && board[2] != 0) && (board[2] == board[6])){ winningX2 = winningY1 = 75; winningX1 = winningY2 = 825; - if (playfield[2] == 1){ + if (board[2] == 1){ boardValue = 10; } else { boardValue = -10; } return true; - } else if ((playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8])){ + } else if ((board[0] == board[4] && board[0] != 0) && (board[0] == board[8])){ winningX1 = winningY1 = 75; winningX2 = winningY2 = 825; - if (playfield[0] == 1){ + if (board[0] == 1){ boardValue = 10; } else { boardValue = -10; @@ -175,8 +125,8 @@ public class Game { return winningY2; } - public int getBoardValue() { - checkWin(); + public int getBoardValue(int[] board) { + checkWin(board); return boardValue; } diff --git a/out/production/TicTacToe - MinMax/Board$2.class b/out/production/TicTacToe - MinMax/Board$2.class index 53920d2..8dd6fb0 100644 Binary files a/out/production/TicTacToe - MinMax/Board$2.class and b/out/production/TicTacToe - MinMax/Board$2.class differ diff --git a/out/production/TicTacToe - MinMax/Game.class b/out/production/TicTacToe - MinMax/Game.class index 183a47e..5191e1d 100644 Binary files a/out/production/TicTacToe - MinMax/Game.class and b/out/production/TicTacToe - MinMax/Game.class differ