//*******************************************************************

// play.java

//*******************************************************************



import java.applet.*;

import java.awt.*;



//*******************************************************************



public class play extends Applet

  {

    Graphics g;

    cards card_deck;



    int width;

    int height;

    int card;

    int number;

    int suit;



    boolean card_displayed[];

    int dealer_cards[];

    int player_cards[];



    int card_count;

    int card_width = 100;

    int card_height = 140;



    int number_of_dealer_cards;

    int number_of_player_cards;



    boolean hand_done = true;



    //----------------------------------------------------------------



    public void init()

      {

        g = getGraphics();



        card_deck = new cards(g);



        card_displayed = new boolean [52];

        dealer_cards   = new int     [10];

        player_cards   = new int     [10];



        new buttons(g, "Deal"  , 10,  50, 80, 20);

        new buttons(g, "Hit Me", 10,  80, 80, 20);

        new buttons(g, "Stay"  , 10, 110, 80, 20);



        deal();

      }



    //----------------------------------------------------------------



    void reset_cards()

      {

        int i;



        for (i = 0; i < 52; i++)

          card_displayed[i] = false;



        card_count = 0;



        number_of_dealer_cards = 0;

        number_of_player_cards = 0;

      }



    //----------------------------------------------------------------



    void set_defaults()

      {

        width = size().width;

        height = size().height;

      }



    //----------------------------------------------------------------



    void get_card()

      {

        card = (int) (Math.random() * 52);



        do

          {

            if (!card_displayed[card])

              {

                card_displayed[card] = true;

                break;

              }

            else if (++card == 52)

              card = 0;



          } while (true);



        get_number_and_suit(card);

      }



    //----------------------------------------------------------------



    void get_number_and_suit(int card)

      {

        number = card % 13;

        suit = card / 13;

      }



    //----------------------------------------------------------------



    void deal()

      {

        hand_done = false;



        reset_cards();



        for (int x = 0; x < 2; x++)

          {

            for (int y = 0; y < 2; y++)

              {

                get_card();



                if (y == 0)

                  dealer_cards[number_of_dealer_cards++] = card;

                else

                  player_cards[number_of_player_cards++] = card;



                if (x == 0 && y == 0)

                  card_deck.draw_back(card_width, 0,

                                      card_width, card_height);

                else

                  card_deck.draw(card,

                                 (x+1)*card_width, y*card_height,

                                 card_width, card_height);



                card_count++;

                showStatus("Cards dealt: " + card_count);

              }

          }

      }



    //----------------------------------------------------------------



    void hit_me()

      {

        if (hand_done)

          return;



        get_card();



        player_cards[number_of_player_cards++] = card;



        card_deck.draw(card,

                       number_of_player_cards*card_width, card_height,

                       card_width, card_height);



        card_count++;

        showStatus("Cards dealt: " + card_count);



        int player_total = get_total(player_cards,

                                     number_of_player_cards);



        if (player_total > 21)

          done();

      }



    //----------------------------------------------------------------



    int get_total(int cards[], int number_of_cards)

      {

        int total = 0;

        int aces = 0;



        for (int i = 0; i < number_of_cards; i++)

          {

            get_number_and_suit(cards[i]);



            switch (number)

              {

                case 10:

                case 11:

                case 12:

                  total += 10;

                  break;



                case 0:

                  aces++;

                  total += 1;

                  break;



                default:

                  total += number + 1;

              }

          }



        for (int a = 0; a < aces; a++)

          {

            if (total + 10 > 21)

              break;

            else

              total += 10;

          }



        return total;

      }



    //----------------------------------------------------------------



    void done()

      {

        if (hand_done)

          return;



        hand_done = true;

        card_deck.draw(dealer_cards [0], card_width, 0,

                       card_width, card_height);



        int player_total = get_total(player_cards,

                                     number_of_player_cards);



        int dealer_total = get_total(dealer_cards,

                                     number_of_dealer_cards);



        if (player_total > 21)

          {

            g.setColor(Color.red);

            g.drawString("Player", 10, 160);

            g.drawString("BUSTED", 10, 180);

            g.drawString("Dealer", 10, 205);

            g.drawString("WINS"  , 10, 225);

            return;

          }



        while (dealer_total < 17)

          {

            get_card();



            dealer_cards[number_of_dealer_cards++] = card;



            card_deck.draw(card, number_of_dealer_cards*card_width, 0,

                          card_width, card_height);



            card_count++;

            showStatus("Cards dealt: " + card_count);



            dealer_total = get_total(dealer_cards,

                                    number_of_dealer_cards);

          }



        if (dealer_total > 21)

          {

            g.setColor(Color.green);

            g.drawString("Dealer", 10, 160);

            g.drawString("Busted", 10, 180);

            g.drawString("Player", 10, 205);

            g.drawString("WINS"  , 10, 225);

          }

        else if (player_total > dealer_total)

          {

            g.setColor(Color.green);

            g.drawString("Player", 10, 160);

            g.drawString("WINS"  , 10, 180);

          }

        else if (dealer_total == player_total)

          {

            g.setColor(Color.yellow);

            g.drawString("PUSH", 10, 160);

          }

        else

          {

            g.setColor(Color.red);

            g.drawString("Dealer", 10, 160);

            g.drawString("WINS"  , 10, 180);

          }

      }



    //----------------------------------------------------------------



    public boolean mouseUp(Event evt, int x, int y)

      {

        buttons.draw_items();

        return true;

      }



    //----------------------------------------------------------------



    public boolean mouseDown(Event evt, int x, int y)

      {

        int button_pushed = buttons.push_button(x, y);



        switch (button_pushed)

          {

          case 0:

            if (!hand_done)

              break;



            clear();

            deal();

            repaint();

            break;



          case 1:

            hit_me();

            break;



          case 2:

            done();

            break;

          }



        return true;

      }



    //----------------------------------------------------------------



    void clear()

      {

        g.setColor(Color.black);

        g.fillRect(0, 0, width, height);

      }



    //----------------------------------------------------------------



    public void update(Graphics _g)

      {

        clear();



        if (hand_done)

          card_deck.draw(dealer_cards[0], card_width, 0,

                         card_width, card_height);

        else

          card_deck.draw_back(card_width, 0, card_width, card_height);



        for (int i = 1; i < number_of_dealer_cards; i++)

          card_deck.draw(dealer_cards[i], (i+1)*card_width, 0,

                         card_width, card_height);



        for (int i = 0; i < number_of_player_cards; i++)

          card_deck.draw(player_cards[i],

                         (i+1)*card_width, card_height,

                         card_width, card_height);



        buttons.draw_items();

      }



    //----------------------------------------------------------------



    public void paint(Graphics g)

      {

        set_defaults();

        update(g);

      }

  }

