package iad;

import iad.*;

public class Labyrinthe {
	/**
	 *Atributs privés
	 */
	private int largeur;
	private int longueur;
	private Cellule[] cellule;
	/**
	 *Constructeur 
	 *@param largeur et longueur
	 */
	public Labyrinthe(int largeur, int longueur) {
		this.largeur = largeur;
		this.longueur = longueur;
		this.cellule = new Cellule[largeur*longueur];
		for (int i=0; i<largeur*longueur; i++) {
			cellule[i] = new Cellule(i);
		}
	}
	/**
	 *Retourne la largeur du labyrinthe
	 *@return largeur
	 */
	 public int getLargeur() {
	 	return this.largeur;
	}
	/**
	 *Retourne la longueur du labyrinthe
	 *@return longueur
	 */
	 public int getLongueur() {
	 	return this.longueur;
	}
	/**
	 *Retourne une cellule du labyrinthe
	 *@param positionX, positionY
	 *@return cellule de position (X,Y)
	 */
	 public Cellule getCellule(int x, int y) {
	 	
	 	return this.cellule[x+largeur*y];
	}
	/**
	 *Crée une représentation du labyrinthe
	 */
	public String toString() {
		int i,j;
		String s = "";
		
		// Crée les murs extérieurs Nord
		for (i=0; i<this.largeur; i++) {
			s = s.concat("**");
		}
		s = s.concat("*\n*");
		
		// Crée l'intérieur du labyrinthe
		for (j=0; j<this.longueur; j++) {
			for (i=0; i<this.largeur-1; i++) {
				int k = this.getCellule(i,j).getEtat();
				if ((k != 4) && (k != 5) && (k != 7)) {
					s = s.concat(" *");
				}
				else {
					s = s.concat("  ");
				}
			}
			if (j==0) { s = s.concat(" \n*"); }
			else { s = s.concat(" *\n*"); }
			
			if (j<this.longueur-1){
				for (i=0; i<this.largeur; i++) {
					int l = this.getCellule(i,j).getEtat();
					if ((l != 1) && (l != 5) && (l != 13)) {
						s = s.concat("**");
					}
					else {
						s = s.concat(" *");
					}
				}	
				if (j != this.longueur-2) { s = s.concat(" \n*"); }
				else { s = s.concat("\n "); }
			}
		}
		
		// Crée les murs extérieurs Sud
		for (i=0; i<this.largeur; i++) {
			s = s.concat("**");
		}
		s = s.concat("\n");
		return s;
	}
			 
}