RacingSquare

RacingSquare - int count = 0; for(int i = 0; i <...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * File: RacingSquare.java * ----------------------- * This class is an example of a runnable class that animates * a square running a race. */ import acm.graphics.*; import acm.util.*; import java.awt.*; i public class RacingSquare extends GRect implements Runnable { p public RacingSquare(int index, boolean[] finished) { super(SIZE, SIZE); setFilled(true); myIndex = index; finishers = finished; } public void run() { // have not finished when we start finishers[myIndex] = false; f // run the race for(int i = 0; i < 100; i++) { pause(rgen.nextInt(50, 65)); move(STEP, 0); } // watch out for photo finishes!! (BUGGY!) / // count how many others finished before me
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int count = 0; for(int i = 0; i < finishers.length; i++) { if (finishers[i]) count++; } // take some time to do a victory dance pause(50); p // mark myself as having finished finishers[myIndex] = true; f // if no one finished before me, then I win! (turn red) if (count == 0) { setColor(Color.RED); } } /* constants */ private static final double SIZE = 20; private static final double STEP = 5; p /* private instance variables */ private int myIndex; private boolean finishers; private RandomGenerator rgen = RandomGenerator.getInstance(); }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Ask a homework question - tutors are online