2 * Copyright (c) 2018-2019. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.img.piece;
19 import java.awt.image.BufferedImage;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
25 import org.hedgecode.chess.position.Color;
26 import org.hedgecode.chess.position.ColorPiece;
27 import org.hedgecode.chess.position.Piece;
32 * @author Dmitry Samoshin aka gotty
34 public class PieceSet {
36 private Map<ColorPiece, BufferedImage> pieceSetMap = new HashMap<>();
38 private static String[] allPieceNames;
43 public void add(String name, BufferedImage image) {
44 if (name != null && name.length() == 2) {
45 name = name.toLowerCase();
46 ColorPiece colorPiece = ColorPiece.getColorPiece(
47 Color.byLetter(name.charAt(0)),
48 Piece.byLetter(name.charAt(1))
50 if (colorPiece != null){
51 add(colorPiece, image);
56 public void add(ColorPiece colorPiece, BufferedImage image) {
57 if (pieceSetMap.containsKey(colorPiece)) {
58 pieceSetMap.replace(colorPiece, image);
60 pieceSetMap.put(colorPiece, image);
64 public BufferedImage get(ColorPiece colorPiece) {
65 return pieceSetMap.get(colorPiece);
68 public boolean isFull() {
69 for (ColorPiece colorPiece : ColorPiece.values()) {
70 if (!pieceSetMap.containsKey(colorPiece)
71 || (pieceSetMap.get(colorPiece) == null)) {
78 public static String[] getAllPieceNames() {
79 if (allPieceNames == null) {
80 List<String> listNames = new ArrayList<>();
81 for (Color color : Color.values()) {
82 for (Piece piece : Piece.values()) {
85 new char[] {color.letter(), piece.letter()}
90 allPieceNames = listNames.toArray(new String[listNames.size()]);
95 public static PieceSet create() {
96 return new PieceSet();