/**
 * Boat.java
 *
 *
 * Caitlin Ross 
 * 
 * 100735219
 */

public class Boat {
	
	private int boatType;		//0:Battleship, 1:Destroyer, 2:Cruiser, 3:Tugboat
	private String name;
	private int boatLength;
	private BattleshipGame game;
	private int startingRow, startingColumn;
	private char shipDirection;
	
    public Boat(int aType, BattleshipGame aGame) {
    	game = aGame;
    	boatType = aType;
    	
    	if(boatType == 0)
    		createBattleship();
    	else if (boatType == 1)
    		createDestroyer();
    	else if (boatType == 2)
    		createCruiser();
    	else if (boatType == 3)
    		createTugboat();
    }
    
    private void createBattleship(){
    	name = "Battleship";
    	boatLength = 5;
    }
    
    private void createDestroyer(){
    	name = "Destroyer";
    	boatLength = 4;
    }
    
    private void createCruiser(){
    	name = "Cruiser";
    	boatLength = 3;
    }
    
    private void createTugboat(){
    	name = "Tugboat";
    	boatLength = 2;
    }
    
    public String getName(){return name;}
    
    public boolean checkLocation(int rCoordinate, int cCoordinate, char direction, int whichBoard){
    	if ((direction == 'h' || direction == 'H') && cCoordinate+boatLength<=game.getBoard(whichBoard).getSize()){
    		for(int i=0; i<boatLength; i++){
    			if(game.getBoard(whichBoard).getPiece(rCoordinate, cCoordinate + i) != '~')
    				return false;
    		}
    		return true;
    	}
    	if ((direction == 'v' || direction == 'V') && rCoordinate+boatLength<=game.getBoard(whichBoard).getSize()){
    		for(int i=0; i<boatLength; i++){
    			if(game.getBoard(whichBoard).getPiece(rCoordinate + i, cCoordinate) != '~')
    				return false;
    		}
    		return true;
    	}
    	return false;
    }
    
    public void setLocation(int rCoordinate, int cCoordinate, char direction, int whichBoard){
    	startingRow = rCoordinate;
    	startingColumn = cCoordinate;
    	shipDirection = direction;
    	
    	if (direction == 'h' || direction == 'H'){
    		for(int i=0; i<boatLength; i++)
    			game.getBoard(whichBoard).setPiece(rCoordinate, cCoordinate + i, name.charAt(0));
    	}
    	else if (direction == 'v' || direction == 'V'){
    		for(int i=0; i<boatLength; i++)
    			game.getBoard(whichBoard).setPiece(rCoordinate + i, cCoordinate, name.charAt(0));
    	}
    }
    
    public boolean isBoatSunk(int whichPlayer){
    	if(shipDirection == 'h' || shipDirection == 'H'){
    		for(int i=0; i<boatLength; i++){
    			if(!game.getBoard(whichPlayer).isPieceHit(startingRow, startingColumn + i))
    				return false;
    		}
    		return true;
    	}
    	else if(shipDirection == 'v' || shipDirection == 'V'){
    		for(int i=0; i<boatLength; i++){
    			if(!game.getBoard(whichPlayer).isPieceHit(startingRow + i, startingColumn))
    				return false;
    		}
    		return true;
    	}
    	else
    		return false;
    }
    
    public void sinkShip(){
    	for (int i=0; i<boatLength; i++){
    		if (shipDirection == 'v' || shipDirection == 'V')
    			game.getBoard(game.enemyNum()).sinkPiece(startingRow+i, startingColumn);
    		else if (shipDirection == 'h' || shipDirection == 'H')
    			game.getBoard(game.enemyNum()).sinkPiece(startingRow, startingColumn+i);
    	}
    }
    
}