import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CARDS extends Applet{
  Image p[];
  Image offI;
  int values[];
  int score, answer, hands;
  String input = "";
  String status = "";
  String prevCards[];
  boolean game_over, rep, check;
  int pgoal = 1000;
  Dimension d;

  public void init(){
    setBackground(Color.yellow);
    p=new Image[5];
    values = new int[5];
    prevCards=new String[5];
    d = getSize();
    offI=createImage(d.width,d.height);
    setValues();
    requestFocus();
    this.addKeyListener(new KeyAdapter(){
      public void keyPressed(KeyEvent k) {
       if(!game_over){
        if(k.getKeyCode() == KeyEvent.VK_ENTER){
          if(check) score();
          else setValues();
        }
        else if(k.getKeyCode() == KeyEvent.VK_BACK_SPACE ||
                k.getKeyCode() == KeyEvent.VK_DELETE){
          input = input.substring(0,input.length()-1);
        }
        else{
         input += k.getKeyChar();
        }
       }
       else{
         reset();
       }
       repaint();
      }
    });
  }

  public void score(){
    if(input.equals(""+answer)) {
      if(answer>2000){
         score+=2000;
         status="Answer: " + answer + ", MAX POINTS: 2000";
      }
      else {
        score+=answer;
        status = "Press enter to continue";
      }
    }
    else{
       score-=1;
       status = "WRONG. ANSWER: " + answer;
    }
    check=false;

    hands++;
    if(score>10000){
      game_over=true;
      status="Press any key to play again!";
    }
  }

  public void reset(){
    setValues();
    score=0;
    hands=0;
    game_over=false;
  }

  public void setValues(){
    answer = 0;
    setCards();
    int[] v_array = new int[14]; //no value stored at zero
    for(int i = 0; i<v_array.length; i++) v_array[i]=0;
    int temp_ans = 0;
    for(int i = 0; i<values.length; i++){
      v_array[values[i]]++;
    }
    for(int i = 0; i<v_array.length; i++){
      if(v_array[i]<2) answer += i*v_array[i];
      else answer += Math.pow(i,v_array[i]);
    }
    input="";
    check=true;
    status = "Enter value.";
  }
 
  public void setCards(){
   int val = 0;
   int currVal=0;
   for(int c=0; c<prevCards.length; c++)  prevCards[c]=null;
   for(int i = 0; i<p.length; i++){
    String card_value="";
    do {
      Random r = new Random();
      int suit= Math.abs(r.nextInt())%4;
      val= Math.abs(r.nextInt())%13;
      String st="";  
      String vl="";
      //identify suit:
      switch(suit){
        case 0:  st="S"; break;
        case 1:  st="C"; break;
        case 2:  st="H"; break;
        case 3:  st="D"; break;
      }
      //identify value
      switch(val){
        case 0: vl="A"; currVal=1; break;
        case 1: vl="2"; currVal=2;  break;
        case 2: vl="3"; currVal=3;  break;
        case 3: vl="4"; currVal=4;  break;
        case 4: vl="5"; currVal=5;  break;
        case 5: vl="6"; currVal=6;  break;
        case 6: vl="7"; currVal=7;  break;
        case 7: vl="8"; currVal=8;  break;
        case 8: vl="9"; currVal=9;  break;
        case 9: vl="T"; currVal=10; break;
        case 10: vl="J"; currVal=11; break;
        case 11: vl="Q"; currVal=12; break;
        case 12: vl="K"; currVal=13; break;
      }
      card_value="cards/"+vl+st+".gif";
      rep=false;
      for(int c=0; c<prevCards.length; c++) {
        if(card_value.equals(prevCards[c])) rep=true;
      }
    } while(rep); 
    prevCards[i]=card_value;
    p[i]=getImage(getDocumentBase(), card_value);
    values[i]=currVal;
   }
  }

  public void update(Graphics g){
    paint(g);
  }

  public void paint(Graphics g){
    Graphics offG = offI.getGraphics();
    offG.setColor(Color.yellow);
    offG.fillRect(0,0,d.width,d.height);
    offG.setColor(Color.black);
    offG.setFont(new Font("Courier", Font.BOLD, 36));
    offG.drawString("C A R D - V A L U E", 50, 35);
    offG.setFont(new Font("Courier", Font.BOLD, 18));
    offG.drawString(status, 10, 60);
    offG.drawString("INPUT: " + input, 20, 170);
    offG.drawString("ToTaL VaLuE: " + score, 190, 170);
    offG.drawString("HaNdS: " + hands, 430, 170);
    if(!game_over){
      for(int c=0; c<p.length; c++) {
        offG.drawImage(p[c], 50+c*50,80,this);
      }
    }
    else{
      offG.setFont(new Font("Courier", Font.BOLD, 48));
      offG.drawString("WIN!", 120, 150);
    }
    g.drawImage(offI,0,0,this); 
    offG.dispose(); 
  }
}
