-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.java
130 lines (120 loc) · 3.37 KB
/
Block.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
import java.awt.Color;
/**
* Class of blocks.
*
* @author Jin Kim
* @version 8 December 2015
*/
public class Block
{
private MyBoundedGrid<Block> grid;
private Location location;
private Color color;
/**
* constructs a blue block, because blue is the greatest color ever!
*/
public Block()
{
color = Color.BLUE;
grid = null;
location = null;
}
//gets the color of this block
/**
* Gets the color of this block.
*
* @return the color of the block.
*/
public Color getColor()
{
return color;
}
//gets the color of this block to newColor.
/**
* Sets the color of this block to newColor.
*
* @param newColor new color of the block.
*/
public void setColor(Color newColor)
{
color = newColor;
}
//gets the grid of this block, or null if this block is not contained in a grid
/**
* gets the grid of this block, or null if this block is not contained in a grid
*
* @return grid of this block.
*/
public MyBoundedGrid<Block> getGrid()
{
return grid;
}
//gets the location of this block, or null if this block is not contained in a grid
/**
* gets the location of this block, or null if this block is not contained in a grid
*
* @return location of this block.
*/
public Location getLocation()
{
return location;
}
//removes this block from its grid
//precondition: this block is contained in a grid
/**
* Removes block from its Grid.
* @precondition: this block is contained in a grid
*/
public void removeSelfFromGrid()
{
grid.remove(location);
grid = null;
location = null;
}
/**
* puts this block into location loc of grid gr
* if there is another block at loc, it is removed
* @precondition: (1) this block is not contained in a grid
* (2) loc is valid in gr
*
* @param gr Grid of this block.
* @param loc location of this block.
*/
public void putSelfInGrid(MyBoundedGrid<Block> gr, Location loc)
{
if(gr.get(loc)!= null)
{
gr.get(loc).removeSelfFromGrid();
}
gr.put(loc, this);
grid = gr;
location = loc;
}
//moves this block to newLocation
//if there is another block at newLocation, it is removed
//precondition: (1) this block is contained in a grid
// (2) newLocation is valid in the grid of this block
/**
* moves this block to newLocation
* if there is another block at newLocation, it is removed
* @precondition: (1) this block is contained in a grid
* (2) newLocation is valid in the grid of this block
*
* @param newLocation newLocation of the block.
*/
public void moveTo(Location newLocation)
{
grid.remove(location);
putSelfInGrid(grid, newLocation);
}
//returns a string with the location and color of this block
/**
* returns a string with the location and a color of this block.
*
* @return a string with the location and a color of this block.
*/
public String toString()
{
return "Block[location=" + location + ",color=" + color + "]";
}
}