-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.cs
83 lines (66 loc) · 2.24 KB
/
Screen.cs
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
using System.Drawing;
namespace Tetris {
class Screen {
Color[,] grid;
public Screen(int width, int height) {
grid = new Color[width, height];
}
public int GetWidth() {
return grid.GetLength(0);
}
public int GetHeight() {
return grid.GetLength(1);
}
public void SetBlock(int x, int y, Color value) {
grid[x, y] = value;
}
public void RemoveBlock(int x, int y) {
grid[x, y] = Color.Empty;
}
public bool BlockExists(int x, int y) {
if(x < 0 || x >= grid.GetLength(0) || y < 0 || y >= grid.GetLength(1)) return false;
return grid[x, y] != Color.Empty;
}
public bool IsInside(int x, int y) {
if(x >= 0 && x < grid.GetLength(0) && y >= 0 && y < grid.GetLength(1)) return true;
else return false;
}
public bool IsAvailable(int x, int y) {
return IsInside(x, y) && !BlockExists(x, y);
}
public Color GetColor(int x, int y) {
return grid[x, y];
}
public void AddPiece(Piece piece) {
foreach(Point block in piece.GetBlocks()) {
SetBlock(block.X, block.Y, piece.color);
}
}
private bool RowIsFull(int y) {
for(int i = 0; i < GetWidth(); ++i) {
if(!BlockExists(i, y)) return false;
}
return true;
}
private void RemoveRow(int y) {
for(int i = y; i >= 0; --i) {
for(int j = 0; j < GetWidth(); ++j) {
if(i == 0)
RemoveBlock(j, i);
else
SetBlock(j, i, GetColor(j, i - 1));
}
}
}
public int RemoveFullRows() {
int count = 0;
for(int i = 0; i < Tetris.SCREEN_HEIGHT; ++i) {
if(RowIsFull(i)) {
RemoveRow(i);
count++;
}
}
return count;
}
}
}