2025-02-26 10:05:52 +01:00
|
|
|
package de.vivi.chess.board;
|
|
|
|
|
|
|
|
|
|
import de.vivi.chess.pieces.*;
|
|
|
|
|
|
|
|
|
|
public class Board {
|
|
|
|
|
|
|
|
|
|
private static final int SIZE = 8;
|
|
|
|
|
private Field field;
|
|
|
|
|
private final Piece[/* column */][/* row */] grid;
|
|
|
|
|
|
|
|
|
|
public Board() {
|
|
|
|
|
grid = new Piece[SIZE][SIZE];
|
|
|
|
|
setup();
|
|
|
|
|
//field = new Field(0,0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: implement
|
|
|
|
|
* <p>
|
|
|
|
|
* This method should setup the board like it is common
|
|
|
|
|
* for a chess game. Black is in the top two rows, white
|
|
|
|
|
* in the bottom two rows. "White queen, white field, black
|
|
|
|
|
* queen, black field".
|
|
|
|
|
*/
|
|
|
|
|
private void setup() {
|
|
|
|
|
grid[0][0] = new Rook(Color.BLACK, Type.ROOK);
|
|
|
|
|
grid[1][0] = new Knight(Color.BLACK, Type.KNIGHT);
|
|
|
|
|
grid[2][0] = new Bishop(Color.BLACK, Type.BISHOP);
|
|
|
|
|
grid[3][0] = new Queen(Color.BLACK, Type.QUEEN);
|
|
|
|
|
grid[4][0] = new King(Color.BLACK, Type.KING);
|
|
|
|
|
grid[5][0] = new Bishop(Color.BLACK, Type.BISHOP);
|
|
|
|
|
grid[6][0] = new Knight(Color.BLACK, Type.KNIGHT);
|
|
|
|
|
grid[7][0] = new Rook(Color.BLACK, Type.ROOK);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
|
|
grid[i][1] = new Pawn(Color.BLACK, Type.PAWN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grid[0][7] = new Rook(Color.WHITE, Type.ROOK);
|
|
|
|
|
grid[1][7] = new Knight(Color.WHITE, Type.KNIGHT);
|
|
|
|
|
grid[2][7] = new Bishop(Color.WHITE, Type.BISHOP);
|
|
|
|
|
grid[3][7] = new Queen(Color.WHITE, Type.QUEEN);
|
|
|
|
|
grid[4][7] = new King(Color.WHITE, Type.KING);
|
|
|
|
|
grid[5][7] = new Bishop(Color.WHITE, Type.BISHOP);
|
|
|
|
|
grid[6][7] = new Knight(Color.WHITE, Type.KNIGHT);
|
|
|
|
|
grid[7][7] = new Rook(Color.WHITE, Type.ROOK);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
|
|
grid[i][6] = new Pawn(Color.WHITE, Type.PAWN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: implement
|
|
|
|
|
* <p>
|
|
|
|
|
* Moves the piece at the from field to the to field.
|
|
|
|
|
* Doesn't check for validity, this has to be done somewhere
|
|
|
|
|
* else. If a piece is already at the to field, this piece is
|
|
|
|
|
* just getting replaced.
|
|
|
|
|
*/
|
|
|
|
|
public void move(Field from, Field to) {
|
2025-03-01 16:04:33 +01:00
|
|
|
//System.out.println(getPiece(from).getType());
|
|
|
|
|
//System.out.println(from.getColumn());
|
|
|
|
|
|
2025-02-26 10:05:52 +01:00
|
|
|
if (getPiece(from).getType() == Type.PAWN &&
|
2025-03-01 16:04:33 +01:00
|
|
|
(getPiece(from).getColor() == Color.WHITE ||
|
|
|
|
|
getPiece(from).getColor() == Color.BLACK)) {
|
|
|
|
|
grid[to.getColumn()][to.getRow()] = getPiece(from);
|
|
|
|
|
grid[from.getColumn()][from.getRow()] = null;
|
|
|
|
|
} else if (getPiece(from).getType() == Type.ROOK &&
|
2025-02-26 10:05:52 +01:00
|
|
|
getPiece(from).getColor() == Color.WHITE) {
|
2025-03-01 16:04:33 +01:00
|
|
|
grid[to.getColumn()][to.getRow()] = new Rook(Color.WHITE, Type.ROOK);
|
|
|
|
|
grid[from.getColumn()][from.getRow()] = null;
|
2025-02-26 10:05:52 +01:00
|
|
|
|
2025-03-01 16:04:33 +01:00
|
|
|
//check if move is valid by piece
|
|
|
|
|
//set Piece to new position
|
|
|
|
|
// if (getPiece(from).getType() == Type.PAWN &&
|
|
|
|
|
// getPiece(from).getColor() == Color.WHITE) {
|
|
|
|
|
// grid[to.getColumn()][to.getRow()] = getPiece(from);
|
|
|
|
|
// grid[from.getColumn()][from.getRow()] = null;
|
|
|
|
|
// } else
|
|
|
|
|
}
|
2025-02-26 10:05:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: implement
|
|
|
|
|
* <p>
|
|
|
|
|
* Returns the piece at the given field. If no piece is
|
|
|
|
|
* present it just returns null.
|
|
|
|
|
*/
|
|
|
|
|
public Piece getPiece(Field field) {
|
|
|
|
|
//get Piece von Feld A7 = grid[0][6]
|
|
|
|
|
return grid[field.getColumn()][field.getRow()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
boolean whiteField = true;
|
|
|
|
|
|
|
|
|
|
result.append(" ");
|
|
|
|
|
|
|
|
|
|
for (int column = 0; column < SIZE; column++) {
|
|
|
|
|
result.append((char) ('A' + column)).append(' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.append('\n');
|
|
|
|
|
|
|
|
|
|
for (int row = 0; row < SIZE; row++) {
|
|
|
|
|
result.append((char) ('1' + (char) row)).append(' ');
|
|
|
|
|
|
|
|
|
|
for (int column = 0; column < SIZE; column++) {
|
|
|
|
|
Piece piece = grid[column][row];
|
|
|
|
|
|
|
|
|
|
if (piece == null) {
|
|
|
|
|
result.append(whiteField ? '\u25a0' : '\u25a1');
|
|
|
|
|
} else {
|
|
|
|
|
result.append(piece.getSymbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.append(' ');
|
|
|
|
|
whiteField = !whiteField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.append('\n');
|
|
|
|
|
whiteField = !whiteField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|