1

I am trying to make a basic chess AI. I have the class "Schach" that stores ArrayLists of the white Figures and the black Figures. When it's the AI's turn to make a move i first add all the moves and in teh next step I want to remove the ones that put itself in check. But for some reason The AI just executes every single possible move it has. I am not even updating the lists in the main class, the problem relies on the lists always having a reference to the ones in the AI class.


ALSO I already tried the clone() method and it didn't work. Any suggestions??

public class Ai {

    public static final Ai ai = new Ai();

    private static int maxY = 8;
    private static int maxX = 7;
    private static int minY = 1;
    private static int minX = 0;

    private static ArrayList<Entity> whiteFigures;
    private static ArrayList<Entity> blackFigures;
    private static ArrayList<Move> moves;

    public void processMoves() {

        moves = new ArrayList<Move>();

        resetLists();

        for (Entity e : blackFigures) {
            moves.addAll(calcMoves(e.getFigure(), blackFigures, whiteFigures));
        }

        System.out.println(moves.size());

        //removeCheckMoves();

        System.out.println(moves.size());

        executeMove(moves.get((int) (Math.random() * moves.size())));

        resetLists();

        Schach.turn = true;

    }

    private void removeCheckMoves() {

        Figure king = null;

        for (Entity e : blackFigures) {
            if (e.getFigure().type == Figure.king) {
                king = e.getFigure();
                break;
            }
        }

        ArrayList<Move> legalMoves = new ArrayList<Move>();
        for (Move m : moves) {
            resetLists();
            executeMove(m);
            if(!isLegal(king)) {
                legalMoves.add(m);
            }
        }
        
        moves = legalMoves;

    }
    
    private boolean isLegal(Figure king) {
        
        boolean check = false;
        for (Entity w : whiteFigures) {
            for (Move move : Utils.calcMoves(w.getFigure(), whiteFigures, blackFigures)) {
                if (Utils.numToPos(move.to).x == king.x && Utils.numToPos(move.to).y == king.y) {
                    check = true;
                    break;
                }
            }
            if(check) break;
        }
        return check;
        
    }

    private void executeMove(Move m) {
        for (Entity e : blackFigures) {
            if (e.getFigure().x == Utils.numToPos(m.from).x && e.getFigure().y == Utils.numToPos(m.from).y) {
                e.getFigure().x = Utils.numToPos(m.to).x;
                e.getFigure().y = Utils.numToPos(m.to).y;
                e.gotoSquare(Utils.posToNum(e.getFigure()) - 8);
                for (Entity w : whiteFigures) {
                    if (w.getFigure().x == e.getFigure().x && w.getFigure().y == e.getFigure().y) {
                        whiteFigures.remove(w);
                        break;
                    }
                }
                break;
            }
        }
    }

    private void resetLists() {
        
        whiteFigures = new ArrayList<Entity>();
        whiteFigures.clear();
        whiteFigures.addAll(Schach.whiteFigures);
        blackFigures = new ArrayList<Entity>();
        blackFigures.clear();
        blackFigures.addAll(Schach.blackFigures);

    }

//calcMoves function (works fine no reference here for sure)

}

EDIT

with this setup the ai shouldnt execute a move at all just calculate them...


EDIT 2 The resetLists function is the main issue (I guess)

4
  • Your lists are declared as class members. Do you know the consequences? Commented May 26, 2021 at 16:53
  • No... what do you mean? Commented May 26, 2021 at 17:18
  • @TimothyTruckle You mean that they are static? I know that and it's suppossed to be like that. But I don't see how that would prohibit the AI to do it's work. Basically I just want to copy the static list from Schach to the static list in AI without them being connected to one another. Commented May 26, 2021 at 17:23
  • 1
    "I know that and it's suppossed to be like that." -- Your knowledge is wrong. Especially for variables the static key word should be used only if you have a good reason to do so. Commented May 26, 2021 at 18:01

1 Answer 1

1

Alright so I fixed it myself. Here is my solution if someone will ever need it in the future :D

public static ArrayList<Entity> cloneList (ArrayList<Entity> array){
        ArrayList<Entity> arr = new ArrayList<Entity>();
        for(Entity e : array) {
            arr.add(cloneEntity(e));
        }
        return arr;
    }
    
    public static Entity cloneEntity(Entity e) {
        Entity entity = new Entity(e.getPosition(), [etc...]);
        return entity;
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.