class RowGenerator {
  public static final int emptyRow[] =
  new int[] { Mastermind.COLOR_NONE, Mastermind.COLOR_NONE,
                Mastermind.COLOR_NONE, Mastermind.COLOR_NONE,
                Mastermind.COLOR_NONE };

  int row[] = new int[5];
  boolean filled[] = new boolean[5];
  boolean firstDone = false;

  public RowGenerator() {
    this.init();
  }

  public synchronized void init() {
    this.init(RowGenerator.emptyRow);
  }

  public synchronized void init (int row[]) {
    for (int i = 0; i < 5; ++i) {
      int thisColumn = row[i];
      this.row[i] = thisColumn;
      this.filled[i] = thisColumn != Mastermind.COLOR_NONE;
    }
    this.firstDone = false;
  }

  public synchronized int[] getNext() {
    int i;
    if (! this.firstDone) {
      for (i = 0; i < 5; ++i)
        if (! this.filled[i]) this.row[i] = 0;
      this.firstDone = true;
      return this.row;
    }
    i = 5;
    while (i-- > 0 && (this.filled[i] || this.row[i] == 7))
      ;
    if (i < 0) return null;
    this.row[i++]++;
    for (; i < 5; ++i)
      if (! this.filled[i]) this.row[i] = 0;
    return this.row;
  }

  public static void main(String args[]) {
    int count = 0;
    RowGenerator rg = new RowGenerator();
    rg.init(new int[] {-1, 2,3, -1, 6});
    int res[];
    while ((res = rg.getNext()) != null) {
      for (int i = 0; i < 5; ++i) System.out.print(res[i]);
      System.out.println();
      ++count;
    }
    System.out.println("Count: " + count);
  }
}

