-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathGameGrid.cs
97 lines (84 loc) · 2.17 KB
/
GameGrid.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace Tetris
{
public class GameGrid
{
private readonly int[,] grid;
public int Rows { get; }
public int Columns { get; }
public int this[int r, int c]
{
get => grid[r, c];
set => grid[r, c] = value;
}
public GameGrid(int rows, int columns)
{
Rows = rows;
Columns = columns;
grid = new int[rows, columns];
}
public bool IsInside(int r, int c)
{
return r >= 0 && r < Rows && c >= 0 && c < Columns;
}
public bool IsEmpty(int r, int c)
{
return IsInside(r, c) && grid[r, c] == 0;
}
public bool IsRowFull(int r)
{
for (int c = 0; c < Columns; c++)
{
if (grid[r, c] == 0)
{
return false;
}
}
return true;
}
public bool IsRowEmpty(int r)
{
for (int c = 0; c < Columns; c++)
{
if (grid[r, c] != 0)
{
return false;
}
}
return true;
}
private void ClearRow(int r)
{
for (int c = 0; c < Columns; c++)
{
grid[r, c] = 0;
}
}
private void MoveRowDown(int r, int numRows)
{
for (int c = 0; c < Columns; c++)
{
grid[r + numRows, c] = grid[r, c];
grid[r, c] = 0;
}
}
public int ClearFullRows()
{
int cleared = 0;
for (int r = Rows-1; r >= 0; r--)
{
//如果此行满了,则消除此行
//如果此行没满,则根据已经消除的行数,向下移动整行
if (IsRowFull(r))
{
ClearRow(r);
cleared++;
}
else if (cleared > 0)
{
MoveRowDown(r, cleared);
}
}
return cleared;
}
}
}