Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Nees help with BlackJack game!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nelsonkkyy




PostPosted: Mon Jan 17, 2011 11:07 am   Post subject: 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.

Quote:

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<2;++i) {
playersHand.addCard(deck.deal());
dealersHand.addCard(deck.deal());
}
dealersHand.show(dealerText,true);
playersHand.show(playerText,false);
}
void openBetting() {
betLabel.setText("Enter your bet: ");
betField.setText("");
betField.setEditable(true);
state = BET;
}
void playerWins() {
money += bet;
topLine.setText("Player wins $"+bet+".");
totalLine.setText("You have $"+money+".");
openBetting();
}
void dealerWins() {
money -= bet;
topLine.setText("Player loses $"+bet+".");
totalLine.setText("You have $"+money+".");
openBetting();
}
void tie() {
topLine.setText("Tie.");
totalLine.setText("You have $"+money+".");
openBetting();
}
void showResults() {
if(playersHand.busted() && dealersHand.busted()) tie();
else if(playersHand.busted()) dealerWins();
else if(dealersHand.busted()) playerWins();
else if(playersHand.bestScore() > dealersHand.bestScore()) playerWins();
else if(playersHand.bestScore() < dealersHand.bestScore()) dealerWins();
else tie();
}
}

class Deck {
// Variable declarations
int cards[]; // Array of 52 cards
int topCard; // 0-51 (index of card in deck)
Random random;

// Method declarations
public Deck() { // Constructor
cards = new int[52];
for(int i = 0;i<52;++i) cards[i] = i;
topCard = 0;
random = new Random();
shuffle();
}

public void shuffle() {
// Repeat 52 times
for(int i = 0;i<52;++i) {
// Randomly exchange two cards in the deck.
int j = randomCard();
int k = randomCard();
int temp = cards[j];
cards[j] = cards[k];
cards[k] = temp;
}
}

int randomCard() {
int r = random.nextInt();
if(r<0) r = 0-r;
return r%52;
}

Card deal() {
if(topCard>51) {
shuffle();
topCard = 0;
}
Card card = new Card(cards[topCard]);
++topCard;
return card;
}
} // End of Deck class

class Hand {
// Variable declarations
int numCards;
Card cards[];
static int MaxCards = 12;

//Method declarations
public Hand() { // Constructor
numCards = 0;
cards = new Card[MaxCards];
}

void addCard(Card c) {
cards[numCards] = c;
++numCards;
}

void show(TextArea t,boolean hideFirstCard) {
String results = "";
for(int i = 0;i<numCards;++i) {
if(i == 0 && hideFirstCard) results += "Hidden\n";
else results += cards[i].value+" of "+cards[i].suite+"\n";
}
t.setText(results);
}

boolean blackjack() {
if(numCards == 2) {
if(cards[0].iValue == 1 && cards[1].iValue == 10) return true;
if(cards[1].iValue == 1 && cards[0].iValue == 10) return true;
}
return false;
}

boolean under(int n) {
int points = 0;
for(int i = 0;i<numCards;++i) points += cards[i].iValue;
if(points<n) return true;
else return false;
}

int bestScore() {
int points = 0;
boolean haveAce = false;
for(int i = 0;i<numCards;++i) {
points += cards[i].iValue;
if(cards[i].iValue == 1) haveAce = true;
}
if(haveAce) {
if(points+10 < 22) points += 10;
}
return points;
}

boolean mustHit() {
if(bestScore()<17) return true;
else return false;
}

boolean busted() {
if(!under(22)) return true;
else return false;
}
} // End of Hand class

class Card {
// Variable declarations
int iValue; // Numeric value corresponding to card.
String value; // "A" "2" through "9" "T" "J" "Q" "K"
String suite; // "S" "H" "C" "D"

// Method declarations
public Card(int n) { // Constructor
int iSuite = n/13;
iValue = n%13+1;
switch(iSuite) {
case 0:
suite = "Spades";
break;
case 1:
suite = "Hearts";
break;
case 2:
suite = "Clubs";
break;
default:
suite = "Diamonds";
}
if(iValue == 1) value = "Ace";
else if(iValue == 10) value = "Ten";
else if(iValue == 11) value = "Jack";
else if(iValue == 12) value = "Queen";
else if(iValue == 13) value = "King";
else value = Integer.toString(iValue);
if(iValue>10) iValue = 10;
}

int getValue() {
return iValue;
}
} // End of Card class







Thanks!
Sponsor
Sponsor
Sponsor
sponsor
ProgrammingFun




PostPosted: Mon Jan 17, 2011 8:07 pm   Post subject: RE:Nees help with BlackJack game!

Please post your code in syntax tags with indentation:

Java:

   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<2;++i) {
            playersHand.addCard(deck.deal());
            dealersHand.addCard(deck.deal());
         }
         dealersHand.show(dealerText,true);
         playersHand.show(playerText,false);
      }
      void openBetting() {
         betLabel.setText("Enter your bet: ");
         betField.setText("");
         betField.setEditable(true);
         state = BET;
      }
      void playerWins() {
         money += bet;
         topLine.setText("Player wins $"+bet+".");
         totalLine.setText("You have $"+money+".");
         openBetting();
      }
      void dealerWins() {
         money -= bet;
         topLine.setText("Player loses $"+bet+".");
         totalLine.setText("You have $"+money+".");
         openBetting();
      }
      void tie() {
         topLine.setText("Tie.");
         totalLine.setText("You have $"+money+".");
         openBetting();
      }
      void showResults() {
         if(playersHand.busted() && dealersHand.busted()) tie();
         else if(playersHand.busted()) dealerWins();
         else if(dealersHand.busted()) playerWins();
         else if(playersHand.bestScore() > dealersHand.bestScore()) playerWins();
         else if(playersHand.bestScore() < dealersHand.bestScore()) dealerWins();
         else tie();
      }
   }

   class Deck {
   // Variable declarations
      int cards[]; // Array of 52 cards
      int topCard; // 0-51 (index of card in deck)
      Random random;
   
   // Method declarations
      public Deck() { // Constructor
         cards = new int[52];
         for(int i = 0;i<52;++i) cards[i] = i;
         topCard = 0;
         random = new Random();
         shuffle();
      }
   
      public void shuffle() {
      // Repeat 52 times
         for(int i = 0;i<52;++i) {
         // Randomly exchange two cards in the deck.
            int j = randomCard();
            int k = randomCard();
            int temp = cards[j];
            cards[j] = cards[k];
            cards[k] = temp;
         }
      }
   
      int randomCard() {
         int r = random.nextInt();
         if(r<0) r = 0-r;
         return r%52;
      }
   
      Card deal() {
         if(topCard>51) {
            shuffle();
            topCard = 0;
         }
         Card card = new Card(cards[topCard]);
         ++topCard;
         return card;
      }
   } // End of Deck class

   class Hand {
   // Variable declarations
      int numCards;
      Card cards[];
      static int MaxCards = 12;
   
   //Method declarations
      public Hand() { // Constructor
         numCards = 0;
         cards = new Card[MaxCards];
      }
   
      void addCard(Card c) {
         cards[numCards] = c;
         ++numCards;
      }
   
      void show(TextArea t,boolean hideFirstCard) {
         String results = "";
         for(int i = 0;i<numCards;++i) {
            if(i == 0 && hideFirstCard) results += "Hidden\n";
            else results += cards[i].value+" of "+cards[i].suite+"\n";
         }
         t.setText(results);
      }
   
      boolean blackjack() {
         if(numCards == 2) {
            if(cards[0].iValue == 1 && cards[1].iValue == 10)
               return true;
            if(cards[1].iValue == 1 && cards[0].iValue == 10)
               return true;
         }
         return false;
      }
   
      boolean under(int n) {
         int points = 0;
         for(int i = 0;i<numCards;++i) points += cards[i].iValue;
         if(points<n)
            return true;
         else
            return false;
      }
   
      int bestScore() {
         int points = 0;
         boolean haveAce = false;
         for(int i = 0;i<numCards;++i) {
            points += cards[i].iValue;
            if(cards[i].iValue == 1) haveAce = true;
         }
         if(haveAce) {
            if(points+10 < 22) points += 10;
         }
         return points;
      }
   
      boolean mustHit() {
         if(bestScore()<17)
            return true;
         else
            return false;
      }
   
      boolean busted() {
         if(!under(22))
            return true;
         else
            return false;
      }
   } // End of Hand class

   class Card {
   // Variable declarations
      int iValue; // Numeric value corresponding to card.
      String value; // "A" "2" through "9" "T" "J" "Q" "K"
      String suite; // "S" "H" "C" "D"
   
   // Method declarations
      public Card(int n) { // Constructor
         int iSuite = n/13;
         iValue = n%13+1;
         switch(iSuite) {
            case 0:
               suite = "Spades";
               break;
            case 1:
               suite = "Hearts";
               break;
            case 2:
               suite = "Clubs";
               break;
            default:
               suite = "Diamonds";
         }
         if(iValue == 1) value = "Ace";
         else if(iValue == 10) value = "Ten";
         else if(iValue == 11) value = "Jack";
         else if(iValue == 12) value = "Queen";
         else if(iValue == 13) value = "King";
         else value = Integer.toString(iValue);
         if(iValue>10) iValue = 10;
      }
   
      int getValue() {
         return iValue;
      }
   } // End of Card class


Though it is better if such programs are uploaded instead.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: