I'm currently trying to solve a code wars problem titled Battle ships: Sunk damaged or not touched?. Given a 2D array containing 'ships' and another 2D array containing the attack coordinates I have to generate a score. I populate a hashmap with the ship locations and then check the attack locations against the map. When printed, the original locations and attacks are identical on the first test case. Despite this, map.get()
continues to return null and map.containsKey()
returns false.
public class BattleshipsSDN {
public static Map<String,Double> damagedOrSunk(final int[][] board, final int[][] attacks) {
int y;
HashMap<int[], Integer> ships = new HashMap<>();
// build map of boat locations
for (int i = 0; i < board.length; i++) {
y = board.length - i;
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 0) continue;
else {
int[] location = {j+1,y};
ships.put(location, board[i][j]);
System.out.println("Location: "+location[0]+","+location[1]);
//System.out.println("Value: "+ships.get(location));
}
}
}
//establish original boat lengths
int ship1 = Collections.frequency(new ArrayList<Integer>(ships.values()), 1);
int ship2 = Collections.frequency(new ArrayList<Integer>(ships.values()), 2);
int ship3 = Collections.frequency(new ArrayList<Integer>(ships.values()), 3);
System.out.println("Ships: "+ship1+ship2+ship3);
for(int[] x : ships.keySet()) System.out.println(x[0]+","+x[1]);
//check for hits
for (int[] x : attacks) {
System.out.println(x[0]+","+x[1]);
if (ships.get(x) == null) continue;
else{
System.out.println("Hit");
ships.remove(x);
}
}
double sunk = 0;
double hit = 0;
double missed = 0;
//find number of ship spots after attacks
int leftShip1 = Collections.frequency(new ArrayList<Integer>(ships.values()), 1);
int leftShip2 = Collections.frequency(new ArrayList<Integer>(ships.values()), 2);
int leftShip3 = Collections.frequency(new ArrayList<Integer>(ships.values()), 3);
System.out.println("Ships: "+leftShip1);
if (ship1 > 0) {
if (leftShip1 == 0) sunk++;
else if (ship1 % leftShip1 > 0) hit++;
else if (ship1 == leftShip1) missed++;
}
if (ship2 > 0) {
if (leftShip2 == 0) sunk++;
else if (ship2 % leftShip2 > 0) hit++;
else if (ship2 == leftShip2) missed++;
}
if (ship3 > 0) {
if (leftShip3 == 0) sunk++;
else if (ship3 % leftShip3 > 0) hit ++;
else if (ship3 == leftShip3) missed++;
}
HashMap<String, Double> score = new HashMap<>();
score.put("sunk", sunk);
score.put("damaged", hit);
score.put("notTouched", missed);
score.put("points", sunk + hit/2 - missed);
return score;
}
}
I'm not asking you to solve the problem for me. I'm just completely stumped as to why my HashMap is acting this way. Which likely means it's some really small stupid thing.
Note: The y values of the locations are flipped because in the problems 'board' the y-coord is measured from the bottom. So in a 4x4 board or array the index [0][0] corresponds to the coordinates (1,4)