diff --git a/chess/pieces/Pawn.java b/chess/pieces/Pawn.java index 0243ec1..a056a31 100644 --- a/chess/pieces/Pawn.java +++ b/chess/pieces/Pawn.java @@ -30,16 +30,27 @@ public class Pawn extends Piece { public boolean isValidMove(Board board, Field from, Field to) { if (board.getPiece(from).getSymbol() == symbol && board.getPiece(from).getColor() == Color.WHITE) { - if (board.getPiece(to).getSymbol() != symbol && - board.getPiece(to).getColor() != Color.WHITE) { - isValid = true; + if (board.getPiece(to) == null) { + if (from.getRow() == 6 && to.getRow() == 4) { + isValid = true; + } else if (from.getRow() == 6 && to.getRow() == 5) { + isValid = true; + } } + + } else if (board.getPiece(from).getSymbol() == symbol && + board.getPiece(from).getColor() == Color.BLACK) { + if (board.getPiece(to) == null) { + if (from.getRow() == 1 && to.getRow() == 2) { + isValid = true; + } else if (from.getRow() == 1 && to.getRow() == 3) { + isValid = true; + } + } } else { isValid = false; } - System.out.println(isValid); - return isValid; } } diff --git a/chess/pieces/Piece.java b/chess/pieces/Piece.java new file mode 100644 index 0000000..82a4f3d --- /dev/null +++ b/chess/pieces/Piece.java @@ -0,0 +1,50 @@ +package de.vivi.chess.pieces; + +import de.vivi.chess.board.Board; +import de.vivi.chess.board.Field; + +public abstract class Piece { + + private final Color color; + + private final Type type; + protected char symbol; + + protected Piece(Color color, Type type) { + this.color = color; + this.type = type; + symbol = '\u0000'; + } + + /** + * TODO: implement + *
+ * This method needs to be implemented in each piece by + * using inheritance. It returns true if the move from the + * from field to the to field is considered a valid move + * for the type of piece this method is called on. Otherwise + * it returns false. + */ + public abstract boolean isValidMove(Board board, Field from, Field to); + + public Color getColor() { + // TODO: implement + return color; + } + + public Type getType() { + // TODO: implement + return type; + } + + /** + * TODO: implement + *
+ * Use unicode symbols for the chess pieces. You can find + * them here: + * Unicode chess pieces + */ + public char getSymbol() { + return symbol; + } +} diff --git a/chess/pieces/Rook.java b/chess/pieces/Rook.java index 5d28dbf..7ddd89f 100644 --- a/chess/pieces/Rook.java +++ b/chess/pieces/Rook.java @@ -5,6 +5,8 @@ import de.vivi.chess.board.Field; public class Rook extends Piece { + private boolean isValid = false; + public Rook(Color color, Type type) { super(color, type); } @@ -26,6 +28,51 @@ public class Rook extends Piece { @Override public boolean isValidMove(Board board, Field from, Field to) { - return false; + if (board.getPiece(from).getSymbol() == symbol && + board.getPiece(from).getColor() == Color.WHITE) { + + if (from.getColumn() == to.getColumn()) { + for (int i = 1; i < 8; i++) { + Field freeField = Field.from(from.getColumn(), from.getRow() - i); + //System.out.println(freeField.getRow()); + //System.out.println(board.getPiece(freeField).getColor()); + + if (board.getPiece(Field.from(from.getColumn(), freeField.getRow())) == null) { + isValid = true; + } else if ((freeField.getRow() != to.getRow()) && + (board.getPiece(Field.from(from.getColumn(), freeField.getRow())) != null) && + (board.getPiece(Field.from(from.getColumn(), freeField.getRow())).getColor() == Color.BLACK)) { + isValid = false; + System.err.println("Illegal move, try again: "); + } else + /*if ((board.getPiece(Field.from(from.getColumn(), freeField.getRow())).getColor() == Color.BLACK ) && + (freeField.getRow()) == to.getRow()) { + isValid = true; + //System.out.println(board.getPiece(freeField).getType()); + } else + if ((board.getPiece(Field.from(from.getColumn(), freeField.getRow())).getColor() == Color.WHITE) && + (freeField.getRow()) != to.getRow()) { + */ + if ((board.getPiece(Field.from(from.getColumn(), freeField.getRow())) != null) && + (board.getPiece(Field.from(from.getColumn(), freeField.getRow())).getColor() == Color.WHITE)) { + isValid = false; + //System.err.println("Illegal move, try again: "); + } else { + isValid = false; + //System.err.println("Illegal move, try again: "); + } + } + } else { + isValid = false; + //System.err.println("Illegal move, try again: "); + } + } else if (board.getPiece(from).getSymbol() == symbol && + board.getPiece(from).getColor() == Color.BLACK) { + isValid = true; + } else { + isValid = false; + } + //System.out.println(isValid); + return isValid; } }