-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomOccupant.java
More file actions
62 lines (57 loc) · 1.75 KB
/
Copy pathRandomOccupant.java
File metadata and controls
62 lines (57 loc) · 1.75 KB
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
/**
*RandomOccupant.java
*Class RandomOccupant builds a random occupant object which is also an occupant in the maze.
*
*@author akramins
*@version 5/13/2013
*/
import java.util.Random;
import java.util.Scanner;
public abstract class RandomOccupant extends Occupant{
private Random generator;
private Maze maze;
//constructs a random occupant in the maze at a random location.
public RandomOccupant(Maze maze){
super();
generator = new Random();
this.maze = maze;
super.moveTo(this.maze.getSquare(generator.nextInt(this.maze.rows()), generator.nextInt(this.maze.cols())));
}
//constructs a random occupant in the maze with a seeded random location.
public RandomOccupant(Maze maze, long seed){
super();
generator = new Random(seed);
this.maze = maze;
super.moveTo(this.maze.getSquare(generator.nextInt(this.maze.rows()), generator.nextInt(this.maze.cols())));
}
//constructs a random occupant in the maze at a given location.
public RandomOccupant(Maze maze, Square location){
super(location);
this.maze = maze;
generator = new Random();
}
//Method move randomly moves the random occupant around the maze.
public void move(){
int row = super.location().row();
int col = super.location().col();
int g = generator.nextInt(4);
while(super.location().wall(g)){
g = generator .nextInt(4);
}
if(g == Square.UP){
super.moveTo(maze.getSquare(row - 1, col));
}
else if(g == Square.RIGHT){
super.moveTo(maze.getSquare(row, col + 1));
}
else if(g == Square.DOWN){
super.moveTo(maze.getSquare(row + 1, col));
}
else if(g == Square.LEFT){
super.moveTo(maze.getSquare(row, col - 1));
}
}
public void toObject(Scanner input){
super.moveTo(maze.getSquare(input.nextInt(), input.nextInt()));
}
}