-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
97 lines (87 loc) · 2.44 KB
/
Grid.java
File metadata and controls
97 lines (87 loc) · 2.44 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.ArrayList;
public class Grid
{
private Location[][] grid;
public Grid(int length, int width)
{
grid = new Location[length][];
for(int i = 0; i < grid.length; i++)
{
for(int j = 0; j < width; j++)
{
grid[i][j] = new Location(i, j);
}
}
}
public int getLength()
{
return grid.length;
}
public int getWidth()
{
return grid[this.getLength() - 1].length;
}
public Location getLocation(int x, int y)
{
return grid[x][y];
}
public void putObj(GameObject obj)
{
grid[obj.getObjLocation().getXCoord()][obj.getObjLocation().getYCoord()].setObj(obj);
}
public Object removeObj(Location loc)
{
Object returnObj = grid[loc.getXCoord()][loc.getYCoord()].getObj();
grid[loc.getXCoord()][loc.getYCoord()].setObj(null);
return returnObj;
}
public boolean isValidLocation(Location loc)
{
if(loc.getXCoord() < 0 || loc.getXCoord() > this.getWidth())
return false;
if(loc.getYCoord() < 0 || loc.getYCoord() > this.getLength())
return false;
else
return true;
}
public ArrayList<Location> getAdjacentLocations(Location loc)
{
ArrayList<Location> adjacentLocs = new ArrayList<Location>();
for(int i = loc.getXCoord() - 1; i < loc.getXCoord() + 1; i++)
{
for(int j = loc.getYCoord() + 1; j < loc.getYCoord() - 1; i++)
{
if(this.isValidLocation(this.getLocation(i, j))
&& i != this.getLocation(i, j).getXCoord() && j != this.getLocation(i, j).getYCoord())
{
adjacentLocs.add(this.getLocation(i, j));
}
}
}
return adjacentLocs;
}
public Location getAdjacentLocationTowards(Location loc, Direction dir)
{
if(dir.getDirection() == Direction.UP)
{
if(this.isValidLocation(this.getLocation(loc.getXCoord(), loc.getYCoord() + 1 )))
return this.getLocation(loc.getXCoord(), loc.getYCoord() + 1);
}
else if(dir.getDirection() == Direction.LEFT)
{
if(this.isValidLocation(this.getLocation(loc.getXCoord() - 1, loc.getYCoord() )))
return this.getLocation(loc.getXCoord() - 1, loc.getYCoord());
}
else if(dir.getDirection() == Direction.DOWN)
{
if(this.isValidLocation(this.getLocation(loc.getXCoord(), loc.getYCoord() - 1 )))
return this.getLocation(loc.getXCoord(), loc.getYCoord() - 1);
}
else if(dir.getDirection() == Direction.RIGHT)
{
if(this.isValidLocation(this.getLocation(loc.getXCoord() + 1, loc.getYCoord() )))
return this.getLocation(loc.getXCoord() + 1, loc.getYCoord());
}
return new Location(-1, -1, null);
}
}