// Author: Jim Carlson (copyright) 1996 // Date: June 17, 1996 // Web: http://www.math.utah.edu // E-mail: carlson@math.utah.edu // All rights reserved, non-commercial use OK. import java.applet.*; import java.awt.*; import java.util.Random; public class GamblersRuin extends Applet implements Runnable { int thisHeight, thisWidth; Thread runner; boolean running; int delay; boolean ruin = true; Image offscreenImg; Graphics offscreenG; Color lineColor = Color.red; int trial, nSteps, stake; int x0, y0, x, y; int hMargin = 20; int vMargin = 20; double kx = 5; double ky = 5; public void init() { thisHeight = this.size().height; thisWidth = this.size().width; offscreenImg = createImage( this.size().width, this.size().height ); offscreenG = offscreenImg.getGraphics(); String s = getParameter("delay"); if ( s != null ) delay = Integer.parseInt( s ); else delay = 100; s = getParameter("nSteps"); if ( s != null ) nSteps = Integer.parseInt( s ); else nSteps = 60; s = getParameter("stake"); if ( s != null ) stake = Integer.parseInt( s ); else stake = 0; s = getParameter("ruin"); if ( s != null ) ruin = s.equals("true"); else ruin = false; s = getParameter("ky"); if ( s != null ) ky = Double.valueOf( s ).doubleValue(); kx = (thisWidth - 2.0*hMargin)/nSteps; x = 0; y = stake; trial = 0; offscreenG.setColor( Color.lightGray ); offscreenG.fillRect( 0, 0, thisWidth, thisHeight ); offscreenG.setColor( Color.black ); } public void start() { if ( runner == null ) { runner = new Thread(this); running = true; runner.start(); } } public void stop() { if ( runner != null ) { runner.stop(); runner = null; } } public void run() { Random R = new Random(); offscreenG.drawLine( sx(0), sy(0), sx(nSteps), sy(0) ); offscreenG.drawLine( sx(0), thisHeight - vMargin, sx(0), vMargin ); offscreenG.drawLine( sx(-2/kx), sy(stake), sx(2/kx), sy(stake) ); offscreenG.drawLine( sx(-2/kx), sy(-stake), sx(2/kx), sy(-stake) ); int stringX = hMargin + 40; int stringY = thisHeight - 30; while( true ) { trial++; x0 = x; y0 = y; x++; if (( R.nextDouble() - 0.5 ) > 0) y++; else y--; offscreenG.setColor( lineColor ); offscreenG.drawLine( sx( x0 ), sy( y0 ), sx( x ), sy( y ) ); //showStatus("trial = "+trial+", $$ = "+y); offscreenG.setColor( Color.lightGray); offscreenG.fillRect(stringX-2, stringY-20, 140, 30 ); offscreenG.setColor( Color.black); offscreenG.drawString("trial = "+trial+", $$ = "+y, stringX, stringY ); repaint(); if ( ruin == true && y == 0 ) { running = false; runner.suspend(); x = 0; y = stake; trial = 0; changeColors(); } try { Thread.sleep( delay ); } catch ( InterruptedException e ) { } } } public void update( Graphics g ) { paint (g); } public void paint( Graphics g ) { g.drawImage( offscreenImg, 0, 0, this ); } int changeColor( int c, int dc ) { c = c + dc; c = c%256; if ( c < 0 ) c += 256; return c; } void changeColors() { int dColor = 60; int red = lineColor.getRed(); red = changeColor( red, -dColor ); int blue = lineColor.getBlue(); blue = changeColor( blue, dColor ); int green = lineColor.getGreen(); lineColor = new Color( red , green, blue ); } public boolean mouseDown( Event evt, int mx, int my ) { if (running) { if ( mx < hMargin + 1 ) { x = 0; y = stake; trial = 0; changeColors(); } else { running = false; runner.suspend(); } } else { running = true; runner.resume(); } return true; } int sx( double x ) { return (int) (kx*x + hMargin); } int sy( double y ) { return (int) (-ky*y + thisHeight/2); } }