-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrunkard.java
68 lines (55 loc) · 1.76 KB
/
Drunkard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Name:Yangping Zheng
// USC loginid:1512846791
// CS 455 PA1
// Fall 2015
/**
Drunkard class
Represents a "drunkard" doing a random walk on a grid.
*/
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Random;
public class Drunkard {
/**
Creates drunkard with given starting location and distance
to move in a single step.
@param startLoc starting location of drunkard
@param theStepSize size of one step in the random walk
*/
private ImPoint startLoc;
private int theStepSize;
Random r = new Random();
public Drunkard(ImPoint startLoc, int theStepSize) {
this.startLoc = startLoc;
this.theStepSize = theStepSize;
}
/**
Takes a step of length step-size (see constructor) in one of
the four compass directions. Changes the current location of the
drunkard.
*/
public void takeStep() {
int i = 1+r.nextInt(4); //generate a random number indicating the direction
switch(i){
case 1: startLoc = startLoc.translate(0-theStepSize,0); break; //step left
case 2: startLoc = startLoc.translate(theStepSize,0); break; //step right
case 3: startLoc = startLoc.translate(0,theStepSize); break; //step up
case 4: startLoc = startLoc.translate(0,0-theStepSize); break; //step down
}
}
public Point2D[] getPath(int stepsNum){
Point2D[] thePath = new Point2D[stepsNum];
for(int i=0;i<stepsNum;i++){
thePath[i] = this.getCurrentLoc().getPoint2D();
this.takeStep();
}
return thePath;
}
/**
gets the current location of the drunkard.
@return an ImPoint object representing drunkard's current location
*/
public ImPoint getCurrentLoc() {
return startLoc;
}
}