@Override public String toString() { StringBuilder s = new StringBuilder(Integer.toString(pieceRow) + ":" + Integer.toString(pieceColumn)); for (ChessboardPosition cp : steps) { s.append(cp.toString()); } return s.toString(); }
public boolean isValidMovePrefix(Move otherMove) { Piece otherPiece = otherMove.getPiece(); if (otherPiece.getPieceColor() != piece.getPieceColor() || otherMove.getPieceColumn() != pieceColumn || otherMove.getPieceRow() != pieceRow) { return false; } Iterator<ChessboardPosition> it = steps.iterator(); for (ChessboardPosition cp : otherMove.getSteps()) { if (!it.hasNext() || !cp.equals(it.next())) { return false; } } return true; }
public void doMove(Chessboard chessboard) { for (ChessboardPosition chessboardPosition : steps) { int rowDirection = Integer.signum(chessboardPosition.getRow() - this.pieceRow); int columnDirection = Integer.signum(chessboardPosition.getColumn() - this.pieceColumn); while (chessboardPosition.getRow() != this.pieceRow) { chessboard.capture(pieceRow, pieceColumn); pieceRow += rowDirection; pieceColumn += columnDirection; } chessboard.movePiece(pieceRow, pieceColumn, piece); } if (piece instanceof Pawn && ((piece.getPieceColor().equals(PieceColor.WHITE) && pieceRow == Chessboard.CHESSBOARD_SIZE - 1) || (piece.getPieceColor().equals(PieceColor.BLACK) && pieceRow == 0))) { piece = new King(piece.getPieceColor()); chessboard.movePiece(pieceRow, pieceColumn, piece); // System.out.println("New king!"); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Move other = (Move) obj; if (pieceRow != other.pieceRow) return false; if (pieceColumn != other.pieceColumn) return false; if (piece == null || other.getPiece() == null) { return false; } if (piece.pieceColor != other.getPiece().pieceColor) { return false; } // Checking for King/Pawn is unnecessary in our context Iterator<ChessboardPosition> otherStepsIt = other.getSteps().iterator(); if (steps == null) { // This is actually weird as f.. if (other.getSteps() == null) { return true; } else { return false; } } for (ChessboardPosition chessboardPosition : steps) { if (!otherStepsIt.hasNext()) { return false; } if (!chessboardPosition.equals(otherStepsIt.next())) { return false; } } if (otherStepsIt.hasNext()) { return false; } return true; }