package de.movope.game;


import de.movope.game.piece.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ChessBoard {

    Map<String, Piece> pieces = new HashMap<>();

    public void initPieces() {
        initPieces(Color.WHITE);
        initPieces(Color.BLACK);
    }

    private void initPieces(Color color) {
        int row = (color == Color.WHITE) ? 1 : 8;
        pieces.put("A" + row, new Rook(color));
        pieces.put("H" + row, new Rook(color));
        pieces.put("B" + row, new Knight(color));
        pieces.put("G" + row, new Knight(color));
        pieces.put("C" + row, new Bishop(color));
        pieces.put("F" + row, new Bishop(color));
        pieces.put("D" + row, new Queen(color));
        pieces.put("E" + row, new King(color));

        if (color == Color.WHITE) {
            row++;
        } else {
            row--;
        }

        String[] files = {"A", "B", "C", "D", "E", "F", "G", "H"};
        for (int i = 0; i < 8; i++) {
            pieces.put(files[i] + row, new Pawn(color));
        }

    }

    public List<String> getSquaresWithPiece(Color color) {
        return pieces.entrySet()
                .stream()
                .filter(entry -> entry.getValue().getColor() == color)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }

    public Piece getPieceAt(Square square) {
        Piece result = pieces.get(square.toString());
        return result != null ? result : Piece.NULL;
    }

    public Piece getPieceAt(String square) {
        return getPieceAt(Square.create(square));
    }

    public void move(Square from, Square to) {
        move(from.toString(), to.toString());
    }

    public void move(String from, String to) {

        System.out.println("Move: " + from + " -> " +to);
        if (pieces.get(to) != null) {
            System.out.println("One piece removed: " + pieces.get(to));
        }
        Piece transfer = getPieceAt(from);
        pieces.remove(from);
        pieces.put(to, transfer);
    }

    public boolean occupiedFromEnemy(Square target, Color enemyColor) {
        if ((target == null) || !target.onBoard()) {
            return false;
        }
        return getPieceAt(target).getColor() == enemyColor;
    }

    public boolean canPieceMoveTo(Square target) {
        return target.onBoard() && getPieceAt(target) == Piece.NULL;
    }
}

@@ -240,881 +240,8 @@
 );%0A%0A
-    public void initPieces() %7B%0A        initPieces(Color.WHITE);%0A        initPieces(Color.BLACK);%0A    %7D%0A%0A    private void initPieces(Color color) %7B%0A        int row = (color == Color.WHITE) ? 1 : 8;%0A        pieces.put(%22A%22 + row, new Rook(color));%0A        pieces.put(%22H%22 + row, new Rook(color));%0A        pieces.put(%22B%22 + row, new Knight(color));%0A        pieces.put(%22G%22 + row, new Knight(color));%0A        pieces.put(%22C%22 + row, new Bishop(color));%0A        pieces.put(%22F%22 + row, new Bishop(color));%0A        pieces.put(%22D%22 + row, new Queen(color));%0A        pieces.put(%22E%22 + row, new King(color));%0A%0A        if (color == Color.WHITE) %7B%0A            row++;%0A        %7D else %7B%0A            row--;%0A        %7D%0A%0A        String%5B%5D files = %7B%22A%22, %22B%22, %22C%22, %22D%22, %22E%22, %22F%22, %22G%22, %22H%22%7D;%0A        for (int i = 0; i %3C 8; i++) %7B%0A            pieces.put(files%5Bi%5D + row, new Pawn(color));%0A        %7D%0A%0A    %7D%0A%0A
     
@@ -1572,11 +1572,884 @@
 L;%0A    %7D
+%0A%0A    public void initPieces() %7B%0A        initPieces(Color.WHITE);%0A        initPieces(Color.BLACK);%0A    %7D%0A%0A    private void initPieces(Color color) %7B%0A        int row = (color == Color.WHITE) ? 1 : 8;%0A        pieces.put(%22A%22 + row, new Rook(color));%0A        pieces.put(%22H%22 + row, new Rook(color));%0A        pieces.put(%22B%22 + row, new Knight(color));%0A        pieces.put(%22G%22 + row, new Knight(color));%0A        pieces.put(%22C%22 + row, new Bishop(color));%0A        pieces.put(%22F%22 + row, new Bishop(color));%0A        pieces.put(%22D%22 + row, new Queen(color));%0A        pieces.put(%22E%22 + row, new King(color));%0A%0A        if (color == Color.WHITE) %7B%0A            row++;%0A        %7D else %7B%0A            row--;%0A        %7D%0A%0A        String%5B%5D files = %7B%22A%22, %22B%22, %22C%22, %22D%22, %22E%22, %22F%22, %22G%22, %22H%22%7D;%0A        for (int i = 0; i %3C 8; i++) %7B%0A            pieces.put(files%5Bi%5D + row, new Pawn(color));%0A        %7D%0A%0A    %7D
 %0A%7D%0A

clean up