diff --git a/TicTacToe - MinMax/src/Board.java b/TicTacToe - MinMax/src/Board.java index 694f605..e7746ad 100644 --- a/TicTacToe - MinMax/src/Board.java +++ b/TicTacToe - MinMax/src/Board.java @@ -15,9 +15,7 @@ public class Board extends JPanel implements ActionListener { private final int DELAY = 50; private boolean ended = false; - private boolean gameWon = false; - private boolean initialized = false; int[] oldPlayfield; @@ -75,7 +73,7 @@ public class Board extends JPanel implements ActionListener { actions++; } } - if (ended) { + if (gameWon) { painter.paintWinnerLine(g); } } @@ -87,7 +85,7 @@ public class Board extends JPanel implements ActionListener { timer.start(); oldPlayfield = game.getPlayfield().clone(); game.setTurnTaken(false); - ended = false; + gameWon = false; repaint(); } @@ -113,9 +111,10 @@ public class Board extends JPanel implements ActionListener { oldPlayfield = game.getPlayfield().clone(); } //stop timer if game won - else if (gameWon || game.emptyTiles() == 0) { - ended = true; - setWinningLine(); + if (gameWon || game.emptyTiles() == 0) { + if (gameWon) { + setWinningLine(); + } repaint(); timer.stop(); try { diff --git a/TicTacToe - MinMax/src/Game.java b/TicTacToe - MinMax/src/Game.java index 842a541..856acf3 100644 --- a/TicTacToe - MinMax/src/Game.java +++ b/TicTacToe - MinMax/src/Game.java @@ -3,11 +3,13 @@ import javax.swing.*; public class Game { private int[] playfield; + private int boardValue; private boolean turnTaken = false; private int winningX1,winningY1,winningX2,winningY2; public Game(){ playfield = new int[9]; + boardValue = 0; } public void place(int position, int player){ @@ -22,22 +24,20 @@ public class Game { } public void computersTurn(){ - boolean isPlaced = false; - try { - Thread.sleep(750); - } catch (InterruptedException e) { - e.printStackTrace(); - } - while(!isPlaced){ - int random = (int) (Math.random() * 9); - // if field is free - if (playfield[random] == 0) { - place(random, -1); - isPlaced = true; + for (int i = 0; i < playfield.length; i++){ + if (playfield[i] == 0){ + if (minmax(playfield, 0, true) == 1){ + place(i, -1); + return; } + } } } + private int minmax(int[] board, int depth, boolean isMaximizing){ + return 1; + } + public boolean checkWin() { //only check if winning is possible if (emptyTiles() < 5) { @@ -47,6 +47,11 @@ public class Game { winningX1 = 75; winningX2 = 825; winningY1 = winningY2 = i * 300 + 150; + if (playfield[i] == 1){ + boardValue = 10; + } else { + boardValue = -10; + } return true; } //vertical @@ -54,6 +59,11 @@ public class Game { winningY1 = 75; winningY2 = 825; winningX1 = winningX2 = i * 300 + 150; + if (playfield[i * 3] == 1){ + boardValue = 10; + } else { + boardValue = -10; + } return true; } } @@ -61,13 +71,24 @@ public class Game { if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){ winningX2 = winningY1 = 75; winningX1 = winningY2 = 825; + if (playfield[2] == 1){ + boardValue = 10; + } else { + boardValue = -10; + } return true; } else if ((playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8])){ winningX1 = winningY1 = 75; winningX2 = winningY2 = 825; + if (playfield[0] == 1){ + boardValue = 10; + } else { + boardValue = -10; + } return true; } } + boardValue = 0; return false; } @@ -78,7 +99,6 @@ public class Game { n -= 1; } } - System.out.println(n); return n; } @@ -113,4 +133,12 @@ public class Game { public int getWinningY2() { return winningY2; } + + public int getBoardValue() { + return boardValue; + } + + public void setBoardValue(int boardValue) { + this.boardValue = boardValue; + } } diff --git a/out/production/TicTacToe - MinMax/Board$1.class b/out/production/TicTacToe - MinMax/Board$1.class index 8e4ef4e..3f4d6d1 100644 Binary files a/out/production/TicTacToe - MinMax/Board$1.class and b/out/production/TicTacToe - MinMax/Board$1.class differ diff --git a/out/production/TicTacToe - MinMax/Board$2.class b/out/production/TicTacToe - MinMax/Board$2.class index 866bc55..53920d2 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/Board.class b/out/production/TicTacToe - MinMax/Board.class index 57442c0..ab78c09 100644 Binary files a/out/production/TicTacToe - MinMax/Board.class and b/out/production/TicTacToe - MinMax/Board.class differ diff --git a/out/production/TicTacToe - MinMax/Game.class b/out/production/TicTacToe - MinMax/Game.class index 2ffd5c6..46d2520 100644 Binary files a/out/production/TicTacToe - MinMax/Game.class and b/out/production/TicTacToe - MinMax/Game.class differ diff --git a/out/production/TicTacToe - MinMax/Minmax.class b/out/production/TicTacToe - MinMax/Minmax.class deleted file mode 100644 index d6d4981..0000000 Binary files a/out/production/TicTacToe - MinMax/Minmax.class and /dev/null differ diff --git a/out/production/TicTacToe - MinMax/Test.class b/out/production/TicTacToe - MinMax/Test.class deleted file mode 100644 index 22a1c24..0000000 Binary files a/out/production/TicTacToe - MinMax/Test.class and /dev/null differ