changing branches
This commit is contained in:
@@ -104,7 +104,7 @@ public class Board extends JPanel implements ActionListener {
|
|||||||
public void run() {
|
public void run() {
|
||||||
//check if game state evaluation needs to be done
|
//check if game state evaluation needs to be done
|
||||||
if (isChanged(oldPlayfield)) {
|
if (isChanged(oldPlayfield)) {
|
||||||
gameWon = game.checkWin();
|
gameWon = game.checkWin(game.getPlayfield());
|
||||||
//repaint board if not won
|
//repaint board if not won
|
||||||
if (!gameWon) {
|
if (!gameWon) {
|
||||||
repaint();
|
repaint();
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
@@ -24,71 +25,20 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void computersTurn(){
|
public void computersTurn(){
|
||||||
place(findBestMove(playfield), -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int minmax(int[] board, int depth, boolean isMaximizing){
|
public boolean checkWin(int[] board) {
|
||||||
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() {
|
|
||||||
//only check if winning is possible
|
//only check if winning is possible
|
||||||
if (emptyTiles() < 5) {
|
if (emptyTiles() < 5) {
|
||||||
|
System.out.println(boardValue);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
//horizontal
|
//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;
|
winningX1 = 75;
|
||||||
winningX2 = 825;
|
winningX2 = 825;
|
||||||
winningY1 = winningY2 = i * 300 + 150;
|
winningY1 = winningY2 = i * 300 + 150;
|
||||||
if (playfield[i] == 1){
|
if (board[i] == 1){
|
||||||
boardValue = 10;
|
boardValue = 10;
|
||||||
} else {
|
} else {
|
||||||
boardValue = -10;
|
boardValue = -10;
|
||||||
@@ -96,11 +46,11 @@ public class Game {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//vertical
|
//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;
|
winningY1 = 75;
|
||||||
winningY2 = 825;
|
winningY2 = 825;
|
||||||
winningX1 = winningX2 = i * 300 + 150;
|
winningX1 = winningX2 = i * 300 + 150;
|
||||||
if (playfield[i * 3] == 1){
|
if (board[i * 3] == 1){
|
||||||
boardValue = 10;
|
boardValue = 10;
|
||||||
} else {
|
} else {
|
||||||
boardValue = -10;
|
boardValue = -10;
|
||||||
@@ -109,19 +59,19 @@ public class Game {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//diagonal
|
//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;
|
winningX2 = winningY1 = 75;
|
||||||
winningX1 = winningY2 = 825;
|
winningX1 = winningY2 = 825;
|
||||||
if (playfield[2] == 1){
|
if (board[2] == 1){
|
||||||
boardValue = 10;
|
boardValue = 10;
|
||||||
} else {
|
} else {
|
||||||
boardValue = -10;
|
boardValue = -10;
|
||||||
}
|
}
|
||||||
return true;
|
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;
|
winningX1 = winningY1 = 75;
|
||||||
winningX2 = winningY2 = 825;
|
winningX2 = winningY2 = 825;
|
||||||
if (playfield[0] == 1){
|
if (board[0] == 1){
|
||||||
boardValue = 10;
|
boardValue = 10;
|
||||||
} else {
|
} else {
|
||||||
boardValue = -10;
|
boardValue = -10;
|
||||||
@@ -175,8 +125,8 @@ public class Game {
|
|||||||
return winningY2;
|
return winningY2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBoardValue() {
|
public int getBoardValue(int[] board) {
|
||||||
checkWin();
|
checkWin(board);
|
||||||
return boardValue;
|
return boardValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user