
-----------------------------------
nelsonkkyy
Mon Jan 17, 2011 11:07 am

Nees help with BlackJack game!
-----------------------------------
I don't know how to input the player's name
and i want to set a backgorund image in it.


import java.applet.*;
import java.awt.*;
import java.util.Random;

public class Blackjack extends Applet {
 static final int BET = 0;
 static final int PLAY = 1;
 static final int DEALER = 2;
 int state = BET;
 int money = 5000;
 int bet = 0;
 Deck deck = new Deck();
 Hand playersHand;
 Hand dealersHand;
 Label topLine = new Label("Welcome to Blackjack!",Label.CENTER); 
 Label totalLine = new Label("You have $5000.",Label.CENTER); 
 Label dealersLabel = new Label("Dealer's Hand",Label.CENTER); 
 Label playersLabel = new Label("Your Hand",Label.CENTER); 
 TextArea dealerText = new TextArea(9,20);
 TextArea playerText = new TextArea(9,20);
 Button hitButton = new Button("Hit");
 Button stayButton = new Button("Stay");
 Label betLabel = new Label("Enter your bet(Press Enter): ",Label.RIGHT); 
 TextField betField = new TextField();
 GridBagLayout gridbag = new GridBagLayout();;
 GridBagConstraints constraints = new GridBagConstraints(); 
 public void init() {
  setLayout(gridbag);
  constraints.fill = GridBagConstraints.BOTH;
  addComponent(topLine,0,0);
  addComponent(totalLine,0,1);
  addComponent(dealersLabel,1,0);
  addComponent(playersLabel,1,1);
  dealerText.setEditable(false);
  playerText.setEditable(false);
  addComponent(dealerText,2,0);
  addComponent(playerText,2,1);
  addComponent(hitButton,3,0);
  addComponent(stayButton,3,1);
  addComponent(betLabel,4,0);
  addComponent(betField,4,1);
 }
 void addComponent(Component c,int y,int x) {
  constraints.gridx = x;
  constraints.gridy = y;
  gridbag.setConstraints(c, constraints);
  add(c);
 }
 public boolean handleEvent(Event event) {
  if(event.target instanceof TextField && event.id == Event.ACTION_EVENT) {
   if(state == BET){
    updateBet();
    return true;
   }
  }else if(event.target instanceof Button && event.id == Event.ACTION_EVENT) {
   if(state == PLAY) {
    if("Hit".equals(event.arg)) {
     playersHand.addCard(deck.deal()); 
     playersHand.show(playerText,false); 
     if(!playersHand.under(22)) state = DEALER;
    }else if("Stay".equals(event.arg)) state = DEALER;
    if(state == DEALER) {
     while(dealersHand.mustHit())
      dealersHand.addCard(deck.deal()); 
     dealersHand.show(dealerText,false); 
     showResults();
    }
   }
  }
  return false;
 }
 public void updateBet() {
  betField.setEditable(false);
  betLabel.setText("Bet: ");
  try {
   Integer i = new Integer(betField.getText()); 
   bet = i.intValue();
  } catch (NumberFormatException ex) {
   bet = 1;
  }
  betField.setText(String.valueOf(bet));
  initialDeal();
  if(playersHand.blackjack()) playerWins();
  else state = PLAY;
 }
 void initialDeal() {
  playersHand = new Hand();
  dealersHand = new Hand();
  for(int i = 0;i dealersHand.bestScore()) playerWins();
  else if(playersHand.bestScore() < dealersHand.bestScore()) dealerWins();
  else tie();
 }
}

class Deck {
 // Variable declarations
 int cards
Thanks!

-----------------------------------
ProgrammingFun
Mon Jan 17, 2011 8:07 pm

RE:Nees help with BlackJack game!
-----------------------------------
Please post your code in syntax tags with indentation:


   import java.applet.*;
   import java.awt.*;
   import java.util.Random;

   public class Blackjack extends Applet {
      static final int BET = 0;
      static final int PLAY = 1;
      static final int DEALER = 2;
      int state = BET;
      int money = 5000;
      int bet = 0;
      Deck deck = new Deck();
      Hand playersHand;
      Hand dealersHand;
      Label topLine = new Label("Welcome to Blackjack!",Label.CENTER);
      Label totalLine = new Label("You have $5000.",Label.CENTER);
      Label dealersLabel = new Label("Dealer's Hand",Label.CENTER);
      Label playersLabel = new Label("Your Hand",Label.CENTER);
      TextArea dealerText = new TextArea(9,20);
      TextArea playerText = new TextArea(9,20);
      Button hitButton = new Button("Hit");
      Button stayButton = new Button("Stay");
      Label betLabel = new Label("Enter your bet(Press Enter): ",Label.RIGHT);
      TextField betField = new TextField();
      GridBagLayout gridbag = new GridBagLayout();;
      GridBagConstraints constraints = new GridBagConstraints();
      public void init() {
         setLayout(gridbag);
         constraints.fill = GridBagConstraints.BOTH;
         addComponent(topLine,0,0);
         addComponent(totalLine,0,1);
         addComponent(dealersLabel,1,0);
         addComponent(playersLabel,1,1);
         dealerText.setEditable(false);
         playerText.setEditable(false);
         addComponent(dealerText,2,0);
         addComponent(playerText,2,1);
         addComponent(hitButton,3,0);
         addComponent(stayButton,3,1);
         addComponent(betLabel,4,0);
         addComponent(betField,4,1);
      }
      void addComponent(Component c,int y,int x) {
         constraints.gridx = x;
         constraints.gridy = y;
         gridbag.setConstraints(c, constraints);
         add(c);
      }
      public boolean handleEvent(Event event) {
         if(event.target instanceof TextField && event.id == Event.ACTION_EVENT) {
            if(state == BET){
               updateBet();
               return true;
            }
         }
         else if(event.target instanceof Button && event.id == Event.ACTION_EVENT) {
            if(state == PLAY) {
               if("Hit".equals(event.arg)) {
                  playersHand.addCard(deck.deal());
                  playersHand.show(playerText,false);
                  if(!playersHand.under(22)) state = DEALER;
               }
               else if("Stay".equals(event.arg)) state = DEALER;
               if(state == DEALER) {
                  while(dealersHand.mustHit())
                     dealersHand.addCard(deck.deal());
                  dealersHand.show(dealerText,false);
                  showResults();
               }
            }
         }
         return false;
      }
      public void updateBet() {
         betField.setEditable(false);
         betLabel.setText("Bet: ");
         try {
            Integer i = new Integer(betField.getText());
            bet = i.intValue();
         } 
            catch (NumberFormatException ex) {
               bet = 1;
            }
         betField.setText(String.valueOf(bet));
         initialDeal();
         if(playersHand.blackjack()) playerWins();
         else state = PLAY;
      }
      void initialDeal() {
         playersHand = new Hand();
         dealersHand = new Hand();
         for(int i = 0;i dealersHand.bestScore()) playerWins();
         else if(playersHand.bestScore() < dealersHand.bestScore()) dealerWins();
         else tie();
      }
   }

   class Deck {
   // Variable declarations
      int cards

Though it is better if such programs are uploaded instead.
