package navale;

import java.io.Serializable;

/*
 * Ne pas oublier d'écrire les méthodes déclarées dans Bateau, puisque Croiseur
 * implémente Bateau.
 */
//implements Serializable pour Exercice 6
public class Croiseur implements Bateau, Serializable {
	private static final long serialVersionUID = 1L;
	/*
	 * Un croiseur doit connaître son emplacement,
	 * savoir s'il est ehorizontal.
	 * Il faut en plus savoir si chaque portion est touchee ou non;
	 * on choisit ici d'utiliser un tableau de trois booléens.
	 * Inutile de stocker si le croiseur est coulé, il suffit de vérifier le tableau.
	 */
	private int x;
	private int y;
	private boolean horizontal;
	private boolean[] touche;
	
	public Croiseur(int x, int y,boolean horizontal) {
		this.horizontal = horizontal;
		this.x = x;
		this.y = y;
		touche = new boolean[3];
	}

	/**
	 * Fonction privée qui permet à partir d'une case (x,y) de récupérer la portion du
	 * bateau présent dans cette case.
	 * @param x
	 * @param y
	 * @return la portion du croiseur present en (x,y), numerotee de 0 a 2;
	 * -1 s'il n'y est pas
	 */
	private int portion(int x, int y){
		int d=4;
		if(horizontal && y==this.y){
			d = x-this.x;
		}
		if(!horizontal && x==this.x){
			d = y-this.y;
		}
		if(d>=-1 && d<=1) return d+1;
		return -1;
	}
	
	
	@Override
	public boolean coup(int x, int y) {
		int p=portion(x,y);
		if(p>=0){
			touche[p] = true;
			return true;
		}
		return false;
	}

	@Override
	public boolean estCoule() {
		for(int i=0;i<3;i++)
			if(!touche[i])
				return false;
		return true;
	}

	@Override
	public boolean estPresent(int x, int y) {
		return portion(x,y)>=0;
	}

	@Override
	public int getX() {
		return x;
	}

	@Override
	public int getY() {
		return y;
	}

	public boolean estHorizontal() {
		return horizontal;
	}
	
	public boolean deplace(int d){
		if(d!=-1 && d!=1)
			return false;
		if(horizontal)
			x+=d;
		else
			y+=d;
		return true;
	}
}
