import isdraw.*;
import geometry.*;
import colors.*;
import java.util.*;

class Mario2 extends World{
  ArrayList<Integer> obstacles = new ArrayList<Integer>();
  int mario; // Mario's height - horizontal is always in the middle
  int COUNTER; // the tick counter
  int tunelength = 33 * 4;
  int tickCount = 0;

  Mario2(){
    this.initWorld();
    this.mario = 50;
    this.COUNTER = 0;
  }

  void initWorld(){
    for (int i = 1; i <= 20; i++){
      this.obstacles.add(randomInt(150));
    }
  }

  /** helper method to generate a random number in the range 0 to n */
  int randomInt(int n){
    return new Random().nextInt(n);
  }

  void moveObstacles(){
    this.obstacles.remove(0);
    this.obstacles.add(randomInt(150));
  }

  // does Mario collide with the middle obstacle?
  boolean collide(){
    return this.mario <= this.obstacles.get(10);   
  }

  // move the queue, check for collision, play the tune
  // Mario falls down slowly
  public void onTick(){
    this.moveObstacles();
    this.COUNTER = this.COUNTER + 1;
    this.tickTunes.add(CHOIR, jeopardyNotes[this.tickCount]);
    this.tickCount = (this.tickCount + 1) % this.tunelength;
    //if (collide())
    //  this.endOfWorld("Good Bye"); 
    this.mario = this.mario + 3;
  }

  public void onKeyEvent(String ke){
    if (ke.equals("up")){
      this.mario = this.mario - 10;
      this.keyTunes.add(BIRD_TWEET, noteG);
    }
  }

  public void draw(){
    drawObstacles(this.theCanvas);
    this.theCanvas.drawDisk(new Posn(100, this.mario), 10,
        new Red());
  }

  void drawObstacles(Canvas c){
    for (int i = 0; i < 20; i++){
      c.drawRect(new Posn(i * 10, 200 - this.obstacles.get(i)),
          20, this.obstacles.get(i), new Black());
    }
  }

  public static int jeopardyNotes[] = {
    0,0,0,0,
    noteG,0,0,0,noteUpC,0,0,0,noteG,0,0,0,noteC,0,0,0,
    noteG,0,0,0,noteUpC,0,0,0,noteG,0,0,0,0,0,0,0,
    noteG,0,0,0,noteUpC,0,0,0,noteG,0,0,0,noteUpC,0,0,0,
    noteUpE,0,0,0,0,0,noteUpD,0,noteUpC,0,noteB,0,noteA,0,noteG,0,
    noteG,0,0,0,noteUpC,0,0,0,noteG,0,0,0,noteC,0,0,0,
    noteG,0,0,0,noteUpC,0,0,0,noteG,0,0,0,0,0,0,0,
    noteUpC,0,0,0,0,0,noteA,0,noteG,0,0,0,noteF,0,0,0,noteE,0,0,0,noteD,0,0,0,noteC,0,0,0,
    0,0,0,0
  };


  public static void main(String[] arv){
    Mario2 world = new Mario2();
    world.bigBang(200, 200, 0.1);
  }
}