// DumbGame.java - John K. Estell - 10 May 2003 // Last modified: 23 Febraury 2004 // Demonstration program for the basic extension of the Hand class //package cardgame; import java.awt.*; import javax.swing.*; import java.util.*; /** * DumbGame is exactly that - a dumb card game. Player is dealt eight cards from a hand, * upon which the player can choose the exciting options of a rank-major or a suit-major * sort. Each card is worth its pip value in points(ace = 1, two = 2, etc.), with the * face cards (jack, queen and king) each worth ten points. The value of a hand is the * summation of the point values of the cards. Purpose of this game is to exercise the * sorting methods of the cards and the evaluation methods of the hand. */ public class DumbGame extends JApplet { private Deck cardDeck; private DumbGameHand myHand; private final int SIZE_OF_HAND = 8; private final String directory = "cards/"; private JLabel[] handLbl = new JLabel[ SIZE_OF_HAND ]; public void init() { // This line prevents the "Swing: checked access to system event queue" message seen in some browsers. getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); // This code is automatically generated by Visual Cafe when you add // components to the visual environment. It instantiates and initializes // the components. To modify the code, only use code syntax that matches // what Visual Cafe can generate, or Visual Cafe may be unable to back // parse your Java file into its visual environment. //{{INIT_CONTROLS getContentPane().setLayout(null); getContentPane().setBackground(java.awt.Color.cyan); setSize(881,203); JLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel1.setText("Card"); JLabel1.setOpaque(true); JLabel1.setToolTipText("This is a card."); getContentPane().add(JLabel1); JLabel1.setForeground(java.awt.Color.black); JLabel1.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel1.setBounds(12,12,101,125); JLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel2.setText("Card"); JLabel2.setOpaque(true); JLabel2.setToolTipText("This is a card."); getContentPane().add(JLabel2); JLabel2.setForeground(java.awt.Color.black); JLabel2.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel2.setBounds(120,12,101,125); JLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel3.setText("Card"); JLabel3.setOpaque(true); JLabel3.setToolTipText("This is a card."); getContentPane().add(JLabel3); JLabel3.setForeground(java.awt.Color.black); JLabel3.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel3.setBounds(228,12,101,125); JLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel4.setText("Card"); JLabel4.setOpaque(true); JLabel4.setToolTipText("This is a card."); getContentPane().add(JLabel4); JLabel4.setForeground(java.awt.Color.black); JLabel4.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel4.setBounds(336,12,101,125); JLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel5.setText("Card"); JLabel5.setOpaque(true); JLabel5.setToolTipText("This is a card."); getContentPane().add(JLabel5); JLabel5.setForeground(java.awt.Color.black); JLabel5.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel5.setBounds(444,12,101,125); JButton1.setText("Draw a Hand"); JButton1.setActionCommand("Draw a Hand"); getContentPane().add(JButton1); JButton1.setBounds(12,156,212,32); JButton2.setText("Perform Rank-Major Sort"); JButton2.setActionCommand("Sort the Hand"); getContentPane().add(JButton2); JButton2.setBounds(228,156,200,31); JLabel6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel6.setText("Card"); JLabel6.setOpaque(true); JLabel6.setToolTipText("This is a card."); getContentPane().add(JLabel6); JLabel6.setForeground(java.awt.Color.black); JLabel6.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel6.setBounds(552,12,101,125); JLabel7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel7.setText("Card"); JLabel7.setOpaque(true); JLabel7.setToolTipText("This is a card."); getContentPane().add(JLabel7); JLabel7.setForeground(java.awt.Color.black); JLabel7.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel7.setBounds(660,12,101,125); JLabel8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel8.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel8.setText("Card"); JLabel8.setOpaque(true); JLabel8.setToolTipText("This is a card."); getContentPane().add(JLabel8); JLabel8.setForeground(java.awt.Color.black); JLabel8.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel8.setBounds(768,12,101,125); scoreLbl.setToolTipText("Using blackjack scoring with all aces = 1"); getContentPane().add(scoreLbl); scoreLbl.setForeground(java.awt.Color.black); scoreLbl.setBounds(636,156,204,29); JButton3.setText("Perform Suit-Major Sort"); JButton3.setActionCommand("Sort the Hand"); getContentPane().add(JButton3); JButton3.setBounds(432,156,200,31); //}} // add the JLabel array mapping here handLbl[0] = JLabel1; handLbl[1] = JLabel2; handLbl[2] = JLabel3; handLbl[3] = JLabel4; handLbl[4] = JLabel5; handLbl[5] = JLabel6; handLbl[6] = JLabel7; handLbl[7] = JLabel8; // add the Card instantiations here cardDeck = new Deck(); Iterator suitIterator = Suit.VALUES.iterator(); while ( suitIterator.hasNext() ) { Suit suit = (Suit) suitIterator.next(); Iterator rankIterator = Rank.VALUES.iterator(); while ( rankIterator.hasNext() ) { Rank rank = (Rank) rankIterator.next(); String imageFile = directory + Card.getFilename( suit, rank ); ImageIcon cardImage = new ImageIcon( getImage( getCodeBase(), imageFile ) ); Card card = new Card( suit, rank, cardImage ); cardDeck.addCard( card ); } } // set up the initial hand myHand = new DumbGameHand(); for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card drawnCard = cardDeck.dealCard(); myHand.addCard( drawnCard ); } // add the displaying of cards here for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card c = myHand.getCard( i ); handLbl[i].setIcon( c.getCardImage() ); handLbl[i].setText( c.toString() ); } scoreLbl.setText( "Score is: " + myHand.evaluateHand() ); //{{REGISTER_LISTENERS SymAction lSymAction = new SymAction(); JButton1.addActionListener(lSymAction); JButton2.addActionListener(lSymAction); JButton3.addActionListener(lSymAction); //}} } //{{DECLARE_CONTROLS javax.swing.JLabel JLabel1 = new javax.swing.JLabel(); javax.swing.JLabel JLabel2 = new javax.swing.JLabel(); javax.swing.JLabel JLabel3 = new javax.swing.JLabel(); javax.swing.JLabel JLabel4 = new javax.swing.JLabel(); javax.swing.JLabel JLabel5 = new javax.swing.JLabel(); javax.swing.JButton JButton1 = new javax.swing.JButton(); javax.swing.JButton JButton2 = new javax.swing.JButton(); javax.swing.JLabel JLabel6 = new javax.swing.JLabel(); javax.swing.JLabel JLabel7 = new javax.swing.JLabel(); javax.swing.JLabel JLabel8 = new javax.swing.JLabel(); javax.swing.JLabel scoreLbl = new javax.swing.JLabel(); javax.swing.JButton JButton3 = new javax.swing.JButton(); //}} class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == JButton1) JButton1_actionPerformed(event); else if (object == JButton2) JButton2_actionPerformed(event); else if (object == JButton3) JButton3_actionPerformed(event); } } void JButton1_actionPerformed(java.awt.event.ActionEvent event) { // to do: code goes here. cardDeck.restoreDeck(); cardDeck.shuffle(); myHand.discardHand(); for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card c = cardDeck.dealCard(); myHand.addCard( c ); handLbl[i].setIcon( c.getCardImage() ); handLbl[i].setText( c.toString() ); } scoreLbl.setText( "Score is: " + myHand.evaluateHand() ); } void JButton2_actionPerformed(java.awt.event.ActionEvent event) { Card.setRankMajorSort(); myHand.sort(); for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card c = myHand.getCard( i ); handLbl[i].setIcon( c.getCardImage() ); handLbl[i].setText( c.toString() ); } } void JButton3_actionPerformed(java.awt.event.ActionEvent event) { Card.setSuitMajorSort(); myHand.sort(); for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card c = myHand.getCard( i ); handLbl[i].setIcon( c.getCardImage() ); handLbl[i].setText( c.toString() ); } } }