//c2000 Hector Chang

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

public class Big2Applet extends Applet {
    private Button c, t, p;
    private int selected; //the clicked card's index
    private int playnum;  //number of cards selected by player
    private int Cardselected[] = new int[13]; //1=selected 0=unselected
    private int trick; //counter for players' turns
    private Big2card deck[] = Big2card.randomDeck(); //initial random deck
    private Big2card player[][] = new Big2card[4][52]; 
    //player[0][] represents the first player,
    //player[2][2] represents the third player's third card

    public void init() {
	super.init();
	this.setLayout(null);
	this.setBackground(new Color(255,255,204));
	c = new Button("deselect all");
	t = new Button("play"); //throw
	p = new Button("    pass    ");
	Label x = new Label("Big Two (C)2000 Hector Chang");
	//unselect cards
	c.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    for(int k=0; k<13; k++)
			Cardselected[k]=0;
		    repaint();
		}
	    }
			    );
	//validate play
	t.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    Big2card leading[] = new Big2card[1];
		    Big2card evaluate[] = collectHand();
		    int valid = isValid(evaluate, leading);
		    if(valid >0) {
			Big2card played[] = collectHand();
			discard();
			System.out.println("Strength of play: " + valid);
			System.out.println(" Leading card is: " + leading[0]);
			for(int p=0; p<played.length; p++)
			    System.out.print(played[p]);
			System.out.println();
		    }
		}
	    }
			    );
	c.setBounds(50, 270, 95, 30);
       	this.add(c);
	t.setBounds(155, 270, 185, 30);
	this.add(t);
	p.setBounds(350, 270, 95, 30);
	this.add(p);
       	x.setBounds(155, 300, 185, 20);
	this.add(x);
	for(int k = 0; k < 13; k++)
	    //initialize variable
	    Cardselected[k] = 0;
	for(int d = 0; d < deck.length; d++)
	    if (deck[d].value==3 && deck[d].suit==1)
		//determine player who goes first (gets the 3 of diamonds; 3$)
		trick = d%4;
	for (int s=0; s<13;s++) {
	    //deal the cards
	    player[0][s]=Big2card.deal(deck); 
	    player[1][s]=Big2card.deal(deck);
	    player[2][s]=Big2card.deal(deck); 
	    player[3][s]=Big2card.deal(deck);
	}
	arrangeAll();
	//determine where mouse clicked and change the Cardselected status.
	//afterwards, repaint the card area
	addMouseListener(
			 new MouseAdapter() {
				 public void mouseClicked( MouseEvent e) {
				     int max=0;
				     for(int i=0; i<13; i++)
					 max+=Cardselected[i];
				     if(e.getY() >= 192 && e.getY() <= 260) {
					 for(int j = 51; j <= 411; j+=30) {
					     if(e.getX() > j && e.getX() < (j + 30)) {
						 selected = (int)((j-50)/30);
						 if (Cardselected[selected] == 1) 
						     Cardselected[selected] = 0;
						 //a card of value 16 cannot be selected or played
						 else if (Cardselected[selected] == 0 && player[0][selected].value != 16 && max < 5)
						     Cardselected[selected] = 1;
			       			 repaint(selected*30+50,190,33,72);
					     }
					 }
				     }

				 }
			     }
			 );
    }

    public void paint(Graphics g) {
	for(int i = 0; i < 13; i++) {
	    //as long as the card is not of value 16(discarded), it is redrawn
	    if(player[0][i].value != 16 )
		drawCard(i, Cardselected[i], player[0][i], g);
	}
    }

    public void makeSuit (int x, int y, int suit, Graphics g) {
	//geometric forms representing each suit
	switch (suit) {
      case 1: //diamond:  x, 18; y, 24
        int xpts4[] = {x+11,  x+20, x+11, x+2};
        int ypts4[] = {y,     y+12, y+24, y+12};
	g.fillPolygon(xpts4, ypts4, xpts4.length);
        break;
      case 2: //club:  x, 22; y, 24
        int xpts2[] = {x+11, x+13, x+16, x+16, x+13, x+18, x+22, x+21, x+18, x+13, x+15, x+7,  x+9,  x+4,  x+1,  x,    x+4, x+9,  x+6, x+6, x+9};
        int ypts2[] = {y+1,  y+1,  y+4,  y+6,  y+11, y+7,  y+10, y+16, y+18, y+15, y+24, y+24, y+15, y+18, y+16, y+10, y+7, y+11, y+6, y+4, y+1};
	g.fillPolygon(xpts2, ypts2, xpts2.length);
        break;
      case 3: //heart: x, 18; y, 24
        int xpts3[] = {x+11, x+15, x+18, x+20, x+18, x+11,  x+4,  x+2, x+4, x+7};
        int ypts3[] = {y+6,  y,    y+2,  y+8,  y+14, y+24,  y+14, y+8, y+2, y};
	g.fillPolygon(xpts3, ypts3, xpts3.length);
        break;
      case 4: //spade: x. 18; y, 24
        int xpts[] = {x+11,  x+19, x+20, x+18, x+16, x+13,  x+15, x+7,   x+9,  x+6,  x+4,  x+2,  x+3};
        int ypts[] = {y,     y+12, y+16, y+20, y+20, y+16,  y+24, y+24,  y+16, y+20, y+20, y+16, y+12};
	g.fillPolygon(xpts, ypts, xpts.length);
        break;
        }
    }

    public void drawCard(int index, int raised, Big2card card, Graphics g) {
	g.setColor(Color.white);
	g.fillRoundRect(index*30+50, 200-raised*10, 32, 60, 10, 10);
	g.setColor(Color.black);
	g.drawRoundRect(index*30+50, 200-raised*10, 32, 60, 10, 10);
	g.setColor(Color.red);
	if (card.suit % 2 == 0)
	    g.setColor(Color.black);
	makeSuit(index*30+55, 205-raised*10, card.suit, g);
	g.setFont(new Font( "SansSerif", Font.PLAIN, 26));
	String tag = new String();
	switch (card.value) {
	case 10:
	    tag = "I0";
	    break;
	case 11:
	    tag = "J";
	    break;
	case 12:
	    tag = "Q";
	    break;
	case 13:
	    tag = "K";
	    break;
	case 14:
	    tag = "A";
	    break;
	case 15:
	    tag = "2";
	    break;
	default:
	    tag = Integer.toString(card.value);
	}
	g.drawString(tag, index*30+56, 255-raised*10);
    }

    public int isValid(Big2card hand[], Big2card lead[]){
	int valid = 0;
	playnum = 0;
	for(int i=0; i<13; i++)
	    playnum += Cardselected[i];
	if(playnum == 0 || playnum == 4 || playnum > 5)
	    valid = 0;
	if(playnum == 1) { 
	    valid += 1;
	    lead[0] = hand[0];
	}
	if(playnum == 2) {
	    if(hand[0].value == hand[1].value)
	        valid += 2;
	    lead[0] = hand[1];
	}
	if(playnum == 3) {
	    if(hand[0].value == hand[1].value && hand[1].value == hand[2].value)
		valid += 3;
	    lead[0] = hand[2];
	}
	if(playnum == 5) {
	    if(hand[0].value == hand[1].value && hand[1].value == hand[2].value && hand[2].value == hand[3].value) {
		valid += 7;
		lead[0] = hand[3];
	    }
	    if(hand[1].value == hand[2].value && hand[2].value == hand[3].value && hand[3].value == hand[4].value) {
		valid += 7;
		lead[0] = hand[4];
	    }
	    if(hand[0].value == hand[1].value && hand[1].value == hand[2].value && hand[3].value == hand[4].value) {
		valid += 6;
		lead[0] = hand[2];
	    }
	    if(hand[0].value == hand[1].value && hand[2].value == hand[3].value && hand[3].value == hand[4].value) {
		valid += 6;
		lead[0] = hand[4];
	    }
	    if(hand[0].suit==hand[1].suit && hand[1].suit==hand[2].suit && hand[2].suit==hand[3].suit && hand[3].suit==hand[4].suit) {
		valid += 5;
		lead[0] = hand[4];
	    }
	    int firstvalue = hand[0].value;
	    if(hand[1].value==firstvalue+1 && hand[2].value==firstvalue+2 && hand[3].value==firstvalue+3 && hand[4].value==firstvalue+4) {
		valid += 4;
		lead[0] = hand[4];
	    }
	}
	return valid;
    }

    public void arrangeAll() {
	Big2card.arrangeHand(player, 0);
	Big2card.arrangeHand(player, 1);
	Big2card.arrangeHand(player, 2);
	Big2card.arrangeHand(player, 3);
    }    

    public Big2card[] collectHand() {
	playnum = 0;
	for(int i=0; i<13; i++)
	    playnum += Cardselected[i];
        Big2card hand[] = new Big2card[playnum];
	int counter = 0;
        for(int i=0; i<13; i++) {
	    if(Cardselected[i]==1) {
		hand[counter] = player[0][i];
		counter++;
	    }
	}
	return hand;
    }

    public void discard() {
	for(int i=0; i<13; i++) {
	    if(Cardselected[i]==1) {
		Cardselected[i]=0;
		//sets value of discarded card to 16: cannot be played
		player[0][i]=new Big2card(16,1);
	    }
	}
	arrangeAll();
	repaint();
    }
}
