This commit is contained in:
vimo 2025-03-01 16:06:01 +01:00
parent 8b380fd69d
commit 69d6dbee3b
3 changed files with 114 additions and 6 deletions

View File

@ -30,16 +30,27 @@ public class Pawn extends Piece {
public boolean isValidMove(Board board, Field from, Field to) { public boolean isValidMove(Board board, Field from, Field to) {
if (board.getPiece(from).getSymbol() == symbol && if (board.getPiece(from).getSymbol() == symbol &&
board.getPiece(from).getColor() == Color.WHITE) { 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 { } else {
isValid = false; isValid = false;
} }
System.out.println(isValid);
return isValid; return isValid;
} }
} }

50
chess/pieces/Piece.java Normal file
View File

@ -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
* <p>
* 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
* <p>
* Use unicode symbols for the chess pieces. You can find
* them here:
* <a href="https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode">Unicode chess pieces</a>
*/
public char getSymbol() {
return symbol;
}
}

View File

@ -5,6 +5,8 @@ import de.vivi.chess.board.Field;
public class Rook extends Piece { public class Rook extends Piece {
private boolean isValid = false;
public Rook(Color color, Type type) { public Rook(Color color, Type type) {
super(color, type); super(color, type);
} }
@ -26,6 +28,51 @@ public class Rook extends Piece {
@Override @Override
public boolean isValidMove(Board board, Field from, Field to) { 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;
} }
} }