-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisGrid.java
More file actions
64 lines (56 loc) · 1.84 KB
/
TetrisGrid.java
File metadata and controls
64 lines (56 loc) · 1.84 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
// Extend the Basic grid to allow for handling full rows.
public class TetrisGrid extends BasicTetrisGrid
{
public TetrisGrid()
{
super();
}
public int checkForFullRows()
{
int fullRowCounter, numberOfFullRows = 0;
for(int y = 19; y >= 0; y--) // Start from last row
{
fullRowCounter=0;
for(int x = 0; x < 10; x++)
if(TetrisGrid[x][y] != GraphicsController.BLACK)
{
fullRowCounter++; // Count number of non-black squares in a row
}
if(fullRowCounter == 10) // Row is full
{
numberOfFullRows++;
for(int j = y; j > 0; j--)
for(int x = 0; x < 10; x++)
TetrisGrid[x][j] = TetrisGrid[x][j-1]; // Move upper rows down
for(int x = 0; x < 10; x++)
TetrisGrid[x][0] = GraphicsController.BLACK; // Set upper row to black (no row above it to move down)
}
}
return numberOfFullRows;
}
public void setColour(int x, int y, int colour)
{
TetrisGrid[x][y] = colour;
}
public int getColour(int x, int y)
{
return TetrisGrid[x][y];
}
public boolean isFull()
{
for(int x = 0; x < 10; x++)
if(TetrisGrid[x][0] != GraphicsController.BLACK)
return true;
return false;
}
public boolean canSpawnNewShape(TetrisDrawableObject shape) // Checks whether a new shape can be spawned. Used so that the last shape that spawns doesn't overlap an existing shape in the upper rows of the grid.
{
TetrisFileLoadingDrawableObject currentShape = shape.getCurrentShape();
for(int x = 0; x < currentShape.getWidth(); x++)
for(int y = 0; y < currentShape.getHeight(); y++)
if(currentShape.getColour(x, y) != GraphicsController.BLACK) // If the non-black squares of the shape to be spawned overlap another shape in the grid, return false
if(TetrisGrid[x + currentShape.getCoordX()][y + currentShape.getCoordY()] != GraphicsController.BLACK)
return false;
return true;
}
}