-1

I am currently Making a battleship

This is an image of the game board:

enter image description here

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]

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 1
    I am a little bit confused. Do you expect the output to be `"Correct"`? – Turing85 Jan 05 '20 at 23:48
  • 6
    As general recommendation, don't. You shouldn't be relying on the presentation for your decision making, but relying on the "state" of the data model. The presentation should be a "user friendly" representation of the data model – MadProgrammer Jan 05 '20 at 23:49
  • I just used that word as an example, it checks to see if they are identical and if they are it will print out correct. – Slayer Of Whales Jan 05 '20 at 23:49

1 Answers1

0

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.

Makhno
  • 441
  • 2
  • 6
  • From OP's question: "*[...] The Solid Blue have the background and foreground colors identical, **containing a hidden "~"** [...]*". – Turing85 Jan 06 '20 at 00:19
  • Works perfectly! I had no idea that the .contains() method could be used on anything other than Array-lists as well. Thanks for a solution that allows me to keep the game-board aesthetic, and sharing a new piece of knowledge with me! – Slayer Of Whales Jan 06 '20 at 00:33