Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chess_.engine.classic.board; import chess_.engine.Alliance; import chess_.engine.classic.pieces.Bishop; import chess_.engine.classic.pieces.King; import chess_.engine.classic.pieces.Knight; import chess_.engine.classic.pieces.Pawn; import chess_.engine.classic.pieces.Piece; import chess_.engine.classic.pieces.Queen; import chess_.engine.classic.pieces.Rook; import chess_.engine.classic.player.BlackPlayer; import chess_.engine.classic.player.Player; import chess_.engine.classic.player.WhitePlayer; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * @author moham */ public final class Board { private final List<Tile> gameBoard; private final Collection<Piece> whitePieces; private final Collection<Piece> blackPieces; private final WhitePlayer whitePlayer; private final BlackPlayer blackPlayer; private final Player currentPlayer; // private final Pawn enPassantPawn; private final Move transitionMove; public Board(final Builder builder) { this.gameBoard = createGameBoard(builder); this.whitePieces = calculateActivePieces(builder, Alliance.WHITE); this.blackPieces = calculateActivePieces(builder, Alliance.BLACK); this.enPassantPawn = builder.enPassantPawn; final Collection<Move> whiteStandardMoves = calculateLegalMoves(this.whitePieces); final Collection<Move> blackStandardMoves = calculateLegalMoves(this.blackPieces); this.whitePlayer = new WhitePlayer(this, whiteStandardMoves, blackStandardMoves); this.blackPlayer = new BlackPlayer(this, whiteStandardMoves, blackStandardMoves); this.currentPlayer = builder.nextMoveMaker.choosePlayerByAlliance(this.whitePlayer, this.blackPlayer); this.transitionMove = builder.transitionMove != null ? builder.transitionMove : Move.NULL_MOVE; } @Override public String toString() { final StringBuilder builder = new StringBuilder(); for (int i = 0; i < BoardUtils.NUM_TILES; i++) { final String tileText = prettyPrint(this.gameBoard.get(i)); builder.append(String.format("%3s", tileText)); if ((i + 1) % 8 == 0) { builder.append("\n"); } } return builder.toString(); } private static String prettyPrint(final Tile tile) { if (tile.isTileOccupied()) { return tile.getPiece().getPieceAllegiance().isBlack() ? tile.toString().toLowerCase() : tile.toString(); } return tile.toString(); } public Collection<Piece> getBlackPieces() { return this.blackPieces; } public Collection<Piece> getWhitePieces() { return this.whitePieces; } public Iterable<Piece> getAllPieces() { return Iterables.unmodifiableIterable(Iterables.concat(this.whitePieces, this.blackPieces)); } public Iterable<Move> getAllLegalMoves() { return Iterables.unmodifiableIterable( Iterables.concat(this.whitePlayer.getLegalMoves(), this.blackPlayer.getLegalMoves())); } public WhitePlayer whitePlayer() { return this.whitePlayer; } public BlackPlayer blackPlayer() { return this.blackPlayer; } public Player currentPlayer() { return this.currentPlayer; } public Tile getTile(final int coordinate) { return this.gameBoard.get(coordinate); } public List<Tile> getGameBoard() { return this.gameBoard; } public Pawn getEnPassantPawn() { return this.enPassantPawn; } public Move getTransitionMove() { return this.transitionMove; } public static Board createStandardBoard() { final Builder builder = new Builder(); // Black Layout builder.setPiece(new Rook(Alliance.BLACK, 0)); builder.setPiece(new Knight(Alliance.BLACK, 1)); builder.setPiece(new Bishop(Alliance.BLACK, 2)); builder.setPiece(new Queen(Alliance.BLACK, 3)); builder.setPiece(new King(Alliance.BLACK, 4, true, true)); builder.setPiece(new Bishop(Alliance.BLACK, 5)); builder.setPiece(new Knight(Alliance.BLACK, 6)); builder.setPiece(new Rook(Alliance.BLACK, 7)); builder.setPiece(new Pawn(Alliance.BLACK, 8)); builder.setPiece(new Pawn(Alliance.BLACK, 9)); builder.setPiece(new Pawn(Alliance.BLACK, 10)); builder.setPiece(new Pawn(Alliance.BLACK, 11)); builder.setPiece(new Pawn(Alliance.BLACK, 12)); builder.setPiece(new Pawn(Alliance.BLACK, 13)); builder.setPiece(new Pawn(Alliance.BLACK, 14)); builder.setPiece(new Pawn(Alliance.BLACK, 15)); // White Layout builder.setPiece(new Pawn(Alliance.WHITE, 48)); builder.setPiece(new Pawn(Alliance.WHITE, 49)); builder.setPiece(new Pawn(Alliance.WHITE, 50)); builder.setPiece(new Pawn(Alliance.WHITE, 51)); builder.setPiece(new Pawn(Alliance.WHITE, 52)); builder.setPiece(new Pawn(Alliance.WHITE, 53)); builder.setPiece(new Pawn(Alliance.WHITE, 54)); builder.setPiece(new Pawn(Alliance.WHITE, 55)); builder.setPiece(new Rook(Alliance.WHITE, 56)); builder.setPiece(new Knight(Alliance.WHITE, 57)); builder.setPiece(new Bishop(Alliance.WHITE, 58)); builder.setPiece(new Queen(Alliance.WHITE, 59)); builder.setPiece(new King(Alliance.WHITE, 60, true, true)); builder.setPiece(new Bishop(Alliance.WHITE, 61)); builder.setPiece(new Knight(Alliance.WHITE, 62)); builder.setPiece(new Rook(Alliance.WHITE, 63)); //white to move builder.setMoveMaker(Alliance.WHITE); //build the board return builder.build(); } private static List<Tile> createGameBoard(final Builder boardBuilder) { final Tile[] tiles = new Tile[BoardUtils.NUM_TILES]; for (int i = 0; i < BoardUtils.NUM_TILES; i++) { tiles[i] = (Tile) Tile.createTile(i, boardBuilder.boardConfig.get(i)); } return ImmutableList.copyOf(tiles); } private Collection<Move> calculateLegalMoves(final Collection<Piece> pieces) { final List<Move> legalMoves = new ArrayList<>(35); for (final Piece piece : pieces) { legalMoves.addAll(piece.calculateLegalMoves(this)); } return ImmutableList.copyOf(legalMoves); } private static Collection<Piece> calculateActivePieces(final Builder builder, final Alliance alliance) { final List<Piece> activePieces = new ArrayList<>(16); for (final Piece piece : builder.boardConfig.values()) { if (piece.getPieceAllegiance() == alliance) { activePieces.add(piece); } } return ImmutableList.copyOf(activePieces); } public static class Builder { Map<Integer, Piece> boardConfig; Alliance nextMoveMaker; Pawn enPassantPawn; Move transitionMove; public Builder() { this.boardConfig = new HashMap<>(33, 1.0f); } public Builder setPiece(final Piece piece) { this.boardConfig.put(piece.getPiecePosition(), piece); return this; } public Builder setMoveMaker(final Alliance nextMoveMaker) { this.nextMoveMaker = nextMoveMaker; return this; } public Builder setEnPassantPawn(final Pawn enPassantPawn) { this.enPassantPawn = enPassantPawn; return this; } public Builder setMoveTransition(final Move transitionMove) { this.transitionMove = transitionMove; return this; } public Board build() { return new Board(this); } } }