import java.util.*;

public class Plateau {
  public class Case {
    private int index;
    private Set<Pion> pions;
    public Case(int i) {
      index = i;
      pions = new HashSet<Pion>();
    }
    public Iterator<Pion> iterator() {
      return pions.iterator();
    }
    public void add(Pion p) {
      pions.add(p);
    }
    public void remove(Pion p) {
      pions.remove(p);
    }
    public boolean derniere() {
      return Plateau.this.cases.length-1==index;
    }
    public Case suivante(Mouvement m) {
      return Plateau.this.suivante(this,m);
    }
  } // fin classe Case
  private Case []cases;
  private Jeu jeu;
  public Plateau(Jeu j,int n) {
    cases = new Case[n];
    for (int i=0; i<n; i++) cases[i] = new Case(i);
    jeu = j;
  }
  private Case suivante(Case c,Mouvement m) {
    if (m.direction()==Mouvement.AVANT) {
      if (c.index<cases.length-1)
        return cases[c.index+1];
      else {
        m.inverse();
        return suivante(c,m);
      }
    }
    else {
      if (c.index>0)
        return cases[c.index-1];
      else
        return cases[c.index];
    }
  }
  public void add(Pion p) {
    p.deposeSurLaCase(cases[0]);
  }
  public Case derniereCase() {
    return cases[cases.length-1];
  }
  public String toString() {
    String s = "";
    for (int i=0; i<cases.length; i++) {
      int idx=0;
      Iterator<Pion> it = cases[i].iterator();
      while (it.hasNext()) {
        s += it.next().getNom();
        idx++;
      }
      for (;idx<jeu.nombreDeJoueurs(); idx++) s += ' ';
      s += '|';
    }
    return s;
  }
}