import java.util.Scanner;
public class FillBoard {
//Pieces counters, there are six types of pieces for each color
private static int[] whitePieces = new int[6];
private static int[] blackPieces = new int[6];
//We also could use a matrix of 2x6
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
while (sumOfPieces() < 32) {
System.out.println("Pieces remaining: " + (32 - sumOfPieces()));
System.out.println("Insert color and name of piece (e.g. 'white pawn'):");
String input = keyboard.nextLine().toLowerCase();
//We use "split" to separate name from color in an array
String[] piece = input.split(" ");
//Evaluating name of piece using "switch"
switch(piece[1]) {
//for each piece, we also evaluate the color with "if else"
case "pawn":
evaluateColorForCounters(piece[0], 0, 8);
break;
case "rook":
evaluateColorForCounters(piece[0], 1, 2);
break;
case "bishop":
evaluateColorForCounters(piece[0], 2, 2);
break;
case "knight":
evaluateColorForCounters(piece[0], 3, 2);
break;
case "queen":
evaluateColorForCounters(piece[0], 4, 1);
break;
case "king":
evaluateColorForCounters(piece[0], 5, 1);
break;
default:
System.out.println("Unknow name of piece");
}
System.out.println("\n");
}
System.out.println("The chessboard is full");
System.out.println("\n\t\t PROGRAM EXIT");
keyboard.close();
}
private static int sumOfPieces() {
int sumWhite = 0, sumBlack = 0;
for (int i = 0; i < 6; i++) {
sumWhite += whitePieces[i];
sumBlack += blackPieces[i];
}
return sumWhite + sumBlack;
}
private static void evaluateColorForCounters(String color, int position, int limit) {
if (color.equals("white")) {
if (whitePieces[position] < limit) {
whitePieces[position]++;
System.out.println("Piece added to the board.");
}
else
System.out.println("Piece not added. Limit reached.");
}
else if (color.equals("black")) {
if (blackPieces[position] < limit) {
blackPieces[position]++;
System.out.println("Piece added to the board.");
}
else
System.out.println("Piece not added. Limit reached.");
}
else
System.out.println("Unknow color.");
}
}