//BlackJack.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class BlackJack extends JApplet { private JTextArea myCardsArea; private JLabel myTotalLabel; private JButton hitmeButton; private JButton holdButton; private JTextArea dealerCardsArea; private JLabel dealerTotalLabel; private LinkedList deck; private LinkedList myCardsImage; private LinkedList dealerCardsImage; private Card dealerDownCard; private int myTotal; private int dealerTotal; private int myTotalAce; //a 11 ace private int dealerTotalAce; public void init() { Container container = getContentPane(); container.setLayout( new FlowLayout() ); myCardsImage = new LinkedList(); dealerCardsImage = new LinkedList(); myCardsArea = new JTextArea( 6, 12 ); myCardsArea.setEditable( false ); //container.add( myCardsArea ); hitmeButton = new JButton( "Deal" ); container.add( hitmeButton ); hitmeButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { Card tempCard; int fValue; if (hitmeButton.getText().equals("Deal")) { myCardsImage.clear(); myCardsArea.setText( "" ); tempCard = (Card)deck.removeFirst(); fValue = tempCard.getFaceValue(); myTotal = fValue; if ( fValue == 1 ) //ace myTotalAce = 11; else myTotalAce = fValue; myCardsImage.add( tempCard.getImageIcon(BlackJack.this) ); myCardsArea.append( "" + tempCard + " " + tempCard.getFaceValue() + "\n"); tempCard = (Card)deck.removeFirst(); fValue = tempCard.getFaceValue(); if ( fValue==1 ) //ace if ( myTotal!=myTotalAce ) //2nd ace myTotalAce += 1; //must be 1 else //first ace myTotalAce += 11; else //not ace myTotalAce += fValue; myTotal += fValue; myCardsImage.add( tempCard.getImageIcon(BlackJack.this) ); myCardsArea.append( "" + tempCard + " " + tempCard.getFaceValue() + "\n"); if ( myTotalAce != myTotal ) myTotalLabel.setText( "" + myTotal + " or " + myTotalAce ); else myTotalLabel.setText( "" + myTotal ); dealerCardsImage.clear(); dealerCardsArea.setText( "" ); dealerDownCard = (Card)deck.removeFirst(); fValue = dealerDownCard.getFaceValue(); dealerTotal = fValue; if ( fValue == 1 ) //ace dealerTotalAce = 11; else dealerTotalAce = fValue; //dealerCardsImage.add( new ImageIcon( // getImage(getCodeBase(), "Cards/down.gif" ) )); dealerCardsImage.add( new ImageIcon( getClass().getResource( "Cards/down.gif" ) ) ); //dealerCardsImage.add( dealerDownCard.getImageIcon(BlackJack.this) ); dealerCardsArea.append( "" + dealerDownCard + " " + dealerDownCard.getFaceValue() + "\n"); tempCard = (Card)deck.removeFirst(); fValue = tempCard.getFaceValue(); if ( fValue==1 ) //ace if ( dealerTotalAce!=dealerTotal ) //2nd ace dealerTotalAce += 1; else //first ace dealerTotalAce += 11; else //not ace dealerTotalAce += fValue; dealerTotal += fValue; dealerCardsImage.add( tempCard.getImageIcon(BlackJack.this) ); dealerCardsArea.append( "" + tempCard + " " + tempCard.getFaceValue() + "\n"); if ( dealerTotalAce != dealerTotal ) dealerTotalLabel.setText( "" + dealerTotal + " or " + dealerTotalAce ); else dealerTotalLabel.setText( "" + dealerTotal ); hitmeButton.setText( "Hit me" ); holdButton.setEnabled( true ); if ( myTotalAce == 21 ) { myTotalLabel.setText( "BlackJack! You win!" ); hitmeButton.setText( "Deal" ); holdButton.setEnabled( false ); } } else { tempCard = (Card)deck.removeFirst(); //System.out.println(tempCard.toGifFilename()); fValue = tempCard.getFaceValue(); if ( fValue==1 ) //ace if ( myTotal!=myTotalAce ) //ace already played myTotalAce += 1; else //first ace myTotalAce += 11; else //not ace myTotalAce += fValue; myTotal += fValue; myCardsImage.add( tempCard.getImageIcon(BlackJack.this) ); myCardsArea.append( "" + tempCard + " " + tempCard.getFaceValue() + "\n"); if ( myTotal > 21 ) { //ace high will be busted too myTotalLabel.setText( "Busted" ); holdButton.setEnabled( false ); hitmeButton.setText( "Deal" ); } else if ( myTotalAce != myTotal && myTotalAce<=21 ) myTotalLabel.setText( "" + myTotal + " or " + myTotalAce ); else myTotalLabel.setText( "" + myTotal ); } repaint(); //syntax error: cannot reference non-static from static context: //if ( deck.size() < 20 ) BlackJack.start(); //ok? } } ); holdButton = new JButton( "Hold" ); holdButton.setEnabled( false ); container.add( holdButton ); holdButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { Card tempCard; int fValue; if ( ! hitmeButton.getText().equals("Deal") ) { dealerCardsImage.removeFirst(); //down card dealerCardsImage.addFirst( dealerDownCard.getImageIcon(BlackJack.this) ); repaint(); //sleep in loop is accumulative??? //try{ //slow him down // Thread.sleep( 1000 ); //} catch (InterruptedException e) {} while (dealerTotal<21 && max21( dealerTotal, dealerTotalAce ) < max21( myTotal, myTotalAce ) ) { tempCard = (Card)deck.removeFirst(); fValue = tempCard.getFaceValue(); if ( fValue==1 ) //ace if ( dealerTotalAce!=dealerTotal )//2+ ace dealerTotalAce += 1; else //first ace dealerTotalAce += 11; else //not ace dealerTotalAce += fValue; dealerTotal += fValue; dealerCardsImage.add( tempCard.getImageIcon(BlackJack.this) ); dealerCardsArea.append( "" + tempCard + " " + tempCard.getFaceValue() + "\n"); if ( dealerTotalAce!=dealerTotal && dealerTotalAce<=21 ) dealerTotalLabel.setText( "" + dealerTotal + " or " + dealerTotalAce ); else dealerTotalLabel.setText( "" + dealerTotal ); repaint(); } if (dealerTotal > 21) myTotalLabel.setText( "You win!" ); else myTotalLabel.setText( "You lose!" ); hitmeButton.setText( "Deal" ); holdButton.setEnabled( false ); } } } ); myTotalLabel = new JLabel( "" ); container.add( myTotalLabel ); dealerCardsArea = new JTextArea( 6, 12 ); dealerCardsArea.setEditable( false ); //container.add( dealerCardsArea ); dealerTotalLabel = new JLabel( ""); container.add( dealerTotalLabel ); } public void start() { deck = new LinkedList(); //initialize the deck of Cards //make it a 6 deck pack for (int d=1; d<=6; d++) for ( int i=0; i<52; i++ ) deck.add( new Card( Card.FACES[i%13], Card.SUITS[i/13] ) ); Collections.shuffle( deck ); // shuffle deck } public void paint( Graphics g ) { super.paint( g ); int x, y; g.drawString( "YOU", 0, getHeight()/4-20 ); Iterator iter = myCardsImage.iterator(); x = 0; y = getHeight()/4; while (iter.hasNext()) { ((ImageIcon)(iter.next())).paintIcon( this, g, x, y); x += 100; } g.drawString( "DEALER", 0, getHeight()/2-20 ); iter = dealerCardsImage.iterator(); x = 0; y = getHeight()/2; while (iter.hasNext()) { ((ImageIcon)(iter.next())).paintIcon( this, g, x, y); x += 100; } } //max of two ints but not over 21 private int max21( int i, int j ) { if ( i <= 21 ) if ( j <= 21 ) if ( i >= j ) return i; else return j; else return i; else if ( j <= 21 ) return j; else return 22; //both over 21 } } // class to represent a Card in a deck of cards //modified from Deitel's Cards.java class Card { public static final String SUITS[] = { "Hearts", "Clubs", "Diamonds", "Spades" }; public static final String FACES[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; private String face; // private int face; private String suit; // initialize a Card public Card( String initialface, String initialSuit ) { face = initialface; suit = initialSuit; } /* public Card( int initialface, String initialSuit ) { face = initialface; suit = initialSuit; } */ // return face of Card public String getFace() { return face; } // return face value of Card public int getFaceValue() { int i=0; while ( ! face.equals(FACES[i]) ) i++; return i<10 ? i+1 : 10; } // return suit of Card public String getSuit() { return suit; } // return String representation of Card public String toString() { StringBuffer buffer = new StringBuffer( face + " of " + suit ); buffer.setLength( 20 ); return buffer.toString(); } public String toGifFilename() { String f; if (getFaceValue() >= 10) { if (face.equals("Ten")) f = "10"; else if (face.equals("Jack")) f = "j"; else if (face.equals("Queen")) f = "q"; else // if (face.equals("King")) f = "k"; } else if (getFaceValue() == 1) f = "a"; else f = "" + getFaceValue(); return "Cards/" + f + suit.substring(0,1).toLowerCase() + ".gif"; } //needs the arg so can access getCodeBase... public ImageIcon getImageIcon( JApplet app ) { //System.out.print(""+app.getCodeBase() ); //jar? //System.out.println(""+toGifFilename() ); //browser: //return new ImageIcon( app.getCodeBase() + toGifFilename() ); return new ImageIcon( getClass().getResource( toGifFilename() ) ); // return new ImageIcon( app.getImage( app.getCodeBase(), toGifFilename() ) ); //appletviewer // return new ImageIcon( toGifFilename() ); } }