/* Programmer Zeb Mullen Date May 13, 1999 Program BlackJack */ import java.applet.*; import java.awt.*; import dlgBJ; import java.util.Hashtable; import java.awt.LayoutManager; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.FontMetrics; import java.awt.Insets; import java.awt.Label; import DialogLayout; public class BlackJack extends Applet /* Plays BlackJack with the computer*/ { int player,dealer,card,pnumber,dnumber,pstart,dstart,ppoint,dpoint, bet,total,dest1,dest2; dlgBJ gametable; boolean bPAce,bDAce; public void init() /*sets the game table and calls dialog box*/ { setBackground(Color.green); gametable=new dlgBJ(this); gametable.CreateControls(); bPAce=bDAce=false; pstart=dstart=ppoint=dpoint=total=0; gametable.bStay.hide(); } public void paint(Graphics g) /*Draws the cards*/ { g.setColor(Color.blue); for(int j=0;j<2;j++) { g.drawRect(15+60*j,43,40,50); } for(int i=0;i<5;i++) { g.drawRect(15+(60*i),125,40,50); } } public void getDealer() /*Gets the dealers remaining cards*/ { while (dpoint<16) { dstart++; dpoint=dpoint+(dealCard(dstart,dnumber)); gametable.lbDStat.setText("" + dpoint); } } public int getCard(int suit) /*Verifies if suit should be 10,11,or 1*/ { if (suit>9) { if ((suit%10)==0|(suit%11)==0|(suit%12)==0|(suit%13)==0) { suit=10; } } if (suit==1) { if ((ppoint+11)<22 | (dpoint+11)<22) { suit=11; } } return suit; } //end getCard public void getBet() { bet=Integer.parseInt(gametable.txtBet.getText()); gametable.lbMsg.setText(""); gametable.lbDStat.setText(""); } public int dealCard(int num,int cnum) /*Deals the card to the proper position*/ { int shuffle=0; player=0; for(int count=cnum;count=5) { dnumber=num; if (card==11) { bDAce=true; } } if (count<=4) { pnumber=num; if (card==11) { bPAce=true; } } //end if } //end for return player; } //end dealCard public void clearLab() /*Resets the game table*/ { gametable.bStay.hide(); gametable.bBet.setLabel("Bet"); ppoint=dpoint=0; bPAce=bDAce=false; gametable.txtBet.setEditable(true); gametable.lbCard0.setText(""); gametable.lbCard1.setText(""); gametable.lbCard2.setText(""); gametable.lbCard3.setText(""); gametable.lbCard4.setText(""); gametable.lbDeal0.setText(""); gametable.lbDeal1.setText(""); } public void verifyPoint() /*Checks to see who won or tie*/ { if ((ppoint==dpoint) && (ppoint<=21) && (dpoint<=21)) { gametable.lbMsg.setText("You Pushed"); clearLab(); } if ( (ppoint<22) && (ppoint>dpoint) | (dpoint>21)) { gametable.lbMsg.setText("You Win"); total+=bet; clearLab(); } if ((ppoint>21) | (dpoint>ppoint) && (dpoint<22)) { gametable.lbMsg.setText("You Lose"); total-=bet; clearLab(); } gametable.lbTotal.setText("$" + total); } public boolean action(Event e,Object o) { if (e.target.equals(gametable.bBet)) { if (gametable.bBet.getLabel().equals("Bet")) { pstart=2; dstart=7; pnumber=0; getBet(); if (bet>0) { gametable.txtBet.setEditable(false); ppoint=(dealCard(pstart,pnumber)); /*Deals the initial cards*/ gametable.lbPStat.setText("" + ppoint); dnumber=5; dpoint=(dealCard(dstart,dnumber)); gametable.bStay.show(); gametable.bBet.setLabel("Hit Me!"); } } else /*Deals the next card*/ { pstart++; showStatus("" + pstart + dstart); ppoint=ppoint+(dealCard(pstart,pnumber)); gametable.lbPStat.setText("" + ppoint); if ( ppoint>=22) { if (bPAce) { ppoint=ppoint-10; gametable.lbPStat.setText("" + ppoint); bPAce=false; } //end if else { verifyPoint(); } //end else } //end if } //end else } //end if if (e.target.equals(gametable.bStay)) /*Checks to see if player won*/ { gametable.lbDeal1.show(); getDealer(); if (dpoint>=22) { if (bDAce) { dpoint=-10; getDealer(); bDAce=false; } //end if } //end if verifyPoint(); } //end if return true; } //end action } //end class // This is a part of the Microsoft Visual J++ library. // Copyright (C) 1996 Microsoft Corporation // All rights reserved. // // class DialogLayout // // DialogLayout is a simple layout manager which works with what the Win32 // API calls "dialog logical units" (DLUs). DLUs are resolution independent // coordinates which work well for laying out controls on a dialog box. The // mapping from DLUs to pixels is based on the font in use in the dialog box. // An x-coordinate DLU is described as 1/4 (.25) of the of the average character // width of the font used in the dialog. A y-coordinate DLU is described as // 1/8 (.125) of the character height used in the dialog. One tricky issue to // note: The average character width is not the average of all characters -- // rather it is the average of all alpha characters both uppercase and // lowercase. That is, it is the extent of the string "a...zA...Z" divided // by 52. // // This class allows you to associate a Rectangle (x, y, width, height) with a // Component in a Container. If called upon to layout the container, this // layout manager will layout based on the translation of dialog units to // pixels. // class DialogLayout implements LayoutManager { protected Hashtable m_map = new Hashtable(); protected int m_width; protected int m_height; // DialogLayout methods public DialogLayout(Container parent, int width, int height) { Construct(parent, width, height); } public DialogLayout(Container parent, Dimension d) { Construct(parent, d.width, d.height); } public void setShape(Component comp, int x, int y, int width, int height) { m_map.put(comp, new Rectangle(x, y, width, height)); } public void setShape(Component comp, Rectangle rect) { m_map.put(comp, new Rectangle(rect.x, rect.y, rect.width, rect.height)); } public Rectangle getShape(Component comp) { Rectangle rect = (Rectangle)m_map.get(comp); return new Rectangle(rect.x, rect.y, rect.width, rect.height); } public Dimension getDialogSize() { return new Dimension(m_width, m_height); } // LayoutManager Methods public void addLayoutComponent(String name, Component comp) { } public void removeLayoutComponent(Component comp) { } public Dimension preferredLayoutSize(Container parent) { return new Dimension(m_width, m_height); } public Dimension minimumLayoutSize(Container parent) { return new Dimension(m_width, m_height); } public void layoutContainer(Container parent) { int count = parent.countComponents(); Rectangle rect = new Rectangle(); int charHeight = getCharHeight(parent); int charWidth = getCharWidth(parent); Insets insets = parent.insets(); FontMetrics m = parent.getFontMetrics(parent.getFont()); for (int i = 0; i < count; i++) { Component c = parent.getComponent(i); Rectangle r = (Rectangle)m_map.get(c); if (r != null) { rect.x = r.x; rect.y = r.y; rect.height = r.height; rect.width = r.width; mapRectangle(rect, charWidth, charHeight); if (c instanceof Label) { // Adjusts for space at left of Java labels. rect.x -= 12; rect.width += 12; } rect.x += insets.left; rect.y += insets.top; c.reshape(rect.x, rect.y, rect.width, rect.height); } } } // Implementation Helpers protected void Construct(Container parent, int width, int height) { Rectangle rect = new Rectangle(0, 0, width, height); mapRectangle(rect, getCharWidth(parent), getCharHeight(parent)); m_width = rect.width; m_height = rect.height; } protected int getCharWidth(Container parent) { FontMetrics m = parent.getFontMetrics(parent.getFont()); String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; int width = m.stringWidth(s) / s.length(); if (width <= 0) width = 1; return width; } protected int getCharHeight(Container parent) { FontMetrics m = parent.getFontMetrics(parent.getFont()); int height = m.getHeight(); return height; } protected void mapRectangle(Rectangle rect, int charWidth, int charHeight) { rect.x = (rect.x * charWidth) / 4; rect.y = (rect.y * charHeight) / 8; rect.width = (rect.width * charWidth) / 4; rect.height = (rect.height * charHeight) / 8; } } //------------------------------------------------------------------------------ // dlgBJ.java: // Implementation for container control initialization class dlgBJ // // WARNING: Do not modify this file. This file is recreated each time its // associated .rct/.res file is sent through the Java Resource Wizard! // // This class can be use to create controls within any container, however, the // following describes how to use this class with an Applet. For addtional // information on using Java Resource Wizard generated classes, refer to the // Visual J++ 1.1 documention. // // 1) Import this class in the .java file for the Applet that will use it: // // import dlgBJ; // // 2) Create an instance of this class in your Applet's 'init' member, and call // CreateControls() through this object: // // dlgBJ ctrls = new dlgBJ (this); // ctrls.CreateControls(); // // 3) To process events generated from user action on these controls, implement // the 'handleEvent' member for your applet: // // public boolean handleEvent (Event evt) // { // // } // //------------------------------------------------------------------------------ class dlgBJ { Container m_Parent = null; boolean m_fInitialized = false; DialogLayout m_Layout; // Control definitions //-------------------------------------------------------------------------- Button bBet; Button bStay; Label lbCard0; Label lbCard1; Label lbCard2; Label lbCard3; Label lbCard4; Label lbDeal0; Label lbDeal1; Label lbBank; Label lbTotal; TextField txtBet; Label lbMsg; Label lbPlayer; Label lbDealer; Label lbPStat; Label lbDStat; // Constructor //-------------------------------------------------------------------------- public dlgBJ (Container parent) { m_Parent = parent; } // Initialization. //-------------------------------------------------------------------------- public boolean CreateControls() { // Can only init controls once //---------------------------------------------------------------------- if (m_fInitialized || m_Parent == null) return false; // Parent must be a derivation of the Container class //---------------------------------------------------------------------- if (!(m_Parent instanceof Container)) return false; // Since there is no way to know if a given font is supported from // platform to platform, we only change the size of the font, and not // type face name. And, we only change the font if the dialog resource // specified a font. //---------------------------------------------------------------------- Font OldFnt = m_Parent.getFont(); if (OldFnt != null) { Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 8); m_Parent.setFont(NewFnt); } // All position and sizes are in Dialog Units, so, we use the // DialogLayout manager. //---------------------------------------------------------------------- m_Layout = new DialogLayout(m_Parent, 303, 145); m_Parent.setLayout(m_Layout); m_Parent.addNotify(); Dimension size = m_Layout.getDialogSize(); Insets insets = m_Parent.insets(); m_Parent.resize(insets.left + size.width + insets.right, insets.top + size.height + insets.bottom); // Control creation //---------------------------------------------------------------------- bStay = new Button ("Stay"); m_Parent.add(bStay); m_Layout.setShape(bStay, 133,114,40,14); bBet = new Button ("Bet"); m_Parent.add(bBet); m_Layout.setShape(bBet, 183, 114, 40, 14); lbCard0 = new Label ("", Label. CENTER); m_Parent.add(lbCard0); m_Layout.setShape(lbCard0, 25, 85, 8, 8); lbCard1 = new Label ("", Label.RIGHT); m_Parent.add(lbCard1); m_Layout.setShape(lbCard1, 60, 85, 8, 8); lbCard2 = new Label ("", Label.RIGHT); m_Parent.add(lbCard2); m_Layout.setShape(lbCard2, 100, 85, 8, 8); lbCard3 = new Label ("", Label.RIGHT); m_Parent.add(lbCard3); m_Layout.setShape(lbCard3, 140, 85, 8, 8); lbCard4 = new Label ("", Label.RIGHT); m_Parent.add(lbCard4); m_Layout.setShape(lbCard4, 180, 85, 8, 8); lbDeal0 = new Label ("", Label.RIGHT); m_Parent.add(lbDeal0); m_Layout.setShape(lbDeal0, 25, 38, 8, 8); lbDeal1 = new Label ("", Label.RIGHT); m_Parent.add(lbDeal1); m_Layout.setShape(lbDeal1, 60, 38, 8 ,8); lbBank = new Label ("Bank", Label.CENTER); m_Parent.add(lbBank); m_Layout.setShape(lbBank, 202, 10, 41, 10); lbTotal = new Label ("$0", Label.CENTER); m_Parent.add(lbTotal); m_Layout.setShape(lbTotal, 206, 24, 32, 9); txtBet = new TextField (""); txtBet.setBackground(Color.white); m_Parent.add(txtBet); m_Layout.setShape(txtBet, 38, 114, 48, 14); lbMsg = new Label ("Place Your Bet", Label.CENTER); m_Parent.add(lbMsg); m_Layout.setShape(lbMsg, 185, 46, 50, 20); lbPlayer = new Label ("Player", Label.CENTER); m_Parent.add(lbPlayer); m_Layout.setShape(lbPlayer, 25, 60, 15, 13); lbDealer = new Label ("Dealer", Label.CENTER); m_Parent.add(lbDealer); m_Layout.setShape(lbDealer, 25, 10, 15, 13); lbPStat = new Label ("", Label.CENTER); m_Parent.add(lbPStat); m_Layout.setShape(lbPStat, 45, 60, 10, 13); lbDStat = new Label ("", Label.CENTER); m_Parent.add(lbDStat); m_Layout.setShape(lbDStat, 45, 10, 10, 13); m_fInitialized = true; return true; } }