Having explicit names for the
Color objects is a problem.
Color Emerald = new Color(231, 223, 134);
The name should certainly be descriptive, but what if you change the values of the channels of the color at some point? You should rename the variable to reflect the new value, but nothing enforces that.
These names are great for constants like the
public static members of the
Color class, but not so much in your situation, because the values are subject to change.
But I'm getting ahead of myself, let's start at the beginning.
know your data
Start with modelling the data for your problem in a convenient way.
Its a 2048 clone and that string is going to be one of the numbers like
2, 4, 8, 16, 32
Sure enough, that works, but as @h.j.k. pointed out:
since this is a 2048 clone, why not just operate on powers of two? Instead of having i as 2, 4... 2048, why not have it as 1, 2... 11?
I think that's spot on. Just because you display numbers, doesn't mean that you should store them in exactly the same way. This is why they always say keep data and display separate.
If you want to model the numbers as their actual values or the powers of two that they are, either way: you want to use a data structure that makes it easy to associate a value with a color in both cases.
I mean just look at these numbers in the comments:
...3, 134); //2
.......04); // 4
......, 3);// 8
They are dying to be of actual use and not just documentation!
To associate the values (2, 4...) with their respective colors, a
HashMap is very useful. It looks like converting a value to a
static functionality, that's only based on the value and always gives the same output for the same value. It's more of a
static utility functionality than a non-static class member. Such a
ColorConverter class could look like that:
public class ColorConverter
private static final HashMap <Integer, Color> valueToColor;
private static final Color defaultColor;
defaultColor = Color.BLACK;
valueToColor = new HashMap <Integer, Color>();
valueToColor.put(2, new Color(231, 223, 134));
valueToColor.put(4, new Color(151, 206, 104));
valueToColor.put(8, new Color(240, 79, 3));
valueToColor.put(16, new Color(116, 116, 204));
valueToColor.put(32, new Color(89, 188, 251));
valueToColor.put(64, new Color(245, 213, 69));
valueToColor.put(128, new Color(46, 204, 113));
valueToColor.put(256, new Color(254, 198, 6));
valueToColor.put(512, new Color(151, 206, 104));
valueToColor.put(1024, new Color(136, 112, 255));
valueToColor.put(2048, new Color(255, 215, 0));
public static Color toColor(int value)
return valueToColor.containsKey(value) ? valueToColor.get(value) : defaultColor;
Adding a tiny
main method for testing
public static void main(String args)
yields this result when executed in the command line:
To associate the powers of 2 (1, 2,...) with their respective colors, you could also use a
HashMap or even an Array, if you include the the power 0, which corresponds to a value of 1, because the values are consecutive integers, which works well with an array that has indices that are consecutive integers. For brevity, I do not include the code for that, because it's quite similar to the above. If you need it, please leave a comment.