I have posted my code. I am unable to understand where the root cause of the issue is present. This is the UI code of the java chess Engine I've been trying to make. The application runs for about 4-5 moves and 8-9 moves if I am lucky or fast. Eventually, i run into a series of infinite errors as I move my mouse. I think somehow somewhere my code is creating multiple instances of mouseListeners which I don't think should happen. Please guide me on what to do.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class UserInterface extends JPanel implements MouseListener, MouseMotionListener {
static int mouseX, mouseY, newMouseX, newMouseY;
static int squareSize = 32;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(120, 100, 45));
this.addMouseListener(this);
this.addMouseMotionListener(this);
for (int i = 0; i < 64; i += 2) {
g.setColor(new Color(255, 200, 100));
g.fillRect((i % 8 + (i / 8) % 2) * squareSize, (i / 8) * squareSize, squareSize, squareSize);
g.setColor(new Color(150, 50, 30));
g.fillRect(((i + 1) % 8 - ((i + 1) / 8) % 2) * squareSize, ((i + 1) / 8) * squareSize, squareSize, squareSize);
}
Image chessPiecesImage;
chessPiecesImage = new ImageIcon("ChessPieces.png").getImage();
for (int i = 0; i < 64; i++) {
int j = -1, k = -1;
switch (alphaBetaChess.chessBoard[i / 8][i % 8]) {
case "P":
j = 5;
k = 0;
break;
case "p":
j = 5;
k = 1;
break;
case "R":
j = 2;
k = 0;
break;
case "r":
j = 2;
k = 1;
break;
case "K":
j = 4;
k = 0;
break;
case "k":
j = 4;
k = 1;
break;
case "B":
j = 3;
k = 0;
break;
case "b":
j = 3;
k = 1;
break;
case "Q":
j = 1;
k = 0;
break;
case "q":
j = 1;
k = 1;
break;
case "A":
j = 0;
k = 0;
break;
case "a":
j = 0;
k = 1;
break;
}
if (j != -1 && k != -1) {
g.drawImage(chessPiecesImage, (i % 8) * squareSize, (i / 8) * squareSize, (i % 8 + 1) * squareSize, (i / 8 + 1) * squareSize, j * 64, k * 64, (j + 1) * 64, (k + 1) * 64, this);
}
}
repaint();
// g.setColor(Color.black);
// g.fillRect(x, y,500/8,500/8);
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getX() < 8 * squareSize && e.getY() < 8 * squareSize) {
//if inside the board
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getX() < 8 * squareSize && e.getY() < 8 * squareSize) {
//if inside the board
newMouseX = e.getX();
newMouseY = e.getY();
if (e.getButton() == MouseEvent.BUTTON1) {
String dragMove;
if (newMouseY / squareSize == 0 && mouseY / squareSize == 1 && "P".equals(alphaBetaChess.chessBoard[mouseY / squareSize][mouseX / squareSize])) {
//pawn promotion
dragMove = "" + mouseX / squareSize + newMouseX / squareSize + alphaBetaChess.chessBoard[newMouseY / squareSize][newMouseX / squareSize] + "QP";
} else {
//regular move
dragMove = "" + mouseY / squareSize + mouseX / squareSize + newMouseY / squareSize + newMouseX / squareSize + alphaBetaChess.chessBoard[newMouseY / squareSize][newMouseX / squareSize];
}
String userPosibilities = alphaBetaChess.possibleMoves();
if (userPosibilities.replaceAll(dragMove, "").length() < userPosibilities.length()) {
//if valid move
alphaBetaChess.makeMove(dragMove);
alphaBetaChess.flipBoard();
alphaBetaChess.makeMove(alphaBetaChess.alphaBeta(alphaBetaChess.globalDepth, 1000000, -1000000, "", 0));
alphaBetaChess.flipBoard();
repaint();
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}