[LIB-9] Add original chesshog source files
[chesshog.git] / src / main / java / org / hedgecode / chess / position / Piece.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.position;
18
19 /**
20  *
21  *
22  * @author Dmitry Samoshin aka gotty
23  */
24 public enum Piece {
25
26     PAWN    ('p'),
27     KNIGHT  ('n'),
28     BISHOP  ('b'),
29     ROOK    ('r'),
30     QUEEN   ('q'),
31     KING    ('k');
32
33     private char letter;
34
35     Piece(char letter) {
36         this.letter = letter;
37     }
38
39     public char letter() {
40         return letter;
41     }
42
43     public static Piece byLetter(char letter) {
44         for (Piece piece : Piece.values()) {
45             if (piece.letter() == letter)
46                 return piece;
47         }
48         return null;
49     }
50
51     public static Piece byLetter(String letter) {
52         if (letter == null || letter.length() != 1)
53             return null;
54         for (Piece piece : Piece.values()) {
55             if (piece.letter() == letter.charAt(0))
56                 return piece;
57         }
58         return null;
59     }
60
61 }