Java免费现货

  • 本文关键字:免费 Java java javafx
  • 更新时间 :
  • 英文 :


如果不使用工件周围的坐标,我有一个工件需要被其他灰色工件包围。

我试着做了以下事情:

public List<Piece> boardPiece(){
    List<Piece> boardPieces = new ArrayList<>();
    for (Piece pieces : listToPiece) {
        if (pieces.getCoordinate() != null){
                boardPieces.add(pieces);
        }
    }
    return boardPieces;
 }
public List<Coordinate> getSurroundings() {
     List<Coordinate> surroundings = new ArrayList<>();
     for (Piece boardpieces : boardPiece()) {
         for (Coordinate coordinate : makeDirections()) {
            surroundings.add(new Coordinate(boardpieces.getCoordinate().getRow() + coordinate.getRow(), boardpieces.getCoordinate().getColumn() + coordinate.getColumn()));
         }
     }
     return surroundings;
}
public List<Coordinate> makeDirections(){
      directions.add(new Coordinate(1,-1));
      directions.add(new Coordinate(-1,1));
      directions.add(new Coordinate(-1,0));
      directions.add(new Coordinate(1,0));
      directions.add(new Coordinate(0,-1));
      directions.add(new Coordinate(0,1));
      return directions;
    }

木板是指在木板上并有坐标的一块木板。一块没有坐标的木板不应该被其他灰色的木板包围。在我的抽屉里,我写了以下代码:

public void drawEmptyPieces() {
    for (Coordinate emptyPiece : emptyPieces) {
        EmptyPiece emptyPiece1 = new EmptyPiece(new Coordinate(emptyPiece.getRow(), emptyPiece.getColumn()));
        emptyPiece1.setFill(Color.LIGHTGRAY);
        emptyPiece1.setStroke(Color.BLACK);
        gameField.getChildren().add(emptyPiece1);
    }
}

我尽量避免灰色的部分被拉到木板上:

public void drawEmptyPieces() {
    for (Coordinate emptyPiece : emptyPieces) {
        for (Piece pieceObjects : pieces){
            if (pieceObjects.getCoordinate().getRow() != emptyPiece.getRow() && pieceObjects.getCoordinate().getColumn() != emptyPiece.getRow()){
                EmptyPiece emptyPiece1 = new EmptyPiece(emptyPiece);
                emptyPiece1.setFill(Color.LIGHTGRAY);
                emptyPiece1.setStroke(Color.BLACK);
                gameField.getChildren().add(emptyPiece1);
            }
        }
    }
}

但是这个代码不起作用。

我不知道您是否提供了足够的信息来回答您的问题。但这里有一些想法。


makeDirections()已损坏(缺少directions局部变量的声明),或者由于每次调用它时都会向directions字段添加6个新的Coordinate对象而损坏。无论哪种情况,它所做的工作都超出了需要。考虑一下:

private final static List<Coordinate> directions;
static {
    List<Coordinate> dirs = new ArrayList<>();
    dirs.add(new Coordinate(1,-1));
    dirs.add(new Coordinate(-1,1));
    dirs.add(new Coordinate(-1,0));
    dirs.add(new Coordinate(1,0));
    dirs.add(new Coordinate(0,-1));
    dirs.add(new Coordinate(0,1));
    directions = Collections.immutableList(dirs);
}
public static List<Coordinate> makeDirections() {  // or getDirections()
    return directions;
}

getSurroundings()看起来可以(将?)多次向周围环境添加相同的Coordinate。如果一个片段位于[5,5],而另一个片段处于[7,5],则会将[5,5]+[1,0][7,5]+[-1,0]添加到环境中。在CCD_ 11处甚至可能存在一个片段,该片段也将把[5,5][7,5]添加到周围环境中。

相反。。。

确保Coordinate实现equalshashCode,因此具有相同行和列的两个Coordinate对象是相等的,并且都散列到相同的值。然后,在getSurroundings()中使用:

Set<Coordinate> surroundings = new HashSet<>();

使得CCD_ 19只包含每个坐标一次。返回之前,您可以从周围环境中删除所有"已占用"的坐标。

for (Piece piece : pieces) {
    surroundings.remove(piece.getCoordinate());
}

您可以返回集合而不是列表,或者如果您想将其保留为列表:

return new ArrayList<>(surroundings);

如果没有包含副本的"环境"和现有作品所在的坐标,你可能会发现绘制棋盘要容易得多。


除非你的游戏板的范围是无限的,否则你将在游戏板的边缘之外创建Coordinate对象。考虑将方法getNeighbours()添加到Coordinate类:

class Coordinate { 
    public Collection<Coordinate> getNeighbours() {
        List<Coordinate> neighbours = new ArrayList<>(6);
        for (Coordinate dir: getDirections()) {
            Coordinate n = new Coordinate(getRow() + dir.getRow(), getColumn() + dir.getColumn());
            if (n.isValid()) {
                neighbours.add(n);
            }
        }
        return neighbours;
    }
}

然后getSurroundings变得简单一点,使用:

for (Piece boardpieces : boardPiece()) {
    surroundings.addAll(boardpieces.getCoordinate().getNeighbours());
}

在没有任何"环境"的情况下。


"方向"不是Coordinate。它们用于不同的用途。Coordinate是板上的一个位置,其中"方向"是相对偏移。该板可能具有行和列值必须介于(例如)0和20之间的约束;您的"Direction"对象具有负的行和列值;这将违反这些约束。这意味着,如果给定了一个错误的值,则无法在Coordinate的构造函数中添加正确的检查和throw new IllegalArgumentException("Invalid coordinate")

我将为Direction对象创建一个新类。实际上,我会使用enum Direction { ... }。然后Direction.values()将返回所有方向的集合——您不需要自己创建该集合。这是一个比其他更改大得多的更改,可能会产生超出您提供的代码子集的后果。这是一个很好的改变,但可能还有很多工作要做。


希望能有所帮助。

最新更新