-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.java
70 lines (60 loc) · 1.85 KB
/
World.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
69
70
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class World{
protected Location[][] locations;
protected Location home;
protected List<Player> players; // all players in the world
public World(){
locations = new Location[3][3];
for(int r=0; r<3; r+=1){
for(int c=0; c<3; c+=1){
locations[r][c] = new EmptyLocation(new Position(r,c), "Nothing here to see.");
}
}
home = locations[0][0];
players = new ArrayList<Player>();
}
public List<Player> getPlayers(){ return players; }
public Location[][] getWorld(){ return locations; }
public Location getHome(){ return home; }
public List<Location> getLocations(){
List<Location> list = new ArrayList<Location>(locations.length*locations[0].length);
for (Location[] array : locations) {
list.addAll(Arrays.asList(array));
}
return list;
}
/* keep a list of all players in the world. Each time a helper is created be
* sure to add them to this list
*/
public World addPlayer(Player p){
players.add(p);
return this;
}
public boolean move(Player p, int direction){
Location loc = p.getLocation(); // player's current location
int x = loc.getPosition().getX();
int y = loc.getPosition().getY();
Location newLocation = null;
//
switch(direction){
case Directions.UP:
newLocation = locations[x-1][y];
break;
case Directions.DOWN:
newLocation = locations[x+1][y];
break;
case Directions.LEFT:
newLocation = locations[x][y-1];
break;
case Directions.RIGHT:
newLocation = locations[x][y+1];
break;
default: break;
}
loc.exit(p);
newLocation.enter(p);
return true;
}
}