Advertisement
aaronvan

RandomWalk

Feb 22nd, 2016
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.*;
  3.  
  4. public class RandomWalk {
  5.  
  6.     // DrawingPanel will have dimensions DIM by DIM
  7.     public static final int DIM = 400;
  8.    
  9.     // Start the random walk in the center of the screen
  10.     public static final int CENTER = DIM / 2;
  11.    
  12.     // how big should the cursor appear?
  13.     public static final int CURSOR_DIM = 10;
  14.    
  15.     // how long should the cursor saty in one place?
  16.     public static final int SLEEP_TIME = 25; // milliseconds
  17.  
  18.     public static void main( String[] args ) {
  19.         DrawingPanel panel = new DrawingPanel( DIM, DIM );
  20.        
  21.         Random rand = new Random();
  22.        
  23.         walkRandomly( panel, rand );
  24.     }
  25.    
  26.     /**
  27.      * Draw a random walk on the panel.
  28.      * Stop as soon as you walk off the panel.
  29.      * Each random step should go only in one
  30.      * of these directions: left, right, up, down.
  31.      * @param panel a DrawingPanel to draw the
  32.      *        random walk on
  33.      * @param rand a Java Random object to be
  34.      *        used to generate random steps
  35.      */
  36.     public static void walkRandomly( DrawingPanel panel, Random rand ) {
  37.         Graphics g = panel.getGraphics();
  38.        
  39.         // start in center of panel
  40.         int x = CENTER;
  41.         int y = CENTER;
  42.        
  43.         // Randomly step left, right, up, or down
  44.         // until cursor goes off screen.
  45.         while ( onScreen( x, y ) ) {
  46.        
  47.             // Draw the cursor in BLACK
  48.             g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);
  49.             // Wait a bit.
  50.             panel.sleep( SLEEP_TIME );
  51.             // Show a shadow version of the cursor
  52.             g.setColor(Color.GRAY);
  53.             g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);
  54.            
  55.             // YOUR CODE GOES BETWEEN THE STARS
  56.             // Choose a new location for the cursor
  57.             //*********************************
  58.        
  59.         // create a new random object
  60.         Random move = new Random();
  61.        
  62.         // chose the next direction based on randomly
  63.         // chosing an integer between 0 and 3
  64.         int xmove = move.nextInt(3) - 1;
  65.         int ymove = 0;
  66.        
  67.         if (xmove == 0) {
  68.             ymove = (move.nextInt(2) * 2) - 1;
  69.         }
  70.  
  71.         x += CURSOR_DIM * xmove;
  72.         y += CURSOR_DIM * ymove;
  73.                            
  74.             //*********************************
  75.             // END OF YOUR CODE
  76.            
  77.             // draw the cursor at its new location
  78.             g.setColor(Color.BLACK);
  79.             g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);
  80.         }
  81.     }
  82.    
  83.     /**
  84.      * determine whether (x, y) is a point on the panel.
  85.      * @param x the x-coord of the cursor
  86.      * @param y the y-coord of the cursor
  87.      * @return true if (x,y) is on the screen,
  88.      *         false if (x,y) is off the screen
  89.      */
  90.     public static boolean onScreen( int x, int y ) {
  91.         // YOUR CODE GOES BETWEEN THE STARS
  92.         // Replace "return true" with code
  93.         // that returns true if (x,y) is still
  94.         // on the screen and returns false if
  95.         // (x,y) is no longer on the screen.
  96.         //*********************************
  97.         return ((x >= 0 && x <= DIM) && (y >= 0 && y <= DIM));
  98.         //*********************************
  99.         // END OF YOUR CODE
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement