package Battleship; import java.util.ArrayList; public class Ship { public enum Direction {NORTH, EAST, WEST, SOUTH}; private Direction direction; private int length; private Integer x,y; private boolean[] hit; public Ship(Direction direction, int length, int x, int y) { this.x = x; this.y = y; this.length = length; this.direction = direction; hit = new boolean[length]; } public Ship() { } public Direction getDirection() { return direction; } public void setDirection(Direction direction) { this.direction = direction; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public ArrayList getCoordinates() { ArrayList coords = new ArrayList(); if(direction == Direction.NORTH) { for (int i = 0; i < length; i++) { coords.add(new Coordinate(x-i,y)); } } else if(direction == Direction.SOUTH) { for (int i = 0; i < length; i++) { coords.add(new Coordinate(x+i,y)); } } else if(direction == Direction.EAST) { for (int i = 0; i < length; i++) { coords.add(new Coordinate(x,y+i)); } } else if(direction == Direction.WEST) { for (int i = 0; i < length; i++) { coords.add(new Coordinate(x,y-i)); } } return coords; } public boolean occupiesCoordinate(Coordinate coord) { for (Coordinate c : getCoordinates()) { if(c.getX() == coord.getX() && c.getY() == coord.getY()) return true; } return false; } public boolean hit(int x, int y) { for (int i = 0; i < length; i++) { if((getCoordinates().get(i).getY() == y)&&(getCoordinates().get(i).getX() == x)) { hit[i] = true; return true; } } return false; } public void resetHitTable() { for (int i = 0; i < hit.length; i++) { hit[i] = false; } hit = new boolean[length]; } public boolean sunken() { for (int i = 0; i < hit.length; i++) { if(hit[i] == false) return false; } return true; } }