From b4c9ca0105a1d120686605c72a33c45a228fd937 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Tue, 14 Mar 2023 12:40:49 -0700 Subject: [PATCH] Get AI working --- src/Board.java | 23 +++++++++++------------ src/Move.java | 7 +++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Board.java b/src/Board.java index 13ef3a5..d85a837 100644 --- a/src/Board.java +++ b/src/Board.java @@ -1,6 +1,9 @@ import java.awt.*; import java.awt.image.ImageObserver; import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; public class Board { // Width and height of each board tile @@ -25,11 +28,12 @@ public class Board { // The current board coordinate that's being dragged BoardCoordinate dragging = null; ArrayList legalMoves = null; - Move lastMove; - Piece captured; + Stack moveHistory; public boolean isGameOver; public Board() { + moveHistory = new Stack<>(); + // Initialize DrawingPanel panel = new DrawingPanel(DIMENSION, DIMENSION); graphics = panel.getGraphics(); @@ -103,8 +107,7 @@ public class Board { } public void move(int fromX, int fromY, int toX, int toY) { - lastMove = new Move(new BoardCoordinate(fromX, fromY), new BoardCoordinate(toX, toY)); - captured = get(toX, toY); + moveHistory.add(new Move(new BoardCoordinate(fromX, fromY), new BoardCoordinate(toX, toY), get(toX, toY))); set(toX, toY, get(fromX, fromY)); set(fromX, fromY, null); } @@ -114,15 +117,15 @@ public class Board { } public void move(Move move) { + if (move == null) return; move(move.from, move.to); - draw(new ScreenCoordinate(0, 0)); } public void undoMove() { - if (lastMove == null) return; + if (moveHistory.isEmpty()) return; + Move lastMove = moveHistory.pop(); set(lastMove.from, get(lastMove.to)); - set(lastMove.to, captured); - lastMove = null; + set(lastMove.to, lastMove.captured); } // Mouse down event handler @@ -147,9 +150,6 @@ public class Board { if (dragging != null && !newCoordinate.equals(dragging)) { // dragging is BoardCoordinate of piece being dragged Piece piece = get(dragging); - move(dragging, newCoordinate); - move(ChessAI.findBestMove(this)); - /* ArrayList legalMoves = piece.getLegalMoves(dragging, this); for (BoardCoordinate legalMove : legalMoves) { if (newCoordinate.equals(legalMove)) { @@ -174,7 +174,6 @@ public class Board { break; } } - */ } // Clear dragging dragging = null; diff --git a/src/Move.java b/src/Move.java index 48a3889..a8f9c59 100644 --- a/src/Move.java +++ b/src/Move.java @@ -1,9 +1,16 @@ public class Move { public BoardCoordinate from; public BoardCoordinate to; + public Piece captured; public Move(BoardCoordinate from, BoardCoordinate to) { this.from = from; this.to = to; } + + public Move(BoardCoordinate from, BoardCoordinate to, Piece captured) { + this.from = from; + this.to = to; + this.captured = captured; + } }