import java.awt.*;
public class Card implements Comparable
{
public static final int LOW_RANK = 0,
HIGH_RANK = 51;
private int rank; //0..51
private int value; //face value 2, 3, ..., 10, 11(jack), 14(ace)
private int suit; //0=club,1=dia, 2=heart,3=spade
private Image faceImg; //face-up image
private boolean faceUp; //true when face-up
//Constructor
//Pre: none
//Post: Card has been initialized with rank, value & suit, face up
public Card (int rank)
{
this.rank = rank;
suit = rank / 13; //gives number in 0..3
value = 2 + rank % 13; //gives number in 2..14
faceUp = true;
}//end constructor
//Pre: Card has been initialized
//Post: Card is turned face up
public void turnUp()
{
faceUp = true;
}
//Pre: Card has been initialized
//Post: Card is turned face down
public void turnDown()
{
faceUp = false;
}
//Pre: Card has been initialized
//Post: Card's image is set to img
public void setImage (Image img)
{
faceImg = img;
}
//Pre: Card has been initialized
//Post: Returns face image of card if it is face up;
// Returns null if face down
public Image toImage()
{
Image img;
if (faceUp)
img = faceImg;
else
img = null; //no image
return img;
}//end toImage
/* Pre: Card has been initialized
* Post: compareTo(obj) returns
* -1 if (obj1 < obj2)
* 0 if (obj1 == obj2)
* 1 if (obj1 > obj2)
*/
public int compareTo(Object obj)
{
//Cast obj into a Card reference
int objRank = ((Card)obj).rank;
int returnVal;
if (this.rank < objRank)
{
returnVal = -1;
}
else if (this.rank == objRank)
{
returnVal = 0;
} else {
returnVal = 1;
}
return returnVal;
}//end compareTo
/* toString() returns a two-character representation of the card.
* e.g., 2C means 2 of clubs.
* Algorithm: the instance variables, suit and value, are
* used as indexes into the literal strings that store the letters
*/
public String toString()
{
return "" + "??23456789tjqka".charAt(value) + "cdhs".charAt(suit);
}//end toString
}//end Card class
Deck.java:
Code:
import java.awt.*;
public class Deck
{
public int NCARDS = 52;
public Card deck[] = new Card[NCARDS];
private int top;
private Image cardBack;
public Deck(CardApplet a)
{
cardBack = a.cardImage("back");
a.showStatus("Please wait while card images are loading. ");
// Make the cards
for (int k = 0; k < deck.length; k++)
{
deck[k] = new Card(Card.LOW_RANK + k);
// Get the card's image
Image img = a.cardImage(deck[k].toString());
deck[k].setImage(img);
}// for()
}// Deck()
public Card dealOneCard(boolean faceUp)
{
// Get the top card
Card topCard = deck[top];
// Flip it
if (faceUp)
{
topCard.turnUp();
} else {
topCard.turnDown();
}// if()
// top is new top card
top = (top + 1) % NCARDS;
// deal it
return topCard;
}// dealOneCard()
public Image getImage()
{
}// getImage()
public void shuffleDeck()
{
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for ( int i = 26; i > 0; i-- )
{
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
}// shuffleDeck()
public void sortDeck(Comparable arr[])
{
Comparable temp;
int n, pass;
boolean done;
n = arr.length;
pass = 1;
done = false;
while ( (pass < n) && (!done) )
{
done = true;
for (int i = 0; i < n - pass; i++)
{
if (arr[i].compareTo(arr[i+1]) > 0)
{
temp = arr[i]; //Swap two positions
arr[i] = arr[i+1];
arr[i+1] = temp;
done = false; //made a swap, might not be done
}//end if
}//end for
pass++;
}//end while
}// sortDeck()
}// Deck
CardApplet.java:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class CardApplet extends Applet implements ActionListener
{
// Declare GUI components
private Button sortButton = new Button("Sort");
private Button shuffleButton = new Button("Shuffle");
private Button showButton = new Button("Hide cards");
// Initialize deck and create instance variables
public Deck deck;
private boolean faceUp;
public void init()
{
// Add ActionListener
sortButton.addActionListener(this);
shuffleButton.addActionListener(this);
showButton.addActionListener(this);
// Add components
add (sortButton);
add (shuffleButton);
add (showButton);
// Set the applet's size
setSize(600,610);
}// init()
public void paint(Graphics g)
{
}// paint()
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sortButton)
{
Deck.sortDeck(deck);
}
else if (e.getSource() == shuffleButton)
{
Deck.shuffleDeck(deck);
}
else if (e.getSource() == showButton)
{
faceUp = false;
}
}// actionPerformed()
public Image cardImage(String s)
{
}// cardImage()
}
My problem is, I cannot figure out what to do for the paint method of the CardApplet class. I am supposed to paint a string version of the cards (2C would be 2 of clubs) to the applet window and then test to see if I can shuffle, sort and hide the cards. The Card.toString() method returns the string version of the cards, but how do I put them together and display?
My second problem is the Deck.sortDeck(deck) type calls, they are giving me an non-applicable error.
I am still working on the showButton button and the Image() methods..that's why they are left incomplete.
Wow, lot's of this stuff is above my head =p. We haven't done graphics yet, so I'm not sure what all of that stuff in the paint() method is. Did you use the Card.toString() method anywhere? If not, how did you get the string representation of the cards?
Also, she wants us to use her "Polymorphic Bubble Sort Method" (my version of sort), so is there any way to work that in there (I noticed you have a different method, which most likely is better than the other but she loves to subract points for not doing it "her way").
I have the images now, I'm not sure how to load them all in (I've only loaded one image before, and of course the book nor her make no mention of how to load multiple images), I'm assuming a for loop is used here. The images are in the form 2c.gif, 2d.gif, 2h.gif, etc.)