Comparing ANSI Strings in Java

I am creating a Battleship game. The game board is shown in the following image:

enter image description here

For the tests, I am looking at tile (1,1) which is the solid blue tile in the top right. The solid blue tiles have the same background and foreground colors, containing a hidden “~”. The other type of tile also has the same background color, but a different foreground color, still containing the “~”.

I need a method to determine if the selected tile contains a tilde or not. For example, if the player hits an enemy ship, the tile could initially be marked with an “S” so the computer knows a ship was hit and not the water. I am having difficulty finding a way to determine if two colored strings are equal to each other.

Here is an example 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\]

To determine if two colored strings are equal to each other, you can remove the color codes from the strings using regular expressions before comparing them. Here’s an example:

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+"~";

// Remove color codes from Tile1 and Tile2 using regular expressions
String CleanTile1 = Tile1.replaceAll("\u001B\\[[;\\d]*m", "");
String CleanTile2 = Tile2.replaceAll("\u001B\\[[;\\d]*m", "");

if (CleanTile1.equals(CleanTile2)){
    System.out.println(Clear+"Correct");
}
else{
    System.out.println(Clear+"Incorrect");
}

This will output “Correct” because the two tiles are identical after removing the color codes.