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

public class GENERIC_GRID{
    
  Vector enemy_vector= new Vector();
  GENERIC_CELL active_cell;
  GENERIC_CELL[] slots = new GENERIC_CELL[7];
  GENERIC_CELL[] targets = new GENERIC_CELL[9];
  int col;
  int score, cnt;
  boolean gameOn = true;  
  Color t_colors[] = { Color.black, Color.yellow, Color.yellow,
    Color.yellow, Color.cyan, Color.cyan, Color.magenta,
    Color.magenta, Color.black };

  GENERIC_GRID(){
    col = 3;
    score = 0;
    active_cell = new GENERIC_CELL(6, 110,25,Color.pink);
    for(int i=0; i<slots.length; i++){
      slots[i]=new GENERIC_CELL(0, 70+i*20, 195, new Color(175,250,0));  //invisible
    }
    for(int i=0; i<targets.length; i++){
      targets[i]=new GENERIC_CELL(i+9, 50+i*20,210,t_colors[i]);  
    }
  }

  public int getScore(){
    return score;
  }

  public void new_AC(){
    Random r = new Random();
    int v = Math.abs(r.nextInt()%8)+2;
    active_cell=new GENERIC_CELL(v,110,25,Color.pink);
    col=3;
    gameOn=true;
  }

  public void move_AC(int m){
    if(col>0 && m==-1){
        col--;
        active_cell.moveX(m);
    }
    else if(col<8 && m==1){
        col++;
        active_cell.moveX(m);
    }
  }

  public void advance_AC(){
    active_cell.moveDown();
    if(active_cell.y >= 195){
      if(col>0 && col<8){
        int ac = active_cell.getValue();
        int sval = slots[col-1].getValue();
        int tval = targets[col].getValue();
        if(sval==0){
          slots[col-1].setValue(ac);
        }
        else if(sval>0){
          if(sval + ac == tval){
            if(col<4) score++;
            else if(col<6) score+=2;
            else if(col<8) score+=3;
          }
          else score--;
          slots[col-1].setValue(0);
        }
      }
      cnt++;
      if(score<0) score=0;
      if(score>19) gameOn=false;
      else new_AC();
    }
  }

  public boolean keepPlaying(){
    return gameOn;
  }

  public void endGame(){
    gameOn=false;
  }

  public void drawSlots(Graphics g){
    for(int i=0; i<slots.length; i++){
      slots[i].draw(g);
    } 
  }

  public void drawTargets(Graphics g){
    for(int i=0; i<targets.length; i++){
      targets[i].draw(g);
    } 
  }

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

