Mostly cosmetic changes to color code

This commit is contained in:
elcnesh
2015-05-06 16:26:45 +00:00
parent fd4f55d9df
commit 658fd8f953
15 changed files with 123 additions and 143 deletions

View File

@@ -42,21 +42,11 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
private final byte myColor;
private final float orderWeight;
private static final ColorSet[] allColors = new ColorSet[32];
private static final ColorSet noColor = new ColorSet();
private static final ColorSet[] cache = new ColorSet[32];
// TODO: some cards state "CardName is %color%" (e.g. pacts of...) - fix
// this later
/**
* Instantiates a new card color.
*
* @param mana
* the mana
*/
private ColorSet() {
myColor = 0;
orderWeight = -1;
}
private static final byte ALL_COLORS_MASK = MagicColor.WHITE | MagicColor.BLUE | MagicColor.BLACK | MagicColor.RED | MagicColor.GREEN;
public static final ColorSet ALL_COLORS = fromMask(ALL_COLORS_MASK);
private static final ColorSet NO_COLORS = fromMask(MagicColor.COLORLESS);
private ColorSet(final byte mask) {
this.myColor = mask;
@@ -64,11 +54,11 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
}
public static ColorSet fromMask(final int mask) {
final int mask32 = mask & MagicColor.ALL_COLORS;
if (allColors[mask32] == null) {
allColors[mask32] = new ColorSet((byte) mask);
final int mask32 = mask & ALL_COLORS_MASK;
if (cache[mask32] == null) {
cache[mask32] = new ColorSet((byte) mask32);
}
return allColors[mask32];
return cache[mask32];
}
public static ColorSet fromNames(final String... colors) {
@@ -113,6 +103,11 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
return (this.myColor & colormask) == colormask;
}
/** this has no other colors except defined by operand. */
public boolean hasNoColorsExcept(final ColorSet other) {
return hasNoColorsExcept(other.getColor());
}
/** this has no other colors except defined by operand. */
public boolean hasNoColorsExcept(final int colormask) {
return (this.myColor & ~colormask) == 0;
@@ -139,7 +134,7 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
*
* @return the order weight
*/
public float getOrderWeight() {
private float getOrderWeight() {
float res = this.countColors();
if(hasWhite()) {
res += 0.0005f;
@@ -255,7 +250,7 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
public ColorSet inverse() {
byte mask = this.myColor;
mask ^= MagicColor.ALL_COLORS;
mask ^= ALL_COLORS_MASK;
return fromMask(mask);
}
@@ -286,7 +281,7 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
* @return the nullColor
*/
public static ColorSet getNullColor() {
return noColor;
return NO_COLORS;
}
/**
@@ -299,8 +294,12 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
return (this.myColor & ccOther.myColor) != 0;
}
public ColorSet getSharedColors(final ColorSet ccOther) {
return fromMask(getColor() & ccOther.getColor());
}
public ColorSet getOffColors(final ColorSet ccOther) {
return ColorSet.fromMask(~this.myColor & ccOther.myColor);
return fromMask(~this.myColor & ccOther.myColor);
}
@Override
@@ -313,8 +312,8 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
private int getIndexOfNextColor(){
int nextBit = currentBit + 1;
while(nextBit < MagicColor.NUMBER_OR_COLORS) {
if((myColor & MagicColor.WUBRG[nextBit]) != 0) {
while (nextBit < MagicColor.NUMBER_OR_COLORS) {
if ((myColor & MagicColor.WUBRG[nextBit]) != 0) {
break;
}
nextBit++;

View File

@@ -6,16 +6,15 @@ import com.google.common.collect.ImmutableMap;
/**
* Holds byte values for each color magic has.
*/
public class MagicColor {
public final class MagicColor {
public static final byte COLORLESS = 0;
public static final byte WHITE = 1 << 0;
public static final byte BLUE = 1 << 1;
public static final byte BLACK = 1 << 2;
public static final byte RED = 1 << 3;
public static final byte GREEN = 1 << 4;
public static final byte WHITE = 1 << 0;
public static final byte BLUE = 1 << 1;
public static final byte BLACK = 1 << 2;
public static final byte RED = 1 << 3;
public static final byte GREEN = 1 << 4;
public static final byte ALL_COLORS = WHITE | BLUE | BLACK | RED | GREEN;
public static final int NUMBER_OR_COLORS = 5;
public static final byte[] WUBRG = new byte[] { WHITE, BLUE, BLACK, RED, GREEN };
@@ -37,30 +36,19 @@ public class MagicColor {
s = s.toLowerCase();
if (s.length() == 1) {
switch (s) {
case "w":
return MagicColor.WHITE;
case "u":
return MagicColor.BLUE;
case "b":
return MagicColor.BLACK;
case "r":
return MagicColor.RED;
case "g":
return MagicColor.GREEN;
case "w": return MagicColor.WHITE;
case "u": return MagicColor.BLUE;
case "b": return MagicColor.BLACK;
case "r": return MagicColor.RED;
case "g": return MagicColor.GREEN;
}
}
else {
} else {
switch (s) {
case Constant.WHITE:
return MagicColor.WHITE;
case Constant.BLUE:
return MagicColor.BLUE;
case Constant.BLACK:
return MagicColor.BLACK;
case Constant.RED:
return MagicColor.RED;
case Constant.GREEN:
return MagicColor.GREEN;
case Constant.WHITE: return MagicColor.WHITE;
case Constant.BLUE: return MagicColor.BLUE;
case Constant.BLACK: return MagicColor.BLACK;
case Constant.RED: return MagicColor.RED;
case Constant.GREEN: return MagicColor.GREEN;
}
}
return 0; // colorless
@@ -68,39 +56,41 @@ public class MagicColor {
public static byte fromName(final char c) {
switch (Character.toLowerCase(c)) {
case 'w': return MagicColor.WHITE;
case 'u': return MagicColor.BLUE;
case 'b': return MagicColor.BLACK;
case 'r': return MagicColor.RED;
case 'g': return MagicColor.GREEN;
case 'w': return MagicColor.WHITE;
case 'u': return MagicColor.BLUE;
case 'b': return MagicColor.BLACK;
case 'r': return MagicColor.RED;
case 'g': return MagicColor.GREEN;
}
return 0; // unknown means 'colorless'
}
public static String toShortString(final String color) {
if (color.equalsIgnoreCase(Constant.SNOW)) { return "S"; } // compatibility
if (color.equalsIgnoreCase(Constant.SNOW)) {
return "S";
} // compatibility
return toShortString(fromName(color));
}
public static String toShortString(final byte color) {
switch (color){
case WHITE: return "W";
case BLUE: return "U";
case BLACK: return "B";
case RED: return "R";
case GREEN: return "G";
default: return "1";
case WHITE: return "W";
case BLUE: return "U";
case BLACK: return "B";
case RED: return "R";
case GREEN: return "G";
default: return "1";
}
}
public static String toLongString(final byte color) {
switch (color){
case WHITE: return Constant.WHITE;
case BLUE: return Constant.BLUE;
case BLACK: return Constant.BLACK;
case RED: return Constant.RED;
case GREEN: return Constant.GREEN ;
default: return Constant.COLORLESS;
case WHITE: return Constant.WHITE;
case BLUE: return Constant.BLUE;
case BLACK: return Constant.BLACK;
case RED: return Constant.RED;
case GREEN: return Constant.GREEN ;
default: return Constant.COLORLESS;
}
}