53 lines
1.6 KiB
Java
53 lines
1.6 KiB
Java
import java.io.PrintStream;
|
|
import java.util.ArrayList;
|
|
|
|
class Board {
|
|
ArrayList<Piece> Pieces;
|
|
|
|
Board() {
|
|
this.Pieces = new ArrayList<Piece>();
|
|
}
|
|
|
|
void add(Piece piece) {
|
|
if (piece.board != this)
|
|
throw new IllegalArgumentException("wrong board");
|
|
if (piece.board.pieceAt(piece.row, piece.col) != null)
|
|
throw new IllegalArgumentException("field is occupied");
|
|
Pieces.add(piece);
|
|
}
|
|
|
|
void printBoard(PrintStream out) {
|
|
out.println(" 1 2 3 4 5 6 7 8");
|
|
out.println(" +---+---+---+---+---+---+---+---+");
|
|
for (int Row = 1; Row <= 8; Row++) {
|
|
out.print("" + Row + " ");
|
|
for (int Col = 1; Col <= 8; Col++) {
|
|
final Piece p = pieceAt(Row, Col);
|
|
final char c = p == null ? ' ' : p.charRep();
|
|
out.print("| " + c + " ");
|
|
}
|
|
out.println("|");
|
|
out.println(" +---+---+---+---+---+---+---+---+");
|
|
}
|
|
}
|
|
|
|
Piece pieceAt(int row, int col) {
|
|
for (Piece p : Pieces)
|
|
if (p.row == row && p.col == col)
|
|
return p;
|
|
return null;
|
|
}
|
|
|
|
void check(PrintStream out) {
|
|
for (Piece P1 : Pieces) {
|
|
out.println(P1.showPiece());
|
|
for (Piece P2 : Pieces)
|
|
if (P1 != P2)
|
|
if (P1.canCapture(P2))
|
|
out.println(" can capture " + P2.showPiece());
|
|
else
|
|
out.println(" cannot capture " + P2.showPiece());
|
|
}
|
|
}
|
|
}
|