This commit is contained in:
vimo 2025-02-26 10:05:52 +01:00
commit 2a719cfe8a
2 changed files with 225 additions and 0 deletions

120
chess/board/Board.java Normal file
View File

@ -0,0 +1,120 @@
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) {
System.out.println(getPiece(from).getType());
System.out.println(from.getColumn());
if (getPiece(from).getType() == Type.PAWN &&
getPiece(from).getColor() == Color.WHITE) {
grid[0][4] = new Pawn()
}
//check if move is valid by piece
//set Piece to new position
}
/**
* 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();
}
}

105
chess/board/Field.java Normal file
View File

@ -0,0 +1,105 @@
package de.vivi.chess.board;
import de.vivi.chess.pieces.Piece;
/**
* TODO: implement fields and constructor
* <p>
* This class contains information to identify a single field
* on the board by its column and row.
*/
public class Field {
private int column;
private int row;
private Board board;
public Field(int column, int row) {
this.column = column;
this.row = row;
//board = new Board();
}
/**
* TODO: implement
* <p>
* Returns a new field that represents the field at the
* column and row indices.
*/
public static Field from(int column, int row) {
return new Field(column, row);
}
/**
* TODO: implement
* <p>
* Returns a new field that represents the field represented
* by the provided string (like "A5", "E7", ...). If the string
* is not valid, because it is garbage (like "hello", "world", ...)
* or the indices are out of bounds (like "K3", "B9", ...")
* it should throw an IllegalArgument exception.
*/
public static Field fromString(String value) throws IllegalArgumentException {
char[] array = value.toCharArray();
int column_ = 0;
int row_ = 0;
//System.out.println(array[0]);
switch(array[0]) {
case 'A': column_ = 0;
break;
case 'B': column_ = 1;
break;
case 'C': column_ = 2;
break;
case 'D': column_ = 3;
break;
case 'E': column_ = 4;
break;
case 'F': column_ = 5;
break;
case 'G': column_ = 6;
break;
case 'H': column_ = 7;
break;
default:
System.out.println("Wrong input");
}
//System.out.println(array[1]);
switch(array[1]) {
case '1': row_ = 0;
break;
case '2': row_ = 1;
break;
case '3': row_ = 2;
break;
case '4': row_ = 3;
break;
case '5': row_ = 4;
break;
case '6': row_ = 5;
break;
case '7': row_ = 6;
break;
case '8': row_ = 7;
break;
default:
System.out.println("Wrong input");
}
//System.out.println(from(column_, row_));
return from(column_, row_);
}
public int getColumn() {
// TODO: implement
return column;
}
public int getRow() {
// TODO: implement
return row;
}
}