Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我必须使用模拟智能来实现connect4。它已经可以工作,但是非常慢。我们必须得到两个玩家的威胁。威胁是一行中有3个硬币,在行之间或两端有一个空字段。我只想在每次移动后仅计算新威胁,并删除不再威胁的威胁。我写了一些方法来检查每个方向,如果最后一步有新的威胁。

private Field row(int row, int column, String Player) {
        Field field = null;
        if(column - 1 >= 0 && column + 1 < board.getColumn() && board.getValue(row, column-1).equals(Player)
                && board.getValue(row, column+1).equals(Player)) {
            if(column -2 >= 0 &&board.getValue(row, column-2).equals(" ")) {
                return field = new Field(row, column -2);
            }
            else if(column + 2 < board.getColumn()&& board.getValue(row, column+2).equals(" ")) {
                return field = new Field(row, column +2);
            }
        }

        else if(column - 2 >= 0  && board.getValue(row, column-1).equals(Player)
                && board.getValue(row, column-2).equals(Player)) {
            if(column -3 >= 0 &&board.getValue(row, column-3).equals(" ")) {
                return field =  new Field(row, column -3);
            }
            else if(column +1 < board.getColumn()&& board.getValue(row, column+1).equals(" ")) {
                return field =  new Field(row, column +1);
            }
        }
        else if(column +2 < board.getColumn()  && board.getValue(row, column+1).equals(Player)
                && board.getValue(row, column+2).equals(Player)) {
            if(column -1 >= 0 &&board.getValue(row, column-1).equals(" ")) {
                return field =  new Field(row, column -1);
            }
            else if(column +3 < board.getColumn()&& board.getValue(row, column+3).equals(" ")) {
                return field =  new Field(row, column +3);
            }
        }
        else if(column - 2 >= 0  && column + 1 < board.getColumn() && board.getValue(row, column-2).equals(Player)
                && board.getValue(row, column+1).equals(Player)) {
            if(column -1 >= 0 &&board.getValue(row, column-1).equals(" ")) {
                return field = new Field(row, column -1);
            }

        }
        else if(column - 1 >= 0  && column +2 < board.getColumn() && board.getValue(row, column-1).equals(Player)
                && board.getValue(row, column+2).equals(Player)) {
            if(column +1 < board.getColumn() &&board.getValue(row, column+1).equals(" ")) {
                return field = new Field(row, column +1);
            }

        }

        else if(column - 3  >= 0 && board.getValue(row, column-2).equals(Player) && board.getValue(row, column-3).equals(Player)) {
            if(board.getValue(row, column-1).equals(" ")) {
                return new Field (row, column -1);
            }
        }
        else if(column-3 >= 0 && board.getValue(row, column-1).equals(Player) && board.getValue(row, column-3).equals(Player)) {
            if(board.getValue(row, column-2).equals(" ")) {
                return new Field(row, column - 2);
            }
        }
        else if(column + 3 < board.getColumn() && board.getValue(row, column+2).equals(Player) && board.getValue(row, column+3).equals(Player)) {
            if(board.getValue(row, column+1).equals(" ")) {
                return new Field(row, column + 1);
            }
        }
        else if(column + 3 < board.getColumn() && board.getValue(row, column+1).equals(Player) && board.getValue(row, column+3).equals(Player)) {
            if(board.getValue(row, column+2).equals(" ")) {
                return new Field(row, column +2);
            }
        }
        return field;
    }

private Field column(int row, int column, String Player) {
        Field field = null;
        if (row + 2 < board.getRow()
                && board.getValue(row + 1, column).equals(Player)
                && board.getValue(row + 2, column).equals(Player)) {
            if(row-1 >= 0 && board.getValue(row-1, column).equals(" ")) {
                return field =  new Field(row-1, column);
            }

        }
        return field;
    }

