X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fimg%2Fpiece%2FPieceSet.java;fp=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fimg%2Fpiece%2FPieceSet.java;h=342ba1aec49a2bec39b94eb947482989d97325fa;hp=0000000000000000000000000000000000000000;hb=ccafd673bfcda89a75f428e58dc06525e1b0a368;hpb=d3bad7f328495d5746704f914fd335a2487ad2bb diff --git a/src/main/java/org/hedgecode/chess/img/piece/PieceSet.java b/src/main/java/org/hedgecode/chess/img/piece/PieceSet.java new file mode 100644 index 0000000..342ba1a --- /dev/null +++ b/src/main/java/org/hedgecode/chess/img/piece/PieceSet.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2018-2019. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.img.piece; + +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.hedgecode.chess.position.Color; +import org.hedgecode.chess.position.ColorPiece; +import org.hedgecode.chess.position.Piece; + +/** + * + * + * @author Dmitry Samoshin aka gotty + */ +public class PieceSet { + + private Map pieceSetMap = new HashMap<>(); + + private static String[] allPieceNames; + + PieceSet() { + } + + public void add(String name, BufferedImage image) { + if (name != null && name.length() == 2) { + name = name.toLowerCase(); + ColorPiece colorPiece = ColorPiece.getColorPiece( + Color.byLetter(name.charAt(0)), + Piece.byLetter(name.charAt(1)) + ); + if (colorPiece != null){ + add(colorPiece, image); + } + } + } + + public void add(ColorPiece colorPiece, BufferedImage image) { + if (pieceSetMap.containsKey(colorPiece)) { + pieceSetMap.replace(colorPiece, image); + } else { + pieceSetMap.put(colorPiece, image); + } + } + + public BufferedImage get(ColorPiece colorPiece) { + return pieceSetMap.get(colorPiece); + } + + public boolean isFull() { + for (ColorPiece colorPiece : ColorPiece.values()) { + if (!pieceSetMap.containsKey(colorPiece) + || (pieceSetMap.get(colorPiece) == null)) { + return false; + } + } + return true; + } + + public static String[] getAllPieceNames() { + if (allPieceNames == null) { + List listNames = new ArrayList<>(); + for (Color color : Color.values()) { + for (Piece piece : Piece.values()) { + listNames.add( + String.valueOf( + new char[] {color.letter(), piece.letter()} + ) + ); + } + } + allPieceNames = listNames.toArray(new String[listNames.size()]); + } + return allPieceNames; + } + + public static PieceSet create() { + return new PieceSet(); + } + +}