给定Piece
的2D数组。有位置等。
Piece[][] board = new Piece[8][8];
给定一对值Location
对象(row,col)。
给出验证移动的验证器(标准国际象棋):
public boolean isValidMove(Piece piece, Location toLocation) { ... }
我写了这个:
/**
* Generates a List of possible overtakes for a Piece.
*
* @param piece
* Piece that will be overtaking another.
* @return A List of possible overtakes the piece can make.
*/
public List<Piece> possibleMoves(Piece piece) {
return Stream.of(board.getBoard()).flatMap(Stream::of).filter(
p -> p != null && isValidMove(piece, p.getLocation())).collect(
Collectors.toList());
}
但这只会返回它可以超越的Piece
。我怎样才能获得Location
?
另外一个问题,这看起来很奇特,但除了表现之外。无论如何,它比for循环更好还是更差?