/
Tetromino.cs
109 lines (91 loc) · 2.75 KB
/
Tetromino.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
using System;
using System.Collections.Generic;
using System.Xml.Schema;
namespace Tetris
{
class Tetromino
{
private readonly bool[,] blocks;
public int NumRows
{
get
{
return (hUnit[rotation,0] != 0) ? blocks.GetLength(0) : blocks.GetLength(1);
}
}
public int NumColumns
{
get
{
return (hUnit[rotation,0] != 0) ? blocks.GetLength(1) : blocks.GetLength(0);
}
}
public int[,,] SrsRotationOffset { get; private set; }
private int rotation;
public int Rotation
{
get
{
return rotation;
}
set
{
if (value < 0 || value > 3)
{
throw new System.InvalidOperationException("Invalid rotation value.");
}
rotation = (4 + value) % 4;
}
}
public int PositionX { get; set; }
public int PositionY { get; set; }
// horizontal unit (for each rotation)
private static readonly int[,] hUnit = {
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
};
// vertical unit (for each rotation)
private static readonly int[,] vUnit = {
{0, 1},
{-1, 0},
{0, -1},
{1, 0},
};
public int Color { get; }
public Tetromino(bool[,] blocks, int[,,] srsRotationOffset, int color)
{
SrsRotationOffset = srsRotationOffset;
this.blocks = blocks;
Color = color;
}
public Tetromino(Tetromino other)
{
this.blocks = other.blocks;
this.Color = other.Color;
this.PositionX = other.PositionX;
this.PositionY = other.PositionY;
this.rotation = other.rotation;
this.SrsRotationOffset = other.SrsRotationOffset;
}
public bool Block(int i, int j)
{
int cx = (hUnit[rotation,0] == -1 || vUnit[rotation,0] == -1) ? blocks.GetLength(0) - 1 : 0;
int cy = (hUnit[rotation,1] == -1 || vUnit[rotation,1] == -1) ? blocks.GetLength(1) - 1 : 0;
return blocks[cx + i * hUnit[rotation,0] + j * vUnit[rotation,0],
cy + i * hUnit[rotation,1] + j * vUnit[rotation,1]];
}
public bool IsLineEmpty(int i)
{
for (int j = 0; j < NumColumns; j++)
{
if (Block(i, j))
{
return false;
}
}
return true;
}
}
}