remove not used library
This commit is contained in:
parent
065b410ef3
commit
8e65b3af2e
|
|
@ -4,7 +4,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
private final int MAX_DEPTH = 3; // Depth of search
|
private final int MAX_DEPTH = 4; // Depth of search
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -13,6 +13,11 @@ public class AutoPlayer {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Move computeBestMove(Board board) {
|
public Move computeBestMove(Board board) {
|
||||||
|
|
||||||
|
if (isCheckmate(board)) {
|
||||||
|
return null; // no move if checkmate
|
||||||
|
}
|
||||||
|
|
||||||
ArrayList<Move> possibleMoves = generateAllPossibleMoves(board);
|
ArrayList<Move> possibleMoves = generateAllPossibleMoves(board);
|
||||||
|
|
||||||
if (possibleMoves.isEmpty()) {
|
if (possibleMoves.isEmpty()) {
|
||||||
|
|
@ -40,15 +45,54 @@ public class AutoPlayer {
|
||||||
return bestMove;
|
return bestMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vérifie si la position actuelle est un échec et mat
|
||||||
|
* @param board
|
||||||
|
* @return true si c'est échec et mat, false sinon
|
||||||
|
*/
|
||||||
|
private boolean isCheckmate(Board board) {
|
||||||
|
boolean isWhiteTurn = board.isTurnWhite();
|
||||||
|
|
||||||
|
// no move + check = checkmate
|
||||||
|
ArrayList<Move> possibleMoves = generateAllPossibleMoves(board);
|
||||||
|
|
||||||
|
if (possibleMoves.isEmpty()) {
|
||||||
|
|
||||||
|
Piece king = null;
|
||||||
|
for (Piece piece : board.getPieces()) {
|
||||||
|
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
|
||||||
|
king = piece;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (king != null) {
|
||||||
|
|
||||||
|
for (Piece piece : board.getPieces()) {
|
||||||
|
if (piece.isWhite() != isWhiteTurn) {
|
||||||
|
ArrayList<int[]> enemyMoves = computeLegalMoves(board, piece);
|
||||||
|
for (int[] move : enemyMoves) {
|
||||||
|
if (move[0] == king.getX() && move[1] == king.getY()) {
|
||||||
|
return true; // Le roi est en échec et aucun mouvement possible -> échec et mat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private int negamax(Board board, int depth, int alpha, int beta) {
|
private int negamax(Board board, int depth, int alpha, int beta) {
|
||||||
// If we've reached the maximum depth or the game is over, evaluate the board
|
|
||||||
if (depth == 0) {
|
if (depth == 0 || isCheckmate(board)) {
|
||||||
return evaluateBoard(board);
|
return evaluateBoard(board);
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<Move> possibleMoves = generateAllPossibleMoves(board);
|
ArrayList<Move> possibleMoves = generateAllPossibleMoves(board);
|
||||||
|
|
||||||
// If no moves are available, this might be checkmate or stalemate
|
|
||||||
if (possibleMoves.isEmpty()) {
|
if (possibleMoves.isEmpty()) {
|
||||||
return -1000; // Heavily penalize positions with no moves
|
return -1000; // Heavily penalize positions with no moves
|
||||||
}
|
}
|
||||||
|
|
@ -239,6 +283,11 @@ public class AutoPlayer {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
boolean isWhiteTurn = board.isTurnWhite();
|
boolean isWhiteTurn = board.isTurnWhite();
|
||||||
|
|
||||||
|
|
||||||
|
if (isCheckmate(board)) {
|
||||||
|
return isWhiteTurn ? -20000 : 20000; // bonus checkmate
|
||||||
|
}
|
||||||
|
|
||||||
// Material value
|
// Material value
|
||||||
for (Piece piece : board.getPieces()) {
|
for (Piece piece : board.getPieces()) {
|
||||||
int pieceValue = getPieceValue(piece.getType());
|
int pieceValue = getPieceValue(piece.getType());
|
||||||
|
|
@ -264,8 +313,8 @@ public class AutoPlayer {
|
||||||
private int getPieceValue(PieceType type) {
|
private int getPieceValue(PieceType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Pawn: return 100;
|
case Pawn: return 100;
|
||||||
case Knight: return 320;
|
case Knight: return 300;
|
||||||
case Bishop: return 330;
|
case Bishop: return 300;
|
||||||
case Rook: return 500;
|
case Rook: return 500;
|
||||||
case Queen: return 900;
|
case Queen: return 900;
|
||||||
case King: return 20000; // Very high value to prioritize king safety
|
case King: return 20000; // Very high value to prioritize king safety
|
||||||
|
|
@ -323,7 +372,7 @@ public class AutoPlayer {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case King:
|
case King:
|
||||||
// Kings are safer at the edge in the middlegame
|
// Kings are safer at the edge
|
||||||
if ((x <= 1 || x >= 6) && y <= 1) {
|
if ((x <= 1 || x >= 6) && y <= 1) {
|
||||||
bonus += 30;
|
bonus += 30;
|
||||||
}
|
}
|
||||||
|
|
@ -333,4 +382,4 @@ public class AutoPlayer {
|
||||||
// Adjust sign based on whose piece it is
|
// Adjust sign based on whose piece it is
|
||||||
return piece.isWhite() == isWhiteTurn ? bonus : -bonus;
|
return piece.isWhite() == isWhiteTurn ? bonus : -bonus;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -224,11 +224,11 @@ public class Board {
|
||||||
|
|
||||||
// Check for castling
|
// Check for castling
|
||||||
if (selectedX == 4 && selectedY == 7) {
|
if (selectedX == 4 && selectedY == 7) {
|
||||||
if (x == 6 && y == 7) { // King-side castling
|
if (x == 6 && y == 7) { // small castling
|
||||||
// Move the rook too
|
// Move the rook too
|
||||||
removePieceAt(7, 7);
|
removePieceAt(7, 7);
|
||||||
pieces.add(new Piece(5, 7, true, PieceType.Rook));
|
pieces.add(new Piece(5, 7, true, PieceType.Rook));
|
||||||
} else if (x == 2 && y == 7) { // Queen-side castling
|
} else if (x == 2 && y == 7) { // big castling
|
||||||
// Move the rook too
|
// Move the rook too
|
||||||
removePieceAt(0, 7);
|
removePieceAt(0, 7);
|
||||||
pieces.add(new Piece(3, 7, true, PieceType.Rook));
|
pieces.add(new Piece(3, 7, true, PieceType.Rook));
|
||||||
|
|
@ -239,11 +239,11 @@ public class Board {
|
||||||
|
|
||||||
// Check for castling
|
// Check for castling
|
||||||
if (selectedX == 4 && selectedY == 0) {
|
if (selectedX == 4 && selectedY == 0) {
|
||||||
if (x == 6 && y == 0) { // King-side castling
|
if (x == 6 && y == 0) { // small castling
|
||||||
// Move the rook too
|
// Move the rook too
|
||||||
removePieceAt(7, 0);
|
removePieceAt(7, 0);
|
||||||
pieces.add(new Piece(5, 0, false, PieceType.Rook));
|
pieces.add(new Piece(5, 0, false, PieceType.Rook));
|
||||||
} else if (x == 2 && y == 0) { // Queen-side castling
|
} else if (x == 2 && y == 0) { // big castling
|
||||||
// Move the rook too
|
// Move the rook too
|
||||||
removePieceAt(0, 0);
|
removePieceAt(0, 0);
|
||||||
pieces.add(new Piece(3, 0, false, PieceType.Rook));
|
pieces.add(new Piece(3, 0, false, PieceType.Rook));
|
||||||
|
|
@ -631,12 +631,11 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Castling
|
// Castling
|
||||||
if (isWhite && !whiteKingMoved && x == 4 && y == 7) {
|
if (isWhite && !whiteKingMoved) {
|
||||||
// King-side castling
|
// small castling
|
||||||
if (!whiteRookKingSideMoved &&
|
if (!whiteRookKingSideMoved &&
|
||||||
getPieceAt(5, 7) == null &&
|
getPieceAt(5, 7) == null &&
|
||||||
getPieceAt(6, 7) == null &&
|
getPieceAt(6, 7) == null &&
|
||||||
getPieceAt(7, 7) != null &&
|
|
||||||
getPieceAt(7, 7).getType() == PieceType.Rook) {
|
getPieceAt(7, 7).getType() == PieceType.Rook) {
|
||||||
|
|
||||||
// Check if king would pass through check
|
// Check if king would pass through check
|
||||||
|
|
@ -647,7 +646,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queen-side castling
|
// big castling
|
||||||
if (!whiteRookQueenSideMoved &&
|
if (!whiteRookQueenSideMoved &&
|
||||||
getPieceAt(1, 7) == null &&
|
getPieceAt(1, 7) == null &&
|
||||||
getPieceAt(2, 7) == null &&
|
getPieceAt(2, 7) == null &&
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,10 @@
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Dimension;
|
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.SwingConstants;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.event.ChangeEvent;
|
|
||||||
import javax.swing.event.ChangeListener;
|
|
||||||
|
|
||||||
import backend.Game;
|
import backend.Game;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
|
@ -24,10 +19,6 @@ import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import javax.swing.JList;
|
|
||||||
import javax.swing.AbstractListModel;
|
|
||||||
import javax.swing.JToggleButton;
|
|
||||||
import javax.swing.JRadioButton;
|
|
||||||
import javax.swing.JCheckBox;
|
import javax.swing.JCheckBox;
|
||||||
|
|
||||||
public class MyInterface extends JFrame {
|
public class MyInterface extends JFrame {
|
||||||
|
|
@ -36,7 +27,6 @@ public class MyInterface extends JFrame {
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private JLabel turnLabel;
|
private JLabel turnLabel;
|
||||||
private JLabel borderLabel;
|
private JLabel borderLabel;
|
||||||
private JLabel speedLabel;
|
|
||||||
private JPanelChessBoard panelDraw;
|
private JPanelChessBoard panelDraw;
|
||||||
private Game game;
|
private Game game;
|
||||||
private JLabel actionLabel;
|
private JLabel actionLabel;
|
||||||
|
|
@ -199,7 +189,6 @@ public class MyInterface extends JFrame {
|
||||||
try {
|
try {
|
||||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||||
String line = fileContent.readLine();
|
String line = fileContent.readLine();
|
||||||
int colorID = 0;
|
|
||||||
while (line != null) {
|
while (line != null) {
|
||||||
lines.add(line);
|
lines.add(line);
|
||||||
line = fileContent.readLine();
|
line = fileContent.readLine();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue