-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplorer.java
More file actions
66 lines (65 loc) · 1.81 KB
/
Copy pathExplorer.java
File metadata and controls
66 lines (65 loc) · 1.81 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
63
64
65
66
/**
*Explorer.java
*Class Explorer builds an Explorer object
*
*@author akramins
*@version 5/13/2013
*/
import java.awt.event.KeyEvent;
import java.util.Scanner;
public class Explorer extends Occupant{
private String name;
private Maze maze;
public Explorer(Maze maze){
this.maze = maze;
name = new String();
}
public Explorer(Square location, Maze maze, String name){
super(location);
this.maze = maze;
this.name = name;
maze.lookAround(location);
}
//returns the name of the explorer.
public String name(){
return this.name;
}
//moves the explorer to a given square based on a key event.
public void move(int key){
int row = super.location().row();
int col = super.location().col();
if(key == KeyEvent.VK_UP || key == KeyEvent.VK_KP_UP){ //Checking for keyevent and in what direction
if(!super.location().wall(Square.UP)){ //checking for a wall in the direction.
moveTo(maze.getSquare(row - 1, col));
}
}
else if(key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_KP_RIGHT){
if(!super.location().wall(Square.RIGHT)){
moveTo(maze.getSquare(row, col + 1));
}
}
else if(key == KeyEvent.VK_DOWN || key == KeyEvent.VK_KP_DOWN){
if(!super.location().wall(Square.DOWN)){
moveTo(maze.getSquare(row + 1, col));
}
}
else if(key == KeyEvent.VK_LEFT || key == KeyEvent.VK_KP_LEFT){
if(!super.location().wall(Square.LEFT)){
moveTo(maze.getSquare(row, col - 1));
}
}
}
//moveTo moves the explorer to a given square.
public void moveTo(Square s){
super.moveTo(s);
s.enter();
maze.lookAround(s); //looks around for walls on its sides.
}
public String toText(char delimiter){
return super.toText(delimiter) + delimiter + name();
}
public void toObject(Scanner input){
moveTo(maze.getSquare(input.nextInt(), input.nextInt()));
name = input.next();
}
}