
/*
   This applet lets the user play a simple card game.
   The playing board has five rows and five columns
   Each space in this grid can hold a card.  The user
   sees one card from the deck at a time, and places
   it on the board by clicking on one of the empty
   spaces on the board.  The goal is to make good
   poker hands in each row and column.  (Note that
   the applet size MUST be 450 by 330 for this to
   work properly.)
*/


import java.awt.*;
import java.applet.Applet;

public class CardsApplet extends Applet {

   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.

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

   Card nextCard; // The card most recently dealt from the deck.

   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.


   public void init() {
         // Called by system when the applet is create;
         // initialize instance variables and start a game.
      setBackground(Color.lightGray);
      bigFont = new Font("Helvetica",Font.ITALIC,24);
      cardImages = getImage(getCodeBase(), "smallcards.gif");
      deck = new Deck();
      startGame();
   }


   void startGame() {
          // Begin a new game.
      deck.shuffle();
      nextCard = deck.dealNextCard();
      repaint();
   }


   void playCard(int row, int col) {
          // This is called when the user clicks on on the
          // playing board.  The parameters are numbers
          // between 0 adnd 4 that tell which position in
          // the grid was clicked on.
          //   This method places the next card on the
          // playing board at the specified position and
          // deals a new card.
      Graphics g = getGraphics();
      putCard(g, nextCard, 10 + 50*col, 5 + 65*row);
      nextCard = deck.dealNextCard();
      putCard(g, nextCard, 330, 155);
      g.dispose();
   }


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

      for (int row = 0; row < 5; row++)
        for (int col = 0; col < 5; col++) {
           putBlank(g, 10 + 50*col, 5+65*row);
        }

      g.setColor(Color.white);
      g.fillRect(260,5,180,320);
      g.setColor(Color.black);
      g.drawRect(260,5,179,319);
      g.setColor(Color.blue);

      g.drawString("Try to place the cards",270,20);
      g.drawString("to make good Poker hands",270,35);
      g.drawString("across each row and down",270,50);
      g.drawString("each column.  Just click",270,65);
      g.drawString("on the blank spot where",270,80);
      g.drawString("you want to put the card.",270,95);

      g.setColor(Color.lightGray);
      g.fillRoundRect(280,255,140,40,20,20);
      g.setColor(Color.red);
      g.drawRoundRect(280,255,140,40,20,20);
      g.drawString("Click to Restart",315,280);

      if (gameOver) {
         g.setFont(bigFont);
         g.setColor(Color.red);
         g.drawString("Game Over",285,180);
      }
      else {
         g.setColor(Color.blue);
         g.drawString("Next card:",270,140);
         putCard(g, nextCard, 330, 155);
      }

   }


   public boolean mouseDown(Event evt, int x, int y) {
         // If user clicks in the "restart" area, start
         // a new game.  If the user clicks on the playing
         // board area, call playCard().
      if (x > 280 && x < 420 && y > 255 && y < 295)  // in restart area
         startGame();
      else {
         int col = (x-5) / 50;  // user clicked on the playing board
         int row = (y-3) / 65;  //    if 0 <= row <= 4 and 0 <= col <= 4.
         if (row >= 0 && row < 5 && col >= 0 & col < 5)
            playCard(row,col);
      }
      return true;
   }


   void putCard(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)
         putBlank(g,x,y);  // to be safe, if c is null just draw a blank
      else {
        Graphics clipped = g.create(x,y,40,60);
        clipped.drawImage(cardImages,-40*c.getValue(),-60*c.getSuit(),this);
      }
   }


   void putBlank(Graphics g, int x, int y) {
          // Draws a white rectangle with a blue border,   The rectangle
          // is the same size as a Card.  Its upper left corner is at (x,y).
      g.setColor(Color.white);
      g.fillRect(x,y,40,60);
      g.setColor(Color.blue);
      g.drawRect(x,y,39,59);
   }


} // end CardsApplet