首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >没有引用就无法复制ArrayList

没有引用就无法复制ArrayList
EN

Stack Overflow用户
提问于 2021-05-26 16:15:38
回答 1查看 113关注 0票数 1

我正试着做一个基本的国际象棋人工智能。我有"Schach“类,它存储白色数字和黑色数字的ArrayLists。当轮到AI做一个动作时,我首先添加所有的动作,在接下来的步骤中,我想删除那些自我控制的动作。但出于某种原因,AI只会执行它的每一个可能的动作。我甚至没有更新主类中的列表,问题在于列表总是引用AI类中的列表。

也是,我已经尝试了克隆()方法,但是它没有工作。有什么建议吗?

代码语言:javascript
运行
复制
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)

}

编辑

有了这个设置,ai不应该执行移动操作,只需计算它们.

编辑2 resetLists函数是主要问题(我猜)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-26 17:49:16

好的,所以我自己修好了。这是我的解决方案,如果将来有人需要它的话:D

代码语言:javascript
运行
复制
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;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67708875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档