forked from pabulaner/vivi
149 lines
5.1 KiB
Java
149 lines
5.1 KiB
Java
package de.vivi.chess.board;
|
|
|
|
import de.vivi.chess.pieces.*;
|
|
import de.vivi.chess.game.Game;
|
|
|
|
public class Board {
|
|
|
|
private static final int SIZE = 8;
|
|
private final Piece[/* column */][/* row */] grid;
|
|
|
|
public Board() {
|
|
grid = new Piece[SIZE][SIZE];
|
|
setup();
|
|
}
|
|
|
|
/**
|
|
* 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, new Field(0, 0));
|
|
grid[1][0] = new Knight(Color.BLACK, Type.KNIGHT, new Field(1, 0));
|
|
grid[2][0] = new Bishop(Color.BLACK, Type.BISHOP, new Field(2, 0));
|
|
grid[3][0] = new Queen(Color.BLACK, Type.QUEEN, new Field(3, 0));
|
|
grid[4][0] = new King(Color.BLACK, Type.KING, new Field(4, 0));
|
|
grid[5][0] = new Bishop(Color.BLACK, Type.BISHOP, new Field(5, 0));
|
|
grid[6][0] = new Knight(Color.BLACK, Type.KNIGHT, new Field(6, 0));
|
|
grid[7][0] = new Rook(Color.BLACK, Type.ROOK, new Field(7, 0));
|
|
//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[i][1] = new Pawn(Color.BLACK, Type.PAWN, new Field(i, 0));
|
|
}
|
|
|
|
grid[0][7] = new Rook(Color.WHITE, Type.ROOK, new Field(0, 7));
|
|
grid[1][7] = new Knight(Color.WHITE, Type.KNIGHT, new Field(1, 7));
|
|
grid[2][7] = new Bishop(Color.WHITE, Type.BISHOP, new Field(2, 7));
|
|
grid[3][7] = new Queen(Color.WHITE, Type.QUEEN, new Field(3, 7));
|
|
grid[4][7] = new King(Color.WHITE, Type.KING, new Field(4, 7));
|
|
grid[5][7] = new Bishop(Color.WHITE, Type.BISHOP, new Field(5, 7));
|
|
grid[6][7] = new Knight(Color.WHITE, Type.KNIGHT, new Field(6, 7));
|
|
grid[7][7] = new Rook(Color.WHITE, Type.ROOK, new Field(7, 7));
|
|
|
|
// 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);
|
|
grid[i][6] = new Pawn(Color.WHITE, Type.PAWN, new Field(i, 6));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
if (grid[from.getColumn()][from.getRow()] != null) {
|
|
grid[to.getColumn()][to.getRow()] = grid[from.getColumn()][from.getRow()];
|
|
grid[to.getColumn()][to.getRow()].setField(to);
|
|
grid[from.getColumn()][from.getRow()] = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* TODO: implement
|
|
* <p>
|
|
* Returns the piece at the given field. If no piece is
|
|
* present it just returns null.
|
|
*/
|
|
public Piece getPiece(Field field) {
|
|
return grid[field.getColumn()][field.getRow()];
|
|
}
|
|
|
|
public Piece[][] getBoard() {
|
|
return grid;
|
|
}
|
|
|
|
public Piece getPiece(int column, int row) {
|
|
return grid[column][row];
|
|
}
|
|
|
|
public void setPiece(int column, int row, Piece piece) {
|
|
grid[column][row] = piece;
|
|
if (piece != null) {
|
|
piece.setField(new Field(column, row));
|
|
}
|
|
}
|
|
|
|
@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();
|
|
}
|
|
}
|