private Field diagonalRight(int row, int column, String Player) {
        Field field = null;
        if(row - 1 >= 0 && column - 1 >= 0 && row + 1 < board.getRow() && column + 1 < board.getColumn()
                && board.getValue(row+ 1, column-1).equals(Player)&& board.getValue(row-1, column+1).equals(Player)) {
            if(row + 2 < board.getRow() && column - 2 >= 0 && board.getValue(row+2, column-2).equals(" ")) {
                return field = new Field(row+2, column - 2);
            }
            else if (row - 2 >=0 && column + 2 < board.getColumn() && board.getValue(row-2, column+2).equals(" ")) {
                return field = new Field(row-2, column + 2);
            }
        }
        else if(row + 2 < board.getRow() && column - 2 >= 0 && board.getValue(row+ 1, column-1).equals(Player)&& board.getValue(row+2, column-2).equals(Player)) {
            if(row +3 < board.getRow() && column - 3 >= 0 && board.getValue(row+3, column-3).equals(" ")) {
                return field = new Field(row+3, column - 3);
            }
            else if (row - 1 >=0 && column + 1 < board.getColumn() && board.getValue(row-1, column+1).equals(" ")) {
                return field = new Field(row-1, column + 1);
            }
        }
        else if(row - 2 >= 0 && column +2 < board.getColumn() && board.getValue(row- 1, column+1).equals(Player)&& board.getValue(row-2, column+2).equals(Player)) {
            if(row -3>= 0 && column + 3 < board.getColumn() && board.getValue(row-3, column+3).equals(" ")) {
                return field = new Field(row-3, column +3);
            }
            else if (row + 1 < board.getRow() && column -1 >=0 && board.getValue(row+1, column-1).equals(" ")) {
            return field = new Field(row+1, column -1);
            }
        }

        else if(row - 2 >= 0 && column - 1 >= 0 && row + 1 < board.getRow() && column + 2 < board.getColumn()
                && board.getValue(row+ 1, column-1).equals(Player)&& board.getValue(row-2, column+2).equals(Player)) {


             if (row - 1 >=0 && column + 1 < board.getColumn() && board.getValue(row-1, column+1).equals(" ")) {
                return field = new Field(row-1, column + 1);
            }
        }
        else if(row + 2 < board.getRow() && column -2 >= 0 && row - 1 >= 0 && column + 1 < board.getColumn()
                && board.getValue(row+ 2, column-2).equals(Player)&& board.getValue(row-1, column+1).equals(Player)) {


             if (row + 1 < board.getRow() && column - 1 >= 0 && board.getValue(row+1, column-1).equals(" ")) {
                return field = new Field(row+1, column - 1);
            }
        }

        else if(row + 3 < board.getRow() && column  + 3 < board.getColumn() && board.getValue(row + 2, column+2).equals(Player) && board.getValue(row+3, column+3).equals(Player)) {
            if( board.getValue(row+1, column+1).equals(" ")) {
                return new Field(row+1, column +1);
            }

        }
        else if(row + 3 < board.getRow() && column + 3 < board.getColumn() && board.getValue(row+3, column+3).equals(Player) && board.getValue(row+1, column+1).equals(Player)) {
            if(board.getValue(row+2, column+2).equals(" ")) {
                return new Field (row +2, column + 2);
            }
        }
        else if(row - 3 >= 0 && column - 3 >= 0 && board.getValue(row-3, column-3).equals(Player) && board.getValue(row-2, column-2).equals(Player)) {
            if(board.getValue(row-1, column-1).equals(" ")) {
                return new Field(row-1, column -1);
            }
        }
        else if(row-3 >=0 && column -3>= 0 && board.getValue(row-3, column-3).equals(Player) && board.getValue(row-1, column-1).equals(Player)) {
            if(board.getValue(row-2, column-2).equals(" ")) {
                return new Field (row-2, column - 2);
            }
        }
        return field ;

    }

