I am currently Making a battleship
This is an image of the game board:
For the tests we are looking at tile (1,1) Which is the solid blue tile in the top right. The Solid Blue have the background and foreground colors identical, containing a hidden "~", the other type of tile contains the same background color, but different foreground color, still containing the "~".
I need a method to determine weather or not the tile selected contains a tilde or not, for example: if the player hits an enemy Ship that tile could initially be marked with an "S" on the other end, so the computer will know that a ship was hit and not the water. However, I cannot find a way to see if two colored strings are equal to each other.
Here is an example set of code creating two tiles that are identical (With different Colors), but failing to recognize it:
class Test{
public static void main(String[] args){
String Blank_Tile_Color = (char)27+"[34;44m";
String Tile_Color = (char)27+"[36;44m";
String Clear = (char)27+"[0m";
String Tile1 = Tile_Color+"~";
String Tile2 = Blank_Tile_Color+"~";
System.out.println(Tile1);
System.out.println(Tile2);
if (Tile1.equals(Tile2)){
System.out.println(Clear+"Correct");
}
else{
System.out.println(Clear+"Incorrect");
}
}
}
[Output: Incorrect]
Honestly speaking, I'm not too confident. But probably you are looking for "contains" method? Like:
String blankTileColor = (char) 27 + "[34;44m";
String tileColor = (char) 27 + "[36;44m";
String clear = (char) 27 + "[0m";
String tile1 = tileColor + "~";
String tile2 = blankTileColor + "~";
String tile3 = clear;
System.out.println(tile1.contains("~")); // true
System.out.println(tile2.contains("~")); // true
System.out.println(tile3.contains("~")); // false
However, if this game is not a simple "sandbox" I'd really recommend think on @MadProgrammer's comment.