-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.java
143 lines (125 loc) · 3.85 KB
/
Grid.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* A square grid of all the locations in the game.
* This class takes concept from the Field class in the 'foxes and rabbit' project
* in chapter 10 of the BlueJ textbook.
*
* @author Huy Nguyen, David J. Barnes, Michael Kolling and Olaf Chitil
* @version (a version number or a date)
*/
public class Grid
{
//Memory storage for all squares
private Tile[][] grid;
//the size of the grid
private int size;
/**
* Creates a Grid with the given size.
* @param depth The depth of the grid, must be larger than 5
* @param width The width of the grid, must be larger than 5
*/
public Grid(int size)
{
assert size > 5 : "Size is too small!";
this.size = size;
grid = new Tile[size][size];
}
/**
* Places a tile at a given location in the grid.
* @param tile The tile to place
* @param location The location for the tile
*/
public void place(Tile tile, Location location)
{
assert location != null : "Location is null";
assert tile != null: "Tile is null";
grid[location.getRow()][location.getCol()] = tile;
}
/**
* Clears the tile at the given location, works even if the tile is null
* @param location The location to clear
*/
public void clearTileAt(Location location)
{
assert location != null : "Location is null";
grid[location.getRow()][location.getCol()] = null;
}
//Accessor methods for snake movement
/**
* Returns the location to the north of the given location.
* @param location The location needed
* @return The location to return
*/
public Location getNorthLocation(Location location)
{
assert location != null : "Location is null";
int row = location.getRow();
int col = location.getCol();
Location loc = new Location (row-1, col);
return loc;
}
/**
* Returns the location to the south of the given location.
* @param location The location needed
* @return The location to return
*/
public Location getSouthLocation(Location location)
{
assert location != null : "Location is null";
int row = location.getRow();
int col = location.getCol();
Location loc = new Location (row+1, col);
return loc;
}
/**
* Returns the location to the east of the given location.
* @param location The location needed
* @return The location to return
*/
public Location getEastLocation(Location location)
{
assert location != null : "Location is null";
int row = location.getRow();
int col = location.getCol();
Location loc = new Location (row, col+1);
return loc;
}
/**
* Returns the location to the west of the given location.
* @param location The location needed
* @return The location to return
*/
public Location getWestLocation(Location location)
{
assert location != null : "Location is null";
int row = location.getRow();
int col = location.getCol();
Location loc = new Location (row, col-1);
return loc;
}
//Accessor methods.
/**
* Returns the tile at the given location
* @param The location of the tile to return
* @return Returns the tile, or null if there is none
*/
public Tile getTileAt(Location location)
{
assert location != null : "Location is null";
return grid[location.getRow()][location.getCol()];
}
/**
* Returns the grid
*/
public Tile[][] getGrid()
{
assert grid != null : "Grid is null";
return grid;
}
/**
* Returns the width of the grid.
*/
public int getSize()
{
return size;
}
}