★ wanayoo — archive 1999 http://www.webtechniques.com/sourcecode/1998/12/java/java.lstNouvelle recherche | Portail wanayoo

 
| home | current issue | archives | source code | events | demo express | search | editorial calendar | advertising | customer service | author guidelines | jobs | about

S O U R C E   C O D E / 1998 / 12 / java / java.lst

"Java@Work: Gambling with Visual J++ 6.0"
by Al Williams
Web Techniques, December 1998

Web Techniques grants permission to use these listings (and code) for 
private or commercial use provided that credit to Web Techniques and 
the author is maintained within the comments of the source. For 
questions, contact editors@web-techniques.com. 



[LISTING ONE]

// Slot machine -- Williams
import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;

/**
 * This class can take a variable number of parameters on the command
 * line. Program execution begins with the main() method. The class
 * constructor is not invoked unless an object of type 'Form1' is
 * created in the main() method.
 */
public class Form1 extends Form
{
   Image pixs[];   // this array will hold the images
   boolean wheel1; // wheel flags are true when spinning
   boolean wheel2;
   boolean wheel3;
   int cash=100;   // your bank balance
   int spincount;  // # of spins (used to set minimum)
   int slot[]=new int[3];  // wheels

   public Form1()
   {
      // Required for Visual J++ Form Designer support
      initForm();      

      // TODO: Add any constructor code after initForm call
// Set up some initial stuff
      bank.setText(Integer.toString(cash));
      pixs=new Image[5];
      pixs[0]=pix0.getImage();
      pixs[1]=pix1.getImage();
      pixs[2]=pix2.getImage();
      pixs[3]=pix3.getImage();
      pixs[4]=pix4.getImage();
      slot0.setImage(pixs[1]);
      slot1.setImage(pixs[2]);
      slot2.setImage(pixs[3]);
      slot[0]=1;
      slot[1]=2;
      slot[2]=3;
      
   }

   /**
    * Form1 overrides dispose so it can clean up the
    * component list.
    */
   public void dispose()
   {
      super.dispose();
      components.dispose();
   }

   // Time to spin
   private void spin_click(Object source, Event e)
   {
      spin.setEnabled(false);  // disable button
      wheel1=true;  // enable all spinning
      wheel2=true;
      wheel3=true;
      cash=cash-1;  // cost you a buck
      bank.setText(Integer.toString(cash));
      status.setText("");
// turn on the timer
      timer1.setInterval(100);
      spincount=0;
      timer1.setEnabled(true);
   }

// This routine gets the next image to show
// I experimented with several algorithms
// here and decided to just make them "rotate"
// in sequence
   private int getAnImage(int current)
   {
//      int n=(int)(Math.random()*10);
// make lemons and blanks more common than anything
//      if (n>=8) n=3;
//      if (n>=5) n=0;
//      return n;
   return (current+1)%5;
   }

// When the timer fires an event, spin
   private void timer1_timer(Object source, Event e)
   {
      // slow down on each cycle
      timer1.setInterval(timer1.getInterval()+10);
// play "click" noise
      Win32.sndPlaySound("click.wav",Win32.SND_ASYNC);
      if (wheel3) slot2.setImage(pixs[slot[2]=getAnImage(slot[2])]);
      if (wheel2) slot1.setImage(pixs[slot[1]=getAnImage(slot[1])]);
      if (wheel1) slot0.setImage(pixs[slot[0]=getAnImage(slot[0])]);
// Spin at least 30 times
      if (++spincount<30) return;
// See if any wheels should stop this time
      if (wheel1)
      {
         if (Math.random()<.3) wheel1=false;
         return;
      }
      if (wheel2)
      {
         if (Math.random()<.3) wheel2=false;
         return;
      }
// If third wheel stops, game's over
      if (wheel3)
      {
         if (Math.random()<.3) 
         {
            int ct=0;
            int reward=0;
            wheel3=false;
            // check for win
            if (pix1.getImage()==slot0.getImage()) ct++;
            if (pix1.getImage()==slot1.getImage()) ct++;
            if (pix1.getImage()==slot2.getImage()) ct++;
            // generous payoffs!
            // big win? bar bar bar
            // sm win? any two bar
            // push? any bar
            if (ct!=0) 
               reward=(int)(5*Math.pow(10.0,ct-1));
            else
            // $5 for any three the same
               if (slot0.getImage()==slot1.getImage()
                && slot1.getImage()==slot2.getImage())
                  reward=5;
         // pay off
            cash+=reward;
            bank.setText(Integer.toString(cash));
         // set status
            if (reward==0)
               status.setText("Lost");
            else
               status.setText("Won $"+reward);
            timer1.setEnabled(false);
            spin.setEnabled(true);
         }
      }
   }

   

