import java.util.ArrayList;

public class DotRaceList {
  private ArrayList<Dot> dotList;
  private int goalDistance;

  public DotRaceList(int maxStep, int distance) {
      this.dotList = new ArrayList<Dot>();
      
    this.dotList.add(new Dot("red", maxStep));
    this.dotList.add(new Dot("blue", maxStep));
    this.dotList.add(new Dot("green", maxStep));    
    this.dotList.add(new Dot("yellow", maxStep));
    this.dotList.add(new Dot("chartruese", maxStep));     
    this.goalDistance = distance;
    
  }
  
  private void step() {
    if (this.keepRacing()) {
            for (Dot nextDot : this.dotList) {
                nextDot.step();
            }
        }
        else {
            System.out.println("Stop hitting Step - the race is over!");
        }
    }
       
  private void showStatus() {
    for (Dot  nextDot : this.dotList) {
        nextDot.showPosition();
    }
    
//     if (this.getRedPosition() >= this.goalDistance && 
//         this.getBluePosition() < this.goalDistance &&
//         this.getGreenPosition() < this.goalDistance) {
//         System.out.println("RED wins");
//     }
//     else if (this.getBluePosition() >= this.goalDistance &&
//              this.getRedPosition() < this.goalDistance &&
//              this.getGreenPosition() < this.goalDistance) {
//         System.out.println("BLUE wins");
//     }
//     else if (this.getGreenPosition() >= this.goalDistance &&
//              this.getRedPosition() < this.goalDistance &&
//              this.getBluePosition() < this.goalDistance) {
//         System.out.println("GREEN wins");
//     }
//     else if (this.getGreenPosition() >= this.goalDistance ||
//         this.getBluePosition() >= this.goalDistance ||
//         this.getRedPosition() >= this.goalDistance) {
//         System.out.println("It's a tie!");
//     }
  }
  
  public void runRace() {
      this.reset();
      while ( this.keepRacing() ) {
          this.step();
          this.showStatus();
      }
  }
  
  private boolean keepRacing() {
      for (Dot nextDot : this.dotList) {
          if (nextDot.getPosition() >= this.goalDistance) {
              return false;
          }
      }
      return true;
  }
    
  private void reset() {
    for (Dot nextDot : this.dotList) {
        nextDot.reset();
    }
  }
}
