-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicTetrisGrid.java
More file actions
92 lines (79 loc) · 3.47 KB
/
BasicTetrisGrid.java
File metadata and controls
92 lines (79 loc) · 3.47 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
// Interface of tetris grid. Holds the information of the grid, adds shapes to the grid and checks for validity of potential movements in the grid.
public class BasicTetrisGrid extends DrawableObject
{
protected int TetrisGrid[][];
public BasicTetrisGrid()
{
TetrisGrid = new int[10][20]; // Create the grid and paint it black
for(int i = 0; i < 10; i++)
for(int j = 0; j < 20; j++)
TetrisGrid[i][j] = GraphicsController.BLACK;
}
public void draw(GraphicsController arg0)
{
for(int i = 0; i < 10; i++)
for(int j = 0; j < 20; j++)
arg0.setGridColour(i, j, TetrisGrid[i][j]);
}
public void add(TetrisDrawableObject shape) //Adds a stationary shape to the grid
{
TetrisFileLoadingDrawableObject currentShape = shape.getCurrentShape();
for(int i = 0; i < currentShape.getWidth(); i++)
for(int j = 0; j < currentShape.getHeight(); j++)
if(currentShape.getColour(i, j) != GraphicsController.BLACK) // Adds the non-black squares of a shape to the grid
TetrisGrid[i + currentShape.getCoordX()][j + currentShape.getCoordY()] = currentShape.getColour(i, j);
}
public boolean canMoveDown(TetrisDrawableObject shape)
{
TetrisFileLoadingDrawableObject currentShape = shape.getCurrentShape();
for(int i = 0; i < currentShape.getWidth(); i++)
for(int j = 0; j < currentShape.getHeight(); j++)
if((currentShape.getColour(i, j) != GraphicsController.BLACK)) // Checks whether a shape's non-black squares can move down
if(j + currentShape.getCoordY() + 1 < 20) // Used to check that movement is within bounds and that an ArrayIndexOutofBoundsException is not thrown in the next check
{
if(TetrisGrid[i + currentShape.getCoordX()][j + currentShape.getCoordY() + 1] != GraphicsController.BLACK) //Checks whether the squares ahead are occupied
return false;
}else
return false;
return true;
}
public boolean canMoveLeft(TetrisDrawableObject shape)
{
TetrisFileLoadingDrawableObject currentShape = shape.getCurrentShape();
for(int i = 0; i < currentShape.getWidth(); i++)
for(int j = 0; j < currentShape.getHeight(); j++)
if((currentShape.getColour(i, j) != GraphicsController.BLACK) ) // Same as above, for left
if(i + currentShape.getCoordX() - 1 >= 0)
{
if(TetrisGrid[i + currentShape.getCoordX() - 1][j + currentShape.getCoordY()] != GraphicsController.BLACK)
return false;
}
else
return false;
return true;
}
public boolean canMoveRight(TetrisDrawableObject shape)
{
TetrisFileLoadingDrawableObject currentShape = shape.getCurrentShape();
for(int i = 0; i < currentShape.getWidth(); i++)
for(int j = 0; j < currentShape.getHeight(); j++)
if((currentShape.getColour(i, j) != GraphicsController.BLACK))
if(i + currentShape.getCoordX() + 1 < 10) // Same as above for right
{
if(TetrisGrid[i + currentShape.getCoordX() + 1][j + currentShape.getCoordY()] != GraphicsController.BLACK)
return false;
}else
return false;
return true;
}
public boolean canRotate(TetrisDrawableObject shape)
{
TetrisFileLoadingDrawableObject nextRotation = shape.getNextRotation();
for(int i = 0; i < nextRotation.getWidth(); i++)
for(int j = 0; j < nextRotation.getHeight(); j++)
if(shape.isSafeToRotate() && (nextRotation.getColour(i, j) != GraphicsController.BLACK)) // Same as above for rotation
if(TetrisGrid[i + nextRotation.getCoordX()][j + nextRotation.getCoordY()] != GraphicsController.BLACK)
return false;
return true;
}
}