Get AI working

main
Elnu 2 years ago
parent eeaf71bc19
commit b4c9ca0105

@ -1,6 +1,9 @@
import java.awt.*; import java.awt.*;
import java.awt.image.ImageObserver; import java.awt.image.ImageObserver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Board { public class Board {
// Width and height of each board tile // Width and height of each board tile
@ -25,11 +28,12 @@ public class Board {
// The current board coordinate that's being dragged // The current board coordinate that's being dragged
BoardCoordinate dragging = null; BoardCoordinate dragging = null;
ArrayList<BoardCoordinate> legalMoves = null; ArrayList<BoardCoordinate> legalMoves = null;
Move lastMove; Stack<Move> moveHistory;
Piece captured;
public boolean isGameOver; public boolean isGameOver;
public Board() { public Board() {
moveHistory = new Stack<>();
// Initialize DrawingPanel // Initialize DrawingPanel
panel = new DrawingPanel(DIMENSION, DIMENSION); panel = new DrawingPanel(DIMENSION, DIMENSION);
graphics = panel.getGraphics(); graphics = panel.getGraphics();
@ -103,8 +107,7 @@ public class Board {
} }
public void move(int fromX, int fromY, int toX, int toY) { public void move(int fromX, int fromY, int toX, int toY) {
lastMove = new Move(new BoardCoordinate(fromX, fromY), new BoardCoordinate(toX, toY)); moveHistory.add(new Move(new BoardCoordinate(fromX, fromY), new BoardCoordinate(toX, toY), get(toX, toY)));
captured = get(toX, toY);
set(toX, toY, get(fromX, fromY)); set(toX, toY, get(fromX, fromY));
set(fromX, fromY, null); set(fromX, fromY, null);
} }
@ -114,15 +117,15 @@ public class Board {
} }
public void move(Move move) { public void move(Move move) {
if (move == null) return;
move(move.from, move.to); move(move.from, move.to);
draw(new ScreenCoordinate(0, 0));
} }
public void undoMove() { public void undoMove() {
if (lastMove == null) return; if (moveHistory.isEmpty()) return;
Move lastMove = moveHistory.pop();
set(lastMove.from, get(lastMove.to)); set(lastMove.from, get(lastMove.to));
set(lastMove.to, captured); set(lastMove.to, lastMove.captured);
lastMove = null;
} }
// Mouse down event handler // Mouse down event handler
@ -147,9 +150,6 @@ public class Board {
if (dragging != null && !newCoordinate.equals(dragging)) { if (dragging != null && !newCoordinate.equals(dragging)) {
// dragging is BoardCoordinate of piece being dragged // dragging is BoardCoordinate of piece being dragged
Piece piece = get(dragging); Piece piece = get(dragging);
move(dragging, newCoordinate);
move(ChessAI.findBestMove(this));
/*
ArrayList<BoardCoordinate> legalMoves = piece.getLegalMoves(dragging, this); ArrayList<BoardCoordinate> legalMoves = piece.getLegalMoves(dragging, this);
for (BoardCoordinate legalMove : legalMoves) { for (BoardCoordinate legalMove : legalMoves) {
if (newCoordinate.equals(legalMove)) { if (newCoordinate.equals(legalMove)) {
@ -174,7 +174,6 @@ public class Board {
break; break;
} }
} }
*/
} }
// Clear dragging // Clear dragging
dragging = null; dragging = null;

@ -1,9 +1,16 @@
public class Move { public class Move {
public BoardCoordinate from; public BoardCoordinate from;
public BoardCoordinate to; public BoardCoordinate to;
public Piece captured;
public Move(BoardCoordinate from, BoardCoordinate to) { public Move(BoardCoordinate from, BoardCoordinate to) {
this.from = from; this.from = from;
this.to = to; this.to = to;
} }
public Move(BoardCoordinate from, BoardCoordinate to, Piece captured) {
this.from = from;
this.to = to;
this.captured = captured;
}
} }