-
Notifications
You must be signed in to change notification settings - Fork 33
/
GameState.cs
186 lines (155 loc) · 4.27 KB
/
GameState.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace Tetris
{
public class GameState
{
private Block currentBlock;
public Block CurrentBlock
{
get => currentBlock;
private set
{
currentBlock = value;
currentBlock.Reset();
for (int i = 0; i < 2; i++)
{
currentBlock.Move(1, 0);
if (!BlockFits())
{
currentBlock.Move(-1, 0);
}
}
}
}
public GameGrid GameGrid { get; }
public BlockQueue BlockQueue { get; }
public bool GameOver { get; private set; }
public int Score { get; private set; }
public Block HeldBlock { get; private set; }
public bool CanHold { get; private set; }
public bool Pause { get; internal set; }
public GameState()
{
GameGrid = new GameGrid(22, 10);
BlockQueue = new BlockQueue();
CurrentBlock = BlockQueue.GetAndUpdate();
CanHold = true;
}
private bool BlockFits()
{
//Determine whether the block can be accommodated, and true if it can be accommodated
foreach (Position p in CurrentBlock.TilePositions())
{
if (!GameGrid.IsEmpty(p.Row, p.Column))
{
return false;
}
}
return true;
}
//Temporarily disable this feature
//public void HoldBlock()
//{
// if (!CanHold)
// {
// return;
// }
// if (HeldBlock == null)
// {
// HeldBlock = CurrentBlock;
// CurrentBlock = BlockQueue.GetAndUpdate();
// }
// else
// {
// Block tmp = CurrentBlock;
// CurrentBlock = HeldBlock;
// HeldBlock = tmp;
// }
// CanHold = false;
//}
public void RotateBlockCW()
{
CurrentBlock.RotateCW();
if (!BlockFits())
{
CurrentBlock.RotateCCW();
}
}
public void RotateBlockCCW()
{
CurrentBlock.RotateCCW();
if (!BlockFits())
{
CurrentBlock.RotateCW();
}
}
public void MoveBlockLeft()
{
CurrentBlock.Move(0, -1);
if (!BlockFits())
{
CurrentBlock.Move(0, 1);
}
}
public void MoveBlockRight()
{
CurrentBlock.Move(0, 1);
if (!BlockFits())
{
CurrentBlock.Move(0, -1);
}
}
private bool IsGameOver()
{
return !(GameGrid.IsRowEmpty(0) && GameGrid.IsRowEmpty(1));
}
private void PlaceBlock()
{
foreach (Position p in CurrentBlock.TilePositions())
{
GameGrid[p.Row, p.Column] = CurrentBlock.Id;
}
Score += GameGrid.ClearFullRows();
if (IsGameOver())
{
GameOver = true;
}
else
{
CurrentBlock = BlockQueue.GetAndUpdate();
CanHold = true;
}
}
public void MoveBlockDown()
{
CurrentBlock.Move(1, 0);
if (!BlockFits())
{
CurrentBlock.Move(-1, 0);
PlaceBlock();
}
}
private int TileDropDistance(Position p)
{
int drop = 0;
while (GameGrid.IsEmpty(p.Row + drop + 1, p.Column))
{
drop++;
}
return drop;
}
public int BlockDropDistance()
{
int drop = GameGrid.Rows;
foreach (Position p in CurrentBlock.TilePositions())
{
drop = System.Math.Min(drop, TileDropDistance(p));
}
return drop;
}
public void DropBlock()
{
CurrentBlock.Move(BlockDropDistance(), 0);
PlaceBlock();
}
}
}