auto player in progress
This commit is contained in:
parent
7509196100
commit
ca414277ec
|
|
@ -1,5 +1,9 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class AutoPlayer {
|
||||
|
||||
|
||||
|
|
@ -9,9 +13,29 @@ public class AutoPlayer {
|
|||
* @return
|
||||
*/
|
||||
public Move computeBestMove(Board board) {
|
||||
|
||||
return null;
|
||||
List<Move> allPossibleMoves = new ArrayList<>();
|
||||
boolean isWhiteTurn = board.isTurnWhite();
|
||||
|
||||
for (Piece piece : board.getPieces()) {
|
||||
if (piece.isWhite() == isWhiteTurn) {
|
||||
board.userTouch(piece.getX(), piece.getY()); // triggers highlighting
|
||||
|
||||
for (int newX = 0; newX < board.getWidth(); newX++) {
|
||||
for (int newY = 0; newY < board.getHeight(); newY++) {
|
||||
if (board.isHighlighted(newX, newY)) {
|
||||
allPossibleMoves.add(new Move(piece.getX(), piece.getY(), newX, newY, piece));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allPossibleMoves.isEmpty()) return null;
|
||||
|
||||
Random rand = new Random();
|
||||
return allPossibleMoves.get(rand.nextInt(allPossibleMoves.size()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -308,10 +308,10 @@ public class Board {
|
|||
|
||||
/* The following methods require more work ! */
|
||||
|
||||
private void highlightedPossibleMoves(Piece piece) {
|
||||
/*private void highlightedPossibleMoves(Piece piece) {
|
||||
highlightedSquares.clear();
|
||||
highlightedSquares.addAll(MoveHighlighter.getPossibleMoves(piece, this));
|
||||
}
|
||||
}*/
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
for(int i=0; i<highlightedSquares.size();i++) { //loops through list of highlighted squares
|
||||
|
|
@ -393,15 +393,80 @@ return false; }
|
|||
}*/
|
||||
|
||||
public void playMove(Move move) {
|
||||
//TODO
|
||||
if (move == null || move.getPieceMoved() == null) return;
|
||||
int fromX = move.getOldX();
|
||||
int fromY = move.getOldY();
|
||||
int toX = move.getNewX();
|
||||
int toY = move.getNewY();
|
||||
|
||||
}
|
||||
/** Expose the raw 2D array for testing or drawing. */
|
||||
public Piece[][] getBoardArray() {
|
||||
return board;
|
||||
Piece piece = board[fromX][fromY];
|
||||
if (piece == null) return;
|
||||
|
||||
// Capture any piece at destination
|
||||
board[toX][toY] = null;
|
||||
|
||||
// Move piece
|
||||
board[fromX][fromY] = null;
|
||||
board[toX][toY] = piece;
|
||||
piece.setPosition(toX, toY); // <-- make sure Piece has this method
|
||||
|
||||
// Update turn
|
||||
isTurnWhite = !isTurnWhite;
|
||||
turnNumber++;
|
||||
|
||||
// Clear selection/highlight
|
||||
chosenPiece = null;
|
||||
highlightedSquares.clear();
|
||||
}
|
||||
|
||||
|
||||
/** Expose the raw 2D array for testing or drawing. */
|
||||
public Piece[][] getBoardArray() {
|
||||
return board;
|
||||
|
||||
}
|
||||
|
||||
public void highlightedPossibleMoves(Piece piece) {
|
||||
highlightedSquares.clear();
|
||||
MovePiece move = new MovePiece(piece, this);
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
|
||||
for (int newX = 0; newX < width; newX++) {
|
||||
for (int newY = 0; newY < height; newY++) {
|
||||
boolean isValid = false;
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
isValid = move.movePawnSimulate(newX, newY);
|
||||
break;
|
||||
case Rook:
|
||||
isValid = move.moveRookSimulate(newX, newY);
|
||||
break;
|
||||
case Knight:
|
||||
isValid = move.moveKnightSimulate(newX, newY);
|
||||
break;
|
||||
case Bishop:
|
||||
isValid = move.moveBishopSimulate(newX, newY);
|
||||
break;
|
||||
case Queen:
|
||||
isValid = move.moveQueenSimulate(newX, newY);
|
||||
break;
|
||||
case King:
|
||||
isValid = move.moveKingSimulate(newX, newY);
|
||||
break;
|
||||
}
|
||||
|
||||
if (isValid) {
|
||||
highlightedSquares.add(new int[]{newX, newY});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@ public class Game extends Thread {
|
|||
|
||||
private void aiPlayerTurn() {
|
||||
if(isAITurn()) {
|
||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
||||
board.playMove(aiPlayer.computeBestMove(board));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -114,6 +114,13 @@ public class Game extends Thread {
|
|||
public Board getBoard() {
|
||||
return board; // assuming your Game keeps its Board in a field named “board”
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -226,24 +226,26 @@ public class MovePiece {
|
|||
|
||||
//Pawn movement logic simulation
|
||||
|
||||
public boolean movePawnSimulate(int x, int y) {
|
||||
int currentX = piece.getX();
|
||||
int currentY = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
int direction = isWhite ? -1 : 1;
|
||||
boolean firstMove = isWhite ? currentY == 6 : currentY == 1;
|
||||
public boolean movePawnSimulate(int x, int y) {
|
||||
int currentX = piece.getX();
|
||||
int currentY = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
int direction = isWhite ? -1 : 1;
|
||||
boolean firstMove = isWhite ? currentY == 6 : currentY == 1;
|
||||
|
||||
// Same logic as movePawn but no actual move
|
||||
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) {
|
||||
return true;
|
||||
}else if (x == currentX && y == currentY + (2 * direction) && firstMove &&
|
||||
board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) {
|
||||
return true;
|
||||
}else if (Math.abs(x - currentX) == 1 && y == currentY + direction &&
|
||||
board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
|
||||
return true;
|
||||
}return false;
|
||||
}
|
||||
|
||||
|
||||
// Same logic as movePawn but no actual move
|
||||
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) {
|
||||
return true;
|
||||
}else if (x == currentX && y == currentY + (2 * direction) && firstMove &&
|
||||
board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) {
|
||||
return true;
|
||||
}else if (Math.abs(x - currentX) == 1 && y == currentY + direction &&
|
||||
board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
|
||||
return true;
|
||||
}return false;
|
||||
}
|
||||
|
||||
//bishop movement simulate
|
||||
public boolean moveBishopSimulate(int x, int y) {
|
||||
|
|
|
|||
|
|
@ -50,4 +50,9 @@ public class Piece {
|
|||
didMove=value;
|
||||
|
||||
}
|
||||
public void setPosition(int x, int y) {
|
||||
this.x_coor = x;
|
||||
this.y_coor = y;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue