-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyBoundedGrid.java
executable file
·142 lines (133 loc) · 3.79 KB
/
MyBoundedGrid.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
import java.util.ArrayList;
/**
* MyBoundedGrid is the grid on which the game is played and on which the blocks exist.
* It is a rectangular grid with a finite number of rows and columns.
*
* @author Dave Feinberg
* @author Richard Page
* @author Susan King
* @author Anna Wang
* @version January 4, 2017
*
* @param <E> the elements that may be put in the grid are any objects
*/
public class MyBoundedGrid<E>
{
/**
* The 2-D array that is used to store the grid's elements.
*/
private Object[][] occupantArray;
/**
* Constructs an empty MyBoundedGrid with the given dimensions.
*
* @param rows the grid's number of rows; rows > 0
* @param cols the grid's number of cols; cols > 0
*/
public MyBoundedGrid(int rows, int cols)
{
occupantArray = new Object[rows][cols];
}
/**
* Retrieves the number of rows.
*
* @return the grid's row count
*/
public int getNumRows()
{
return occupantArray.length;
}
/**
* Retrieves the number of columns.
*
* @return the grid's columns count
*/
public int getNumCols()
{
return occupantArray[0].length;
}
/**
* Determines whether a location is valid.
*
* @param loc the location in quesion. loc != null
* @return true if loc is valid in this grid; otherwise,
* false
*/
public boolean isValid(Location loc)
{
int row=loc.getRow();
int col=loc.getCol();
return( row<getNumRows() && col<getNumCols() && row>=0&&col>=0);
}
/**
* Retrieves an element from this grid at a location, or
* null if the location is unoccupied.
*
* @param loc is a valid location in this grid
*
* @return the object at location loc
* or null if the location is unoccupied
*/
public E get(Location loc)
{
int row=loc.getRow();
int col= loc.getCol();
Object temp= this.occupantArray[row][col];
return (E)temp;
//(You will need to promise the return value is of type E.)
}
/**
* Puts an element at location loc on this grid. Plus
* returns the object previously at that location, or
* null if the location is unoccupied.
*
* @param loc is a valid location in this grid
* @param obj the object to put at location loc
*
* @return the object at location loc
* or null if the location is unoccupied
*/
public E put(Location loc, E obj)
{
E temp= this.get(loc);
occupantArray[loc.getRow()][loc.getCol()]=(Object)obj;
return temp;
}
/**
* Removes an element from this grid at a location. Plus
* returns the object previously at that location, or
* null if the location is unoccupied.
*
* @param loc is a valid location in this grid
*
* @return the object that was at location loc
* or null if the location is unoccupied
*/
public E remove(Location loc)
{
E temp= this.get(loc);
occupantArray[loc.getRow()][loc.getCol()]=null;
return temp;
}
/**
* Returns all the occupied location in this grid.
*
* @return all the occupied locations in an array list;
* 0 <= list.size < getNumRows() * getNumCols()
*/
public ArrayList<Location> getOccupiedLocations()
{
ArrayList<Location> myList=new ArrayList<Location>();
for (int r=0; r<getNumRows(); r++)
{
for (int c=0; c<getNumCols(); c++)
{
Location loc= new Location(r,c);
if (occupantArray[r][c]!=null)
{
myList.add(loc);
}
}
}
return myList;
}
}