-
BlackJack Game help
if you could explain this program to me...line by line...or by commenting it...please help me...
BLACK JACK MAIN
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Blackjack extends Applet implements ActionListener,Runnable
{
private Deck deck; //The deck...
private Hand player_hand;//player hand
private Hand dealer_hand;//dealer hand.
// buttons
private Button bHit=new Button("Hit");
private Button bStay=new Button("Stay");
private Button bRestart=new Button("New Game");
//double buffering....better preformance
Image offIm=null;//null command takes no arguments and returns an object handle that represents the "null" value in Java
Graphics offGraphics=null;
Dimension offDimension=null;
Image[] card_images = new Image[52];
private boolean cards_loaded = false;
private int current_card_loading = 0;
String message; //print a message...
int width; //The width of the applet
int height; //The height of the applet
int card_width = -1;
int card_padding = -1; //card padding
private int games=-1; //games played
public void init()
{
// card loading thread...from internet
System.out.println("Starting card loading thread...");
Thread card_loader = new Thread(this);
card_loader.start();
}
// for new game
public void newGame()
{
bHit.setEnabled(true);// lets hit button for use
bStay.setEnabled(false);//disables stay button..because of new game
player_hand = new Hand(); //Create new hands...
dealer_hand = new Hand();
//shuffle the deck.
deck.shuffleCards();
games++;
}
//hit
public void hit()
{
bStay.setEnabled(true);
player_hand.addCard(deck.dealCard());
if(player_hand.getValue() > 21)
{
message = "You loose! (score:"+player_hand.getValue()+")";
//disable the hit and stay buttons...
bHit.setEnabled(false);
bStay.setEnabled(false);
return;
}
message = "Score: "+player_hand.getValue();
}
// for stay button
public void stay(){
while(dealer_hand.getValue() < 17){
dealer_hand.addCard(deck.dealCard());// deal card to player
}
if(dealer_hand.getValue() <= 21 && player_hand.getValue() < dealer_hand.getValue())
{
message = "You loose! (" + player_hand.getValue()+
" - "+dealer_hand.getValue()+")";
}else if (player_hand.getValue() == dealer_hand.getValue())
{
message = "You draw! (" + player_hand.getValue()+")";
}else {
message = "You win! (" + player_hand.getValue()+
" - "+dealer_hand.getValue()+")";
}
bHit.setEnabled(false);
bStay.setEnabled(false);
}
// from internet
public void actionPerformed(ActionEvent e) {
if(e.getSource() == bRestart)
{
newGame();
}else if (e.getSource() == bHit)
{
hit();
}else if (e.getSource() == bStay)
{
stay();
}
repaint();
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
//sort out double buffering
if(offGraphics==null){
offIm=createImage(getSize().width,getSize().height);
offGraphics=offIm.getGraphics();
}
if(!cards_loaded){
//display a message saying we're loading the cards...
int left_offset = 20;
//offGraphics.setFont(new Font("Arial",Font.PLAIN,14));
offGraphics.setColor(new Color(171,205,239));
offGraphics.fillRect(0,0,getSize().width,getSize().height);
offGraphics.setColor(Color.black);
offGraphics.drawString("Loading cards...", 5 + left_offset,20);
offGraphics.drawRect(15 + left_offset,40, 102 ,10);
offGraphics.drawRect(13 + left_offset,38, 106 ,14);
offGraphics.setColor(new Color(171,205,239));
offGraphics.fillRect(0 + left_offset,0,150,35);
offGraphics.setColor(Color.black);
offGraphics.fillRect(15 + left_offset,40, (current_card_loading)*2 ,10);
offGraphics.drawString("Loading card: "+(current_card_loading+1),15 + left_offset,20);
//^internet
}else{
Image currentCard;
card_width = deck.getCard(0).getImage().getWidth(this);
if(card_padding == -1)
{
card_padding = (width - (card_width * 2) - 4) / 4;
}
//clear the background...
offGraphics.setColor(Color.blue);
offGraphics.fillRect(0,0,width,height);
offGraphics.setColor(Color.black);
offGraphics.fillRect(1,1,width-2,height-2);
offGraphics.setColor(Color.white);
// offGraphics.drawString("v2.0.1",0,0);
offGraphics.drawString("PLAYER:",card_padding,40);
offGraphics.drawString("DEALER:",(width/2) + card_padding,40);
offGraphics.drawString(message,5,height - 20);
if(games > 0)
{
offGraphics.drawString(games + " game(s) played...",5,height - 8);
}
//Draw the players hand...
for(int i=0;i<player_hand.getCardCount();i++){
currentCard = player_hand.getCards()[i].getImage();
offGraphics.drawImage(currentCard, card_padding, 70+(20*(i-1)), this);
}
//now draw the dealers hand...
for(int i=0;i<dealer_hand.getCardCount();i++){
currentCard = dealer_hand.getCards()[i].getImage();
offGraphics.drawImage(currentCard, (width/2 ) + card_padding, 70+(20*(i-1)), this);
}
}
//draw buffered image.
g.drawImage(offIm,0,0,this);
}
public void run(){
MediaTracker t=new MediaTracker(this);
System.out.println("Applet getting cards...");
String imageDir = getStringParam("imageDir","./cards/");
for(current_card_loading=0; current_card_loading < 52; current_card_loading++){
//try{
card_images[current_card_loading] = getImage(getCodeBase(),
imageDir + (current_card_loading+1) + ".gif");
if(card_images[current_card_loading] == null){
System.out.println("Null card... ["+current_card_loading+"]");
}else{
t.addImage(card_images[current_card_loading],0);
}
try{
t.waitForID(0);
}catch(InterruptedException e){
System.err.println("Interrupted waiting for images..>");
}
//lets show our new status.
repaint();
}
//create the deck object now
deck = new Deck(this,card_images); //Create a new deck object.
card_width = deck.getCard(0).getImage().getWidth(this);
bHit.addActionListener(this);
bStay.addActionListener(this);
bRestart.addActionListener(this);
bHit.setEnabled(false);
bStay.setEnabled(false);
width = getSize().width;
height = getSize().height;
this.add(bHit);
this.add(bStay);
this.add(bRestart);
player_hand = new Hand(); //Create the hands...
dealer_hand = new Hand();
//make sure that the buttons appear properly.
this.validate();
message = "";
cards_loaded = true;
System.out.println("End of thread...");
repaint();
}
private String getStringParam(String param,String defaultValue){
String ans = getParameter(param);
if(ans == null){
ans = defaultValue;
System.out.println("Parameter "+param+" not found. Setting to default of:"+defaultValue+".");
}
return ans;
}
}
HAND CLASS
class Hand {
private Card cards[] = new Card[10];
private int count;
private int value;
public void Hand(){
count=0;
value=0;
}
public Card[] getCards(){
return cards;
}
public int getCardCount(){
return count;
}
public void addCard(Card c) {
cards[count] = c;
value += cards[count].getValue();
if(c.getValue() == 11 && value > 21)
{ //got and ace, so decrement the value of the hand by 10, if it
//would have been above 21...
value-=10;
}
count++;
}
public int getValue() {
return value;
}
public boolean ace() {
int aceIndex = 0;
boolean result = false;
for (int i=0; i<count; i++) {
if (cards[i].getValue() == 11) {
value-=10;
return true;
}
}
return false;
}
}
DECK CLASS
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
class Deck {
Card[] cards = new Card[52];
int currCard;
public Deck(Blackjack myApplet,Image[] card_images) {
// These are the (initial) values of all the cards
int[] values = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
// Make all the cards, getting the images individually
for (int i=0; i<52; i++) {
// Image image = myApplet.getImage(myApplet.getCodeBase(), "cards/" + (i+1) + ".gif");
Image image = myApplet.getImage(myApplet.getCodeBase(), "cards/" + (i+1) + "1.gif");
cards[i] = new Card(card_images[i], values[i]);
}
// Shuffle the cards.
shuffleCards();
}
public void shuffleCards(){
Card temp;
//loop through each card in the deck and swap it with some random card.
for (int i=0; i<52; i++) {
int rand = (int)(Math.random()*52);
temp = cards[51-i];//take the current card...
cards[51-i] = cards[rand];//and swap it with some random card...
cards[rand] = temp;
}
// reset the currentCard to deal...
currCard = 0;
}
public Card dealCard() {
Card card = cards[currCard];
currCard++;
return card;
}
public Card getCard(int i) {
return cards[i];
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks