import java.awt.*;
import java.util.*;

public class CD_GRID extends GENERIC_GRID{

  int cnt;    
  CD_CELL player;
  CD_CELL[] targets = new CD_CELL[33];
  int[] map = new int[126];
  int row = 0;  // col also equals 0 (see GENERIC_GRID)
  int miss = 0;

  CD_GRID(){
    super();
    col = 0;
    player = new CD_CELL( 0, 0 , new Color(175,250,0));
    for(int i=0; i<map.length; i++){
      map[i]=0;
    }
    map[0]=2;
    Random r = new Random();
    int n = 1;
    int loc = 0;
    for(int i=0; i<targets.length; i++){
      n = Math.abs(r.nextInt()%4)+1+n; //next number at least 1 
                                       //larger than last number
      do{
        loc = Math.abs(r.nextInt()%map.length);
      }while(map[loc]!=0 || loc==0);  // no 2 cells in same spot
      targets[i]=new CD_CELL(n, loc ,Color.yellow);  
      map[loc]=n;
    }
  }

  public void move_CD(int m){
    if(col>0 && m==-1){
        col--;
        player.moveX(m);
    }
    else if(col<8 && m==1){
        col++;
        player.moveX(m);
    }
    else if(row>0 && m==2){
        row--;
        player.moveUp();
    }
    else if(row<13 && m==0){
        row++;
        player.moveDown();
    }
    collision_check();
  }

  public boolean keepPlaying(){
    if(cnt<33) return true;
    else return false;
  }

  public void collision_check(){
    int ploc = row*9+col;
    if(map[ploc]>0 && ploc != 0 ){
      if( map[ploc] == targets[cnt].getValue() ){
        targets[cnt].setValue(-1);
        map[ploc]=-1;
        cnt++;
      }
      else{
        miss++;
      }
    }
  }

  public void drawTargets(Graphics g){
    int ploc=row*9+col;
    for(int i=0; i<targets.length; i++){
      if(ploc==targets[i].getLocation() && map[ploc]>0) targets[i].draw(g,true);
      else  targets[i].draw(g,false);
    } 
  }

  public void draw(Graphics g){
    player.draw(g,false);
    drawTargets(g);
    g.setColor(Color.black);
    g.drawRect(50,10,180,215);
  }
}

