|
| 1 | + /* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Frank Kopp |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +package fko.FrankyJ.types; |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * The Color class represents the two colors of a chess game and a |
| 31 | + * special color for empty fields (NONE). |
| 32 | + * This class can not be instantiated. It keeps public references to the only possible instances |
| 33 | + * White and Black |
| 34 | + * These instances are immutable. As it is not possible to have any other instances of |
| 35 | + * Colors the use of these instances is as fast as if using an int. |
| 36 | + * @author Frank Kopp ([email protected]) |
| 37 | + */ |
| 38 | + public enum Color { |
| 39 | + |
| 40 | + // order has influence on Piece |
| 41 | + White(1), // 0 |
| 42 | + Black(-1); // 1 |
| 43 | + |
| 44 | + /** |
| 45 | + * This is 1 for white and -1 for black. Useful in evaluation and pawn directions |
| 46 | + */ |
| 47 | + public final int pawnDir; |
| 48 | + private Color opponentColor; |
| 49 | + private char shortName; |
| 50 | + |
| 51 | + Color(int pawnDir) { |
| 52 | + this.pawnDir = pawnDir; |
| 53 | + } |
| 54 | + |
| 55 | + public static final Color[] values = {White, Black}; |
| 56 | + |
| 57 | + static { |
| 58 | + for (Color c : Color.values()) { |
| 59 | + switch (c) { |
| 60 | + case White -> { |
| 61 | + c.opponentColor = Black; |
| 62 | + c.shortName = 'w'; |
| 63 | + } |
| 64 | + case Black -> { |
| 65 | + c.opponentColor = White; |
| 66 | + c.shortName = 'b'; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the other Color. |
| 74 | + * @return int - as defined in Color |
| 75 | + */ |
| 76 | + public Color flip() { |
| 77 | + return opponentColor; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns a character to use for a String representation of the field.<br> |
| 82 | + * It accepts ChesslyColor.BLACK (X), ChesslyColor.WHITE (O), ChesslyColor.EMPTY (-) otherwise |
| 83 | + * returns |
| 84 | + * an empty character. |
| 85 | + * @return char - one of 'b', '-', 'w' or ' ' |
| 86 | + */ |
| 87 | + public char toChar() { |
| 88 | + return shortName; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Convenience method to check if the instance is BLACK |
| 93 | + * @return true if black |
| 94 | + */ |
| 95 | + public boolean isBlack() { |
| 96 | + return this == Black; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Convenience method to check if the instance is WHITE |
| 101 | + * @return true if white |
| 102 | + */ |
| 103 | + public boolean isWhite() { |
| 104 | + return this == White; |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
0 commit comments