-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathBlock.cs
64 lines (56 loc) · 1.54 KB
/
Block.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
using System.Collections.Generic;
namespace Tetris
{
public abstract class Block
{
protected abstract Position[][] Tiles { get; }
protected abstract Position StartOffset { get; }
public abstract int Id { get; }
private int rotationState;
private Position offset;
public Block()
{
offset = new Position(StartOffset.Row, StartOffset.Column);
}
public IEnumerable<Position> TilePositions()
{
foreach (Position p in Tiles[rotationState])
{
yield return new Position(p.Row + offset.Row, p.Column + offset.Column);
}
}
public IEnumerable<Position> TilePositionsInNext()
{
foreach (Position p in Tiles[rotationState])
{
yield return new Position(p.Row, p.Column);
}
}
public void RotateCW()
{
rotationState = (rotationState + 1) % Tiles.Length;
}
public void RotateCCW()
{
if (rotationState == 0)
{
rotationState = Tiles.Length - 1;
}
else
{
rotationState--;
}
}
public void Move(int rows, int columns)
{
offset.Row += rows;
offset.Column += columns;
}
public void Reset()
{
rotationState = 0;
offset.Row = StartOffset.Row;
offset.Column = StartOffset.Column;
}
}
}