import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.String;
import java.awt.Color;
import java.awt.Point;
import javax.swing.Timer;
/**
* Write a description of class AppPanel here.
*
* @author Roshan Krishnan
* @version (a version number or a date)
*/
public class AppPanel extends JPanel implements MouseListener, ActionListener
{
// PROPERTIES
Card [] deck;
Card [] handOne;
Card [] handTwo;
Card [] handThree;
Card [] handFour;
private Card tempOne, tempTwo, tempThree, tempFour;
private Timer timer;
JButton button;
JButton clearButton;
int x = 0;
int card = 0;
int counter = 0;
int wrongSuit = 0;
String suitWrong = Integer.toString(wrongSuit);
int round = 0;
String compare = "";
boolean followOne;
boolean followTwo;
boolean followThree;
boolean followFour;
// METHODS
public AppPanel()
{
button = new JButton( "Deal" );
add( button );
button.addActionListener(this);
clearButton = new JButton( "Clear" );
add( clearButton );
clearButton.addActionListener(this);
addMouseListener( this );
clearButton.setVisible(false);
deck = new Card[52];
handOne = new Card[13];
handTwo = new Card[13];
handThree = new Card[13];
handFour = new Card[13];
for( int i = 0; i < 52; i++ )
{
x = x + 15;
deck[i] = new Card( x, 100, 72, 100, i );
}
shuffle();
bubblesort();
repaint();
}
public void paintComponent( Graphics g )
{
super.paintComponent(g);
for( int i = 0; i < handOne.length; i++ )
{
handOne[i].draw( g );
}
for( int i = 0; i < handTwo.length; i++ )
{
handTwo[i].draw( g );
}
for( int i = 0; i < handThree.length; i++ )
{
handThree[i].draw( g );
}
for( int i = 0; i < handFour.length; i++ )
{
handFour[i].draw( g );
}
}
public void bubblesort()
{
int h = handOne.length;
for( int pass = 1; pass < h; pass++ )
{
for( int i = 0; i < (h - pass); i++ )
{
if( handOne[i].getValue() > handOne[i + 1].getValue() )
{
Card temp = handOne[i];
handOne[i] = handOne[i + 1];
handOne[i + 1] = temp;
}
}
}
int x = 225;
for( int i = 0; i < handOne.length; i++ )
{
handOne[i].setLocation( x, 400 );
x = x + 15;
}
int h2 = handTwo.length;
for( int pass = 1; pass < h2; pass++ )
{
for( int i = 0; i < (h2 - pass); i++ )
{
if( handTwo[i].getValue() > handTwo[i + 1].getValue() )
{
Card temp = handTwo[i];
handTwo[i] = handTwo[i + 1];
handTwo[i + 1] = temp;
}
}
}
int x2 = 225;
for( int i = 0; i < handTwo.length; i++ )
{
handTwo[i].setLocation( x2, 100 );
x2 = x2 + 15;
}
int h3 = handThree.length;
for( int pass = 1; pass < h3; pass++ )
{
for( int i = 0; i < (h3 - pass); i++ )
{
if( handThree[i].getValue() > handThree[i + 1].getValue() )
{
Card temp = handThree[i];
handThree[i] = handThree[i + 1];
handThree[i + 1] = temp;
}
}
}
int y3 = 100;
for( int i = 0; i < handThree.length; i++ )
{
handThree[i].setLocation( 50, y3 );
y3 = y3 + 25;
}
int h4 = handFour.length;
for( int pass = 1; pass < h4; pass++ )
{
for( int i = 0; i < (h4 - pass); i++ )
{
if( handFour[i].getValue() > handFour[i + 1].getValue() )
{
Card temp = handFour[i];
handFour[i] = handFour[i + 1];
handFour[i + 1] = temp;
}
}
}
int y4 = 100;
for( int i = 0; i < handFour.length; i++ )
{
handFour[i].setLocation( 550, y4 );
y4 = y4 + 25;
}
}
public void shuffle ()
{
Card temp;
for( int k = 0; k < 1000; k++ )
{
int i = (int)(Math.random()*52);
int j = (int)(Math.random()*52);
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
for( int i = 0; i < 13; i++ )
{
int index = i * 4;
handOne[i] = deck[index];
}
for( int i = 0; i < 13; i++ )
{
int index = i * 4 + 1;
handTwo[i] = deck[index];
}
for( int i = 0; i < 13; i++ )
{
int index = i * 4 + 2;
handThree[i] = deck[index];
}
for( int i = 0; i < 13; i++ )
{
int index = i * 4 + 3;
handFour[i] = deck[index];
}
}
public void followCards()
{
followOne = false;
followTwo = false;
followThree = false;
followFour = false;
for( int i = 0; i < 13; i++ )
{
if( handTwo[i].getSuit().equals(compare) && handTwo[i].notDead() )
{
followTwo= true;
}
if( handFour[i].getSuit().equals(compare) && handFour[i].notDead() )
{
followFour = true;
}
if( handOne[i].getSuit().equals(compare) && handOne[i].notDead() )
{
followOne = true;
}
if( handThree[i].getSuit().equals(compare) && handThree[i].notDead() )
{
followThree = true;
}
}
}
public void mouseClicked( MouseEvent e )
{
}
public void mouseEntered( MouseEvent e )
{
}
public void mouseExited( MouseEvent e )
{
}
public void mousePressed( MouseEvent e )
{
int getx = e.getX();
int gety = e.getY();
int value;
if (round < 4)
{
for( int i = 0; i < 13; i++ )
{
if( counter == 0 )
{
if( i == clickArea( handTwo ) )
{
value = 2;
}
else
{
value = 1;
}
if( handTwo[i].boundsCheck( getx, gety, value ) && handTwo[i].notDead() )
{
if( round == 0 )
{
compare = handTwo[i].getSuit();
followCards();
}
if( followTwo == false )
{
handTwo[i].setLocation( 300, 200 );
tempTwo = handTwo[i];
repaint();
round++;
tempTwo.setValue(0);
if( round == 4 )
{
counter = 4;
}
else
{
counter = 1;
}
}
else if( handTwo[i].getSuit().equals(compare) )
{
handTwo[i].setLocation( 300, 200 );
tempTwo = handTwo[i];
repaint();
round++;
if( round == 4 )
{
counter = 4;
}
else
{
counter = 1;
}
}
}
}
}
for( int i = 0; i < 13; i++ )
{
if( counter == 1 )
{
if( i == clickArea( handFour ) )
{
value = 2;
}
else
{
value = 0;
}
if( handFour[i].boundsCheck( getx, gety, value ) && handFour[i].notDead() )
{
if( round == 0 )
{
compare = handFour[i].getSuit();
followCards();
}
if( followFour == false )
{
handFour[i].setLocation( 375, 250 );
tempFour = handFour[i];
repaint();
round++;
tempFour.setValue(0);
if( round == 4 )
{
counter = 4;
}
else
{
counter = 2;
}
}
else if( handFour[i].getSuit().equals(compare) )
{
handFour[i].setLocation( 375, 250 );
tempFour = handFour[i];
repaint();
round++;
if( round == 4 )
{
counter = 4;
}
else
{
counter = 2;
}
}
}
}
}
for( int i = 0; i < 13; i++ )
{
if( counter == 2 )
{
if( i == clickArea( handOne ) )
{
value = 2;
}
else
{
value = 1;
}
if( handOne[i].boundsCheck( getx, gety, value ) && handOne[i].notDead() )
{
if( round == 0 )
{
compare = handOne[i].getSuit();
followCards();
}
if( followOne == false )
{
handOne[i].setLocation( 300, 300 );
tempOne = handOne[i];
repaint();
round++;
tempOne.setValue(0);
if( round == 4 )
{
counter = 4;
}
else
{
counter = 3;
}
}
else if( handOne[i].getSuit().equals(compare) )
{
handOne[i].setLocation( 300, 300 );
tempOne = handOne[i];
repaint();
round++;
if( round == 4 )
{
counter = 4;
}
else
{
counter = 3;
}
}
}
}
}
for( int i = 0; i < 13; i++ )
{
if( counter == 3 )
{
if( i == clickArea( handThree ) )
{
value = 2;
}
else
{
value = 0;
}
if( handThree[i].boundsCheck( getx, gety, value ) && handThree[i].notDead() )
{
if( round == 0 )
{
compare = handThree[i].getSuit();
followCards();
}
if( followThree == false )
{
handThree[i].setLocation( 225, 250 );
tempThree = handThree[i];
repaint();
round++;
tempThree.setValue(0);
if( round == 4 )
{
counter = 4;
}
else
{
counter = 0;
}
}
else if( handThree[i].getSuit().equals(compare) )
{
handThree[i].setLocation( 225, 250 );
tempThree = handThree[i];
repaint();
round++;
if( round == 4 )
{
counter = 4;
}
else
{
counter = 0;
}
}
}
}
}
}
if( round == 4 )
{
clearButton.setVisible(true);
}
System.out.println( "Round: " + round );
System.out.println( "Counter: " + counter);
}
public int clickArea( Card [] Array )
{
int value = 0;
for( int i = 0; i < 13; i++ )
{
if( Array[i].notDead() && value < i )
{
value = i;
}
}
return value;
}
public void mouseReleased( MouseEvent e )
{
}
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == button )
{
for( int i = 0; i < 52; i++ )
{
x = x + 15;
deck[i] = new Card( x, 100, 72, 100, i );
}
shuffle();
bubblesort();
repaint();
counter = 0;
}
if( e.getSource() == clearButton )
{
round = 0;
tempOne.killCard();
tempTwo.killCard();
tempThree.killCard();
tempFour.killCard();
bubblesort();
repaint();
// assumes temp[0] was the first card played during the trick
/*int winner = 0;
for(int i = 1; i < 4; i ++)
{
if( temp[i].getSuit.equals(compare) )
{
if( temp[i].getValue() > temp[winner].getValue() )
{
winner = i;
}
}
}*/
if( tempTwo.getValue() > tempThree.getValue() && tempTwo.getValue() > tempFour.getValue() && tempTwo.getValue() > tempOne.getValue() )
{
counter = 0;
}
if( tempFour.getValue() > tempThree.getValue() && tempFour.getValue() > tempTwo.getValue() && tempFour.getValue() > tempOne.getValue() )
{
counter = 1;
}
if( tempOne.getValue() > tempThree.getValue() && tempOne.getValue() > tempFour.getValue() && tempOne.getValue() > tempTwo.getValue() )
{
counter = 2;
}
if( tempThree.getValue() > tempTwo.getValue() && tempThree.getValue() > tempFour.getValue() && tempThree.getValue() > tempOne.getValue() )
{
counter = 3;
}
}
}
}