private Field diagonalLeft(int row, int column, String Player) {
        Field field = null;
        if(row - 1 >= 0 && column - 1 >= 0 && row + 1 < board.getRow() && column + 1 < board.getColumn()
                && board.getValue(row+ 1, column+1).equals(Player)&& board.getValue(row-1, column-1).equals(Player)) {
            if(row + 2 < board.getRow() && column + 2 < board.getColumn() && board.getValue(row+2, column+2).equals(" ")) {
                return field = new Field(row+2, column + 2);
            }
            else if (row - 2 >=0 && column - 2 >=0 && board.getValue(row-2, column-2).equals(" ")) {
                return field = new Field(row-2, column - 2);
            }
        }
        else if(row + 2 < board.getRow() && column + 2 < board.getColumn()&& board.getValue(row+ 1, column+1).equals(Player)&& board.getValue(row+2, column+2).equals(Player)) {
            if(row +3 < board.getRow() && column + 3 < board.getColumn() && board.getValue(row+3, column+3).equals(" ")) {
                return field = new Field(row+3, column + 3);
            }
            else if (row - 1 >=0 && column - 1 >=0 && board.getValue(row-1, column-1).equals(" ")) {
                return field = new Field(row-1, column - 1);
            }
        }
        else if(row - 2 >= 0 && column -2 >=0 && board.getValue(row- 1, column-1).equals(Player)&& board.getValue(row-2, column-2).equals(Player)) {
            if(row -3>= 0 && column - 3 >= 0 && board.getValue(row-3, column-3).equals(" ")) {
                return field = new Field(row-3, column -3);
            }
            else if (row + 1 < board.getRow() && column +1 <board.getColumn() && board.getValue(row+1, column+1).equals(" ")) {
                return field = new Field(row+1, column +1);
            }
        }

        else if(row - 1 >= 0 && column - 1 >= 0 && row + 2 < board.getRow() && column + 2 < board.getColumn()
                && board.getValue(row- 1, column-1).equals(Player)&& board.getValue(row+2, column+2).equals(Player)) {


             if (row + 1 < board.getRow() && column + 1 < board.getColumn() && board.getValue(row+1, column+1).equals(" ")) {
                return field = new Field(row+1, column + 1);
            }
        }
        else if(row + 1 < board.getRow() && column -2 >= 0 && row - 2 >= 0 && column + 1 < board.getColumn()
                && board.getValue(row- 2, column-2).equals(Player)&& board.getValue(row+1, column+1).equals(Player)) {


             if (row - 1 >= 0 && column - 1 >= 0 && board.getValue(row-1, column-1).equals(" ")) {
                return field = new Field(row-1, column - 1);
            }
        }

        else if(row - 3 >=0 && column  + 3 < board.getColumn() && board.getValue(row - 2, column+2).equals(Player) && board.getValue(row-3, column+3).equals(Player)) {
            if( board.getValue(row-1, column+1).equals(" ")) {
                return new Field(row-1, column +1);
            }

        }
        else if(row - 3 >=0 && column + 3 < board.getColumn() && board.getValue(row-3, column+3).equals(Player) && board.getValue(row-1, column+1).equals(Player)) {
            if(board.getValue(row-2, column+2).equals(" ")) {
                return new Field (row -2, column + 2);
            }
        }
        else if(row + 3 < board.getRow() && column - 3 >= 0 && board.getValue(row+3, column-3).equals(Player) && board.getValue(row+2, column-2).equals(Player)) {
            if(board.getValue(row+1, column-1).equals(" ")) {
                return new Field(row+1, column -1);
            }
        }
        else if(row+3 < board.getRow() && column -3>= 0 && board.getValue(row+3, column-3).equals(Player) && board.getValue(row+1, column-1).equals(Player)) {
            if(board.getValue(row+2, column-2).equals(" ")) {
                return new Field (row+2, column - 2);
            }
        }
        return field;
    }


方法真的很高,也有一个错误..但我找不到它。您能否给我一些技巧,说明如何更好地实现这些方法?
谢谢

编辑1:

perhebs您应该只看第一种方法。我采取最后一步,并检查tehre是否是包括最后一步在内的任何威胁。因此我检查同一位玩家行中是否还有另外两个硬币构成威胁。大A是最后一步,我想检查一下小A是否造成威胁:

| | a | A | a | | | |
例如,我检查A的右边的字段是否为a,而A的左边的字段是否为a,如果为真,则检查是否周围有一个空字段,以便您可以连续获取4

| | | A | a | a | | |

| a | a | A | | | | |

| | | A | a | | a | |

| | | A | | | a | a |

依此类推..我检查了威胁的所有可能性。在行之后,我将其用于列和对角线

您看到更好的解决方案了吗?

编辑2:

private Field row(int row, int column, int Player) {
        Field field = null;
        int j = 0;
        boolean check = false;
        if(column + 3 < board.getColumn()) {
            for(int i = column; i< column + 4; i++) {
                if(board.getValue(row, i).equals(Player)){
                    j++;
                }
                else if(board.getValue(row, i).equals(" ") && check == false) {
                    j++;
                    check = true;
                    field = new Field(row,i);
                }
            }
            if(j == 4) {
                return field;
            }
        }
        return null;
    }


我将从4个不同点调用该方法。你这样行吗?

最佳答案

为什么以所有神圣的名义,您有N次被复制粘贴的相同的血腥代码?唯一的区别是血腥数字在变化。将它们重写为一个循环,然后只有一点可以检查其正确性。

关于java - 计算威胁,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22327173/

10-10 16:20