Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 奥赛罗-寻找可用选项的算法_Java_Algorithm_Swing_User Interface_Matrix - Fatal编程技术网

Java 奥赛罗-寻找可用选项的算法

Java 奥赛罗-寻找可用选项的算法,java,algorithm,swing,user-interface,matrix,Java,Algorithm,Swing,User Interface,Matrix,我正在尝试使用Java开发游戏《奥赛罗》,我正在努力寻找玩家可用的动作(而不是电脑) 比如我是玩家1,我在玩白色的棋子 检查按钮是否为空。(我将按钮用作平铺) 检查是否有颜色相反的邻居 如果有,继续检查每个方向是否有相反的颜色,直到 如果我们到达边界-返回false 如果我们达到了我们的颜色-把所有的碎片变成我的颜色 我正在努力实现3。五, 我如何迭代所有方向(如果我在电路板的内部,最多8个方向),如何进一步检查每个方向的颜色 我考虑为内板实现所有的8个方向,然后在外板实现所有的可能性,并检查边

我正在尝试使用Java开发游戏《奥赛罗》,我正在努力寻找玩家可用的动作(而不是电脑)

比如我是玩家1,我在玩白色的棋子

  • 检查按钮是否为空。(我将按钮用作平铺)
  • 检查是否有颜色相反的邻居
  • 如果有,继续检查每个方向是否有相反的颜色,直到
  • 如果我们到达边界-返回false
  • 如果我们达到了我们的颜色-把所有的碎片变成我的颜色
  • 我正在努力实现3。五,

    我如何迭代所有方向(如果我在电路板的内部,最多8个方向),如何进一步检查每个方向的颜色

    我考虑为内板实现所有的8个方向,然后在外板实现所有的可能性,并检查边缘选项,这是非常不有效的,我不想这样编码

    您不必查看代码,我正在尝试如何处理它(考虑2个for循环),但下面是函数和整个代码:(每个按钮都有一个图标-黑色/白色部分)

    private void checkLegalPlay(整数行,整数列){
    如果(playerNum==0){//Black player
    如果(行>0&&行<7&&列>0&&列<7){//内板-
    //不好,我想从棋盘的任何一点开始
    对于(int i=col-1;i!=0;i--)
    如果(正方形[行][i].getIcon()!=null){
    if(正方形[行][i].getIcon()==blackPiece){
    //前进到边界-返回false
    //如果有黑色碎片,请前进
    //如果我们到了白色部分,把所有的都变成白色
    //白色碎片
    }
    }
    }
    }
    }
    

    它已经有将近300行代码了,所以如果你真的想看看我到目前为止所做的事情,我宁愿给你一个链接:-删除-

    软件开发是一门抽象的艺术。你应该努力培养一种技能,去发现逻辑片段之间的相似之处,并将它们抽象出来。例如,要检查移动是否合法,您必须应用相同的检查逻辑从单元格的不同方向进行迭代。此外,检查移动和应用移动(翻转片段)共享相同的迭代逻辑。所以让我们把它抽象出来,也就是说,让我们把迭代和我们在迭代中所做的逻辑分开:

        private static final int SIZE = 8;
    
        static boolean isValidPos(int pos) {
            return pos >= 0 && pos < SIZE;
        }
    
    
        static class Point {
            public final int row;
            public final int col;
    
            public Point(int row, int col) {
                this.row = row;
                this.col = col;
            }
        }
    
    
        private static final Point[] ALL_DIRECTIONS = new Point[]{
                new Point(1, 0),
                new Point(1, 1),
                new Point(0, 1),
                new Point(-1, 1),
                new Point(-1, 0),
                new Point(-1, -1),
                new Point(0, -1),
                new Point(1, -1),
        };
    
    
        interface CellHandler {
            boolean handleCell(int row, int col, Icon icon);
        }
    
    
        void iterateCells(Point start, Point step, CellHandler handler) {
            for (int row = start.row + step.row, col = start.col + step.col;
                 isValidPos(row) && isValidPos(col);
                 row += step.row, col += step.col) {
                Icon icon = squares[row][col].getIcon();
                // empty cell
                if (icon == null)
                    break;
                // handler can stop iteration
                if (!handler.handleCell(row, col, icon))
                    break;
    
            }
        }
    
    
        static class CheckCellHandler implements CellHandler {
            private final Icon otherIcon;
            private boolean hasOtherPieces = false;
            private boolean endsWithMine = false;
    
            public CheckCellHandler(Icon otherIcon) {
                this.otherIcon = otherIcon;
            }
    
            @Override
            public boolean handleCell(int row, int column, Icon icon) {
                if (icon == otherIcon) {
                    hasOtherPieces = true;
                    return true;
                } else {
                    endsWithMine = true;
                    return false;
                }
            }
    
            public boolean isGoodMove() {
                return hasOtherPieces && endsWithMine;
            }
        }
    
        class FlipCellHandler implements CellHandler {
            private final Icon myIcon;
            private final Icon otherIcon;
            private final List<Point> currentFlipList = new ArrayList<Point>();
    
            public FlipCellHandler(Icon myIcon, Icon otherIcon) {
                this.myIcon = myIcon;
                this.otherIcon = otherIcon;
            }
    
            @Override
            public boolean handleCell(int row, int column, Icon icon) {
                if (icon == myIcon) {
                    // flip all cells
                    for (Point p : currentFlipList) {
                        squares[p.row][p.col].setIcon(myIcon);
                    }
                    return false;
                } else {
                    currentFlipList.add(new Point(row, column));
                    return true;
                }
            }
        }
    
    
        private boolean checkLegalPlay(int row, int col) {
            ImageIcon otherIcon = (playerNum == 0) ? whitePiece : blackPiece;
            Point start = new Point(row, col);
            for (Point step : ALL_DIRECTIONS) {
                // handler is stateful so create new for each direction
                CheckCellHandler checkCellHandler = new CheckCellHandler(otherIcon);
                iterateCells(start, step, checkCellHandler);
                if (checkCellHandler.isGoodMove())
                    return true;
            }
            return false;
        }
    
    private static final int SIZE=8;
    静态布尔值isValidPos(int-pos){
    返回位置>=0&&pos
    private static final int SIZE = 8; static boolean isValidPos(int pos) { return pos >= 0 && pos < SIZE; } static class Point { public final int row; public final int col; public Point(int row, int col) { this.row = row; this.col = col; } } private static final Point[] ALL_DIRECTIONS = new Point[]{ new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(-1, 1), new Point(-1, 0), new Point(-1, -1), new Point(0, -1), new Point(1, -1), }; interface CellHandler { boolean handleCell(int row, int col, Icon icon); } void iterateCells(Point start, Point step, CellHandler handler) { for (int row = start.row + step.row, col = start.col + step.col; isValidPos(row) && isValidPos(col); row += step.row, col += step.col) { Icon icon = squares[row][col].getIcon(); // empty cell if (icon == null) break; // handler can stop iteration if (!handler.handleCell(row, col, icon)) break; } } static class CheckCellHandler implements CellHandler { private final Icon otherIcon; private boolean hasOtherPieces = false; private boolean endsWithMine = false; public CheckCellHandler(Icon otherIcon) { this.otherIcon = otherIcon; } @Override public boolean handleCell(int row, int column, Icon icon) { if (icon == otherIcon) { hasOtherPieces = true; return true; } else { endsWithMine = true; return false; } } public boolean isGoodMove() { return hasOtherPieces && endsWithMine; } } class FlipCellHandler implements CellHandler { private final Icon myIcon; private final Icon otherIcon; private final List<Point> currentFlipList = new ArrayList<Point>(); public FlipCellHandler(Icon myIcon, Icon otherIcon) { this.myIcon = myIcon; this.otherIcon = otherIcon; } @Override public boolean handleCell(int row, int column, Icon icon) { if (icon == myIcon) { // flip all cells for (Point p : currentFlipList) { squares[p.row][p.col].setIcon(myIcon); } return false; } else { currentFlipList.add(new Point(row, column)); return true; } } } private boolean checkLegalPlay(int row, int col) { ImageIcon otherIcon = (playerNum == 0) ? whitePiece : blackPiece; Point start = new Point(row, col); for (Point step : ALL_DIRECTIONS) { // handler is stateful so create new for each direction CheckCellHandler checkCellHandler = new CheckCellHandler(otherIcon); iterateCells(start, step, checkCellHandler); if (checkCellHandler.isGoodMove()) return true; } return false; }