problem writing an applet
I can't figure out how to write the if-else code to determine the outcome of the game. This is because I can't figure out how to write the value of the variable for the picked card! I've commented on the part where I am having problems.
// User is asked to guess which of the two face down cards has
// the greatest value. The face value is the only thing used to
// determine the value of the cards. When the user selects a card,
// the fronts of the cards are shown, and the result of the game is
// displayed. The user is aksed if he or she wishes to play again.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Exercise4 extends Applet implements MouseListener
{
DeckOfCards deck;
Card card1, card2, drawnCard1, drawnCard2;
String result, message, programmerName = "2";
Font bigFont;
Image cardpics; by-60
public void init()
{
setBackground( Color.cyan );
setSize (320,250);
addMouseListener(this);
cardpics = getImage(getCodeBase(), "cards.gif");
bigFont = new Font("Serif", Font.BOLD, 14);
deck = new DeckOfCards();
deal();
}
//
// Start the game by dealing two cards and repainting the applet.
//
void deal()
{
card1 = card2 = drawnCard1 = drawnCard2 = null;
message = "Which Card is Higher?";
result = "";
deck.shuffle();
deck.deal();
repaint();
}
//
// Because the applet is registered to listen for mouse clicks on
// itself, the system will call this method when the user presses
// the mouse on the applet. In this case, the method checks whether
// the user clicked on one of the cards or on the message at the
// bottom of the screen. If the user clicked card N, this method
// calls clickedCard(N). If the user clicked on the message, this
// method calls clickedMessage().
//
public void mousePressed(MouseEvent evt)
{
int x = evt.getX(); // convenient name for x-coord of mouse
int y = evt.getY(); // convenient name for y-coord of mouse
if ( y > 20 && y < 80 )
{
if (x > 100 && x < 140)
clickedCard(1);
else if (x > 160 && x < 200)
clickedCard(2);
}
else
if (x > 33 && x > 25)
deal();
}
//
// This method is called (by the mousePressed() method given below)
// when the user clicks on one of the cards. The parameter, cardNum,
// tells which card was clicked. String result is set to the appropriate
// message telling the outcome of the game.
//
void clickedCard(int cardNum)
{
// I can't see to pick up the right card value and display it. I don't think I am using the method above right?
cardNum = card1.getValue();
if (card1.getValue() > card2.getValue())
message = "You win!";
else if (card1.getValue() < card2.getValue())
message = "Too Bad - You Lose";
else if (card1.getValue() == card2.getValue())
message = "A Draw";
drawnCard1 = card1;
drawnCard2 = card2;
message = "Play Again?";
repaint();
}
//
// The paint method is called by the system when the applet needs
// to be repainted. In this applet, it draws the two cards
// and the messages at the bottom of the applet.
//
public void paint(Graphics g)
{
int width = getSize().width;
int height = getSize().height;
FontMetrics fm = getFontMetrics(bigFont);
int w;
g.setColor(Color.blue);
g.drawRect(0,0,width-1,height-1);
g.setColor(Color.pink); background.
g.fillRect(33, 105, 250, 25);
g.setColor(Color.blue);
g.drawRect(33, 105, 250, 25);
g.setFont( bigFont );
w = fm.stringWidth(message);
g.drawString( message, 86, 122);
g.setColor(Color.pink);
g.fillRect(33, 155, 250, 25);
g.setColor(Color.blue);
g.drawRect(33, 155, 250, 25);
g.setFont( bigFont );
w = fm.stringWidth(result);
g.drawString( result, 20, 30);
g.setColor(Color.pink);
g.fillRect(33, 205, 250, 25);
g.setColor(Color.blue);
g.drawRect(33, 205, 250, 25);
g.setFont( bigFont );
w = fm.stringWidth(programmerName);
g.drawString( programmerName, 65, 222);
drawCard( g, drawnCard1, 100, 20 ); // Draw the two cards.
drawCard( g, drawnCard2, 160, 20 ); // If drawnCard1 or drawnCard2 are null,
// the cards will be drawn face down.
}
//
// Draws a card as a 40 by 60 rectangle with
// upper left corner at (x,y). The card is drawn
// in the graphics context g. If card is null, then
// a face-down card is drawn.
//
void drawCard(Graphics g, Card card, int x, int y)
{
if (card == null)
{
// Draw a face-down card
g.setColor(Color.blue);
g.fillRect(x,y,40,60);
g.setColor(Color.white);
g.drawRect(x+3,y+3,33,53);
g.drawRect(x+4,y+4,31,51);
}
else
{
int row = 0;
switch (card.getSuit())
{
case 0: row = 0; break;
case 1: row = 1; break;
case 2: row = 2; break;
case 3: row = 3; break;
}
int sx, sy; // coords of upper left corner in the source image.
sx = 40*(card.getValue() - 1);
sy = 60*row;
g.drawImage(cardpics, x, y, x+40, y+60,
sx, sy, sx+40, sy+60, this);
}
}
//
// Other mouse routines required by the MouseListener interface.
// They are not used in this applet
//
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
}
//********************************************************************
// DeckOfCards.java
//
//********************************************************************
import java.util.Random;
public class DeckOfCards
{
private int count;
private boolean[] deck = new boolean[53];
Random generator = new Random();
public DeckOfCards()
{
shuffle();
}
public void shuffle()
{
for (int i = 1; i <= 52; i++)
deck = false;
count = 0;
}
public Card deal()
{
int num, suit;
if (count > 51)
{
num = -1;
suit = -1;
}
else
{
do
{
num = generator.nextInt(13) + 1;
suit = generator.nextInt(4);
}
while (deck[num+suit*13] == true);
deck[num+suit*13] = true;
count++;
}
return new Card(num,suit);
}
public int cardsInDeck()
{
return (52 - count);
}
}
//********************************************************************
// Card.java
//
//********************************************************************
public class Card
{
private int num, suit;
public Card (int num, int suit)
{
this.num = num;
this.suit = suit;
}
public int getValue ()
{
return num;
}
public int getSuit ()
{
return suit;
}
public String toString()
{
String result = "message";
switch (num)
{
case -1: result = " No Card"; break;
case 1: result = " Ace "; break;
case 2: result = " 2 "; break;
case 3: result = " 3 "; break;
case 4: result = " 4 "; break;
case 5: result = " 5 "; break;
case 6: result = " 6 "; break;
case 7: result = " 7 "; break;
case 8: result = " 8 "; break;
case 9: result = " 9 "; break;
case 10: result = " 10 "; break;
case 11: result = " Jack "; break;
case 12: result = " Queen"; break;
case 13: result = " King "; break;
}
switch (suit)
{
case -1: break;
case 0: result = result + " of Clubs"; break;
case 1: result = result + " of Hearts"; break;
case 2: result = result + " of Spades"; break;
case 3: result = result + " of Diamonds"; break;
}
return result;
}
}

