
/*
   This applet lets the user play a very simple card game.
   The applet shows one card at a time, and the user tries
   to guess whether the next card will be higher or lower
   than the preceding card.  The game ends when the user
   guesses wrong or when the six cards have been shown.
      Note that this file defines two classes: the main
   applet class and a canvas class that is used by the
   applet.  Most of the actual work is done in the canvas
   class.  The applet just does the setup and layout.
*/


import java.awt.*;
import java.awt.event.*;

public class HighLow extends java.applet.Applet {

   public void init() {
         // Called by system when the applet is created;
         // Lay out the applet and set up the canvas to
         // listen for events from the buttons.

      HighLowCanvas canvas;  // The canvas where the game will be played.

      Image cardpics;  // This will be an image that contains pictures
                       // of all the playing cards.  It is loaded here and
                       // passed to the constructor for the canvas.  It's
                       // done this way because only applets have a convenient
                       // method for loading images.

      Button highBttn, lowBttn, newGameBttn;  // Buttons to be shown below the canvas.

      setBackground(Color.lightGray);

      cardpics = getImage(getCodeBase(), "smallcards.gif");
      canvas = new HighLowCanvas(cardpics);

      highBttn = new Button("Higher");  // Create buttons and set up canvas as listener
      highBttn.addActionListener(canvas);

      lowBttn = new Button("Lower");
      lowBttn.addActionListener(canvas);

      newGameBttn = new Button("New Game");
      newGameBttn.addActionListener(canvas);

      Panel bottom = new Panel();   // A panel to hold the buttons.
      bottom.add(highBttn);
      bottom.add(lowBttn);
      bottom.add(newGameBttn);

      setLayout(new BorderLayout(3,3));   // Create applet layout
      add("South",bottom);
      add("Center",canvas);

   } // end init();


   public Insets getInsets() {
        // Called by the system to find out how much space to leave around the edges of the applet.
      return new Insets(3,3,3,3);
   }

} // end class HighLow



class HighLowCanvas extends Canvas implements ActionListener {

   Image cardImages;   // This Image will contain pictures of all 52 cards.
                       // It is loaded from a file "smallcards.gif" which
                       // must be in the same directory as the applet class file.
                       // This applet will only work with this file.

   Deck deck;     // A deck of playing cards to be used in the game.

   Card currentCard;   // The card most recently shown.

   boolean gameOver;   // This is set to false when a game begins and
                       // to true when it ends.  (The appearance of the
                       // applet and the legal actions are different,
                       // depending on whether the game is over or not.) 

   Font bigFont;  // A large-size font for displaying a message.
   String message;


   HighLowCanvas(Image cardImages) {
         // Constructor.  This receives the set of card pictures from the applet,
         // it creates the deck of cards and the font for displaying messages, and
         // it begins the first game.
      this.cardImages = cardImages;
      setBackground(new Color(220,220,255));
      deck = new Deck();
      bigFont = new Font("Serif",Font.BOLD,18);
      startGame();
   }


   void startGame() {
          // Begin a new game.
      deck.shuffle();
      currentCard = deck.dealNextCard();
      message = "Will the next card be higher or lower?";
      gameOver = true;
      repaint();
   }


   public void actionPerformed(ActionEvent evt) {
      String cmd = evt.getActionCommand();
      currentCard = deck.dealNextCard();    // THIS HAS TO BE REPLACED
      repaint();
   }


   public void paint(Graphics g) {  // repaint the applet

      if (message != null) {
         g.setColor(Color.red);
         g.setFont(bigFont);
         g.drawString(message,30,30);
      }

      drawCard(g,currentCard,20,60);

   }  // end paint()


   //----------------- Two subroutines for drawing cards ------------------------------

   void drawCard(Graphics g, Card c, int x, int y) {
          // Draws the specified card with its upper left-hand
          // corner at (x,y).  A card occupies a 40-by-60
          // rectangle.  This method will only work with the
          // card images in the file smallcards.gif.  If that
          // file is not available, it won't draw anything.
      if (c == null)
         drawCardFaceDown(g,x,y);  // to be safe, if c is null just draw a face-down card
      else {
        Graphics clipped = g.create(x,y,40,60);
        clipped.drawImage(cardImages,-40*(c.getValue()-1),-60*(c.getSuit()-1),this);
      }
   }


   void drawCardFaceDown(Graphics g, int x, int y) {
          // Draws a face-down card, just showing the back of the card.  
          // The upper left corner of the card is at (x,y), and it
          // occupies a 40-by-60 rectangle.
      g.setColor(Color.white);
      g.fillRect(x,y,40,60);
      g.setColor(Color.blue);
      g.drawRect(x,y,39,59);
      g.fillRect(x+3,y+3,34,54);
   }


} // end CardsApplet