   /**
    * NOTE: The following code is required by the Visual J++ form
    * designer.  It can be modified using the form editor.  Do not
    * modify it using the code editor.
    */
   Container components = new Container();
   PictureBox pix1 = new PictureBox();
   PictureBox pix2 = new PictureBox();
   PictureBox pix0 = new PictureBox();
   PictureBox pix3 = new PictureBox();
   PictureBox pix4 = new PictureBox();
   PictureBox slot0 = new PictureBox();
   PictureBox slot1 = new PictureBox();
   PictureBox slot2 = new PictureBox();
   Label bank = new Label();
   Button spin = new Button();
   Timer timer1 = new Timer(components);
   Label status = new Label();

   private void initForm()
   {
      // NOTE:  This form is storing resource information in an
      // external file.  Do not modify the string parameter to any
      // resources.getObject() function call.  For example, do not
      // modify "foo1_location" in the following line of code
      // even if the name of the Foo object changes: 
      //    foo1.setLocation((Point)resources.getObject("foo1_location"));

      IResourceManager resources = new ResourceManager(this, "Form1");
      this.setText("Web Tech Bandit!");
      this.setAutoScaleBaseSize(13);
      this.setClientSize(new Point(471, 202));

      pix1.setEnabled(false);
      pix1.setLocation(new Point(88, 208));
      pix1.setSize(new Point(100, 50));
      pix1.setTabIndex(4);
      pix1.setTabStop(false);
      pix1.setText("pictureBox1");
      pix1.setImage((Bitmap)resources.getObject("pix1_image"));

      pix2.setEnabled(false);
      pix2.setLocation(new Point(200, 208));
      pix2.setSize(new Point(100, 50));
      pix2.setTabIndex(3);
      pix2.setTabStop(false);
      pix2.setText("pictureBox1");
      pix2.setImage((Bitmap)resources.getObject("pix2_image"));

      pix0.setEnabled(false);
      pix0.setLocation(new Point(-8, 216));
      pix0.setSize(new Point(100, 50));
      pix0.setTabIndex(2);
      pix0.setTabStop(false);
      pix0.setText("pictureBox1");
      pix0.setImage((Bitmap)resources.getObject("pix0_image"));

      pix3.setEnabled(false);
      pix3.setLocation(new Point(296, 208));
      pix3.setSize(new Point(100, 50));
      pix3.setTabIndex(1);
      pix3.setTabStop(false);
      pix3.setText("pictureBox1");
      pix3.setImage((Bitmap)resources.getObject("pix3_image"));

      pix4.setEnabled(false);
LISTING ONE continued



      pix4.setLocation(new Point(368, 208));
      pix4.setSize(new Point(100, 50));
      pix4.setTabIndex(0);
      pix4.setTabStop(false);
      pix4.setText("pictureBox1");
      pix4.setImage((Bitmap)resources.getObject("pix4_image"));

      slot0.setSize(new Point(150, 150));
      slot0.setTabIndex(7);
      slot0.setTabStop(false);
      slot0.setText("pictureBox1");

      slot1.setLocation(new Point(160, 0));
      slot1.setSize(new Point(150, 150));
      slot1.setTabIndex(6);
      slot1.setTabStop(false);
      slot1.setText("pictureBox1");

      slot2.setLocation(new Point(320, 0));
      slot2.setSize(new Point(150, 150));
      slot2.setTabIndex(5);
      slot2.setTabStop(false);
      slot2.setText("pictureBox1");

      bank.setLocation(new Point(16, 168));
      bank.setSize(new Point(104, 24));
      bank.setTabIndex(8);
      bank.setTabStop(false);
      bank.setText("bank");

      spin.setLocation(new Point(320, 168));
      spin.setSize(new Point(128, 32));
      spin.setTabIndex(9);
      spin.setText("Spin");
      spin.addOnClick(new EventHandler(this.spin_click));

      timer1.setInterval(500);
      timer1.addOnTimer(new EventHandler(this.timer1_timer));
      /* @designTimeOnly timer1.setLocation(new Point(376, 152)); */

      status.setLocation(new Point(128, 168));
      status.setSize(new Point(168, 24));
      status.setTabIndex(11);
      status.setTabStop(false);
      status.setText("");

      this.setNewControls(new Control[] {
                     status, 
                     spin, 
                     bank, 
                     slot2, 
                     slot1, 
                     slot0, 
                     pix4, 
                     pix3, 
                     pix0, 
                     pix2, 
                     pix1});
   }

   /**
    * The main entry point for the application. 
    *
    * @param args Array of parameters passed to the application
    * via the command line.
    */
   public static void main(String args[])
   {
      Application.run(new Form1());
   }
}


| home | current issue | archives | source code | demo express | events | search | editorial calendar | advertising | customer service | author guidelines | jobs | about




Entire contents copyright 1996-2000 CMP Media Inc.
Read our privacy policy.