-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.java
executable file
·132 lines (122 loc) · 3.12 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
131
132
import java.awt.Color;
/**
* Block class maintains information about a block.
* This class is extremely flexible to multiple games;
* such as Tile Game, Tetris, and Tic-Tac-Toe.
*
* @author Dave Feinberg
* @author Richard Page
* @author Susan King Added documentation
* @author Anna Wang
* @version January 4, 2017
*/
public class Block
{
private MyBoundedGrid<Block> grid;
private Location location;
private Color color;
private String text;
/**
* Constructs a blue block.
*/
public Block()
{
color = Color.BLUE;
grid = null;
location = null;
}
/**
* Gets the color of this block.
*
* @return the color of this block
*/
public Color getColor()
{
return this.color;
}
/**
* Sets the color of this block to newColor.
*
* @param newColor the new color of this block
*/
public void setColor(Color newColor)
{
this.color=newColor;
}
/**
* Gets the grid of this block, or null if this block is not contained
* in a grid.
*
* @return the grid
*/
public MyBoundedGrid<Block> getGrid()
{
return this.grid;
}
/**
* Gets the location of this block, or null if this block is not contained
* in a grid.
*
* @return this block's location, or null if this block is not in the grid
*/
public Location getLocation()
{
return this.location;
}
/**
* Removes this block from its grid.
*
* @precondition this block is contained in a grid
*/
public void removeSelfFromGrid()
{
this.grid.remove(this.getLocation());
this.grid=null;
this.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 the grid to place this block
* @param loc the location to place this block
*/
public void putSelfInGrid(MyBoundedGrid<Block> gr, Location loc)
{
Block old= gr.put(loc,this);
grid=gr;
location=loc;
if(old!=null)
{
old.grid=null;
old.location=null;
}
}
/**
* 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 the location that the block is to be moved
*/
public void moveTo(Location newLocation)
{
Location oldLocation=this.getLocation();
grid.remove(oldLocation);
putSelfInGrid(grid, newLocation);
}
/**
* Returns a string with the location and color of this block.
*
* @return location and color information about the block
*/
public String toString()
{
return "Block[location=" + location + ",color=" + color + "]";
}
}