-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshape.go
140 lines (130 loc) · 2.84 KB
/
shape.go
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
package main
// moveShape shifts a shape in a directy according to a given row and column.
func moveShape(r, c int, s Shape) Shape {
var newShape Shape
for i := 0; i < 4; i++ {
newShape[i].row = s[i].row + r
newShape[i].col = s[i].col + c
}
return newShape
}
func moveShapeDown(s Shape) Shape {
return moveShape(-1, 0, s)
}
func moveShapeRight(s Shape) Shape {
return moveShape(0, 1, s)
}
func moveShapeLeft(s Shape) Shape {
return moveShape(0, -1, s)
}
// isGameOver checks if any of the Points in a shape are in the invisable rows
// (ie rows 20 and 21)
func isGameOver(s Shape) bool {
for i := 0; i < 4; i++ {
if s[i].row >= 20 {
return true
}
}
return false
}
func getShapeWidth(s Shape) int {
maxWidth := 0
for i := 1; i < 4; i++ {
w := s[i].col - s[0].col
if w > maxWidth {
maxWidth = w
}
}
return maxWidth
}
func getShapeHeight(s Shape) int {
maxHeight := -1
minHeight := 22
for i := 0; i < 4; i++ {
if s[i].row < minHeight {
minHeight = s[i].row
}
if s[i].row > maxHeight {
maxHeight = s[i].row
}
}
return maxHeight - minHeight
}
// rotateShape rotates a shape by 90 degrees based on the pivot point
// which is always the second element in the shape array (ie s[1]).
func rotateShape(s Shape) Shape {
var retShape Shape
pivot := s[1]
retShape[1] = pivot
for i := 0; i < 4; i++ {
// Index 1 is the pivot point
if i == 1 {
continue
}
dRow := pivot.row - s[i].row
dCol := pivot.col - s[i].col
retShape[i].row = pivot.row + (dCol * -1)
retShape[i].col = pivot.col + (dRow)
}
return retShape
}
// getShapeFromPiece returns the shape based on the piece type. There
// are seven shapes available: LPiece, IPiece, OPiece, TPiece, SPiece,
// ZPiece, and JPiece.
func getShapeFromPiece(p Piece) Shape {
var retShape Shape
switch p {
case LPiece:
retShape = Shape{
Point{row: 1, col: 0},
Point{row: 1, col: 1},
Point{row: 1, col: 2},
Point{row: 0, col: 0},
}
case IPiece:
retShape = Shape{
Point{row: 1, col: 0},
Point{row: 1, col: 1},
Point{row: 1, col: 2},
Point{row: 1, col: 3},
}
case OPiece:
retShape = Shape{
Point{row: 1, col: 0},
Point{row: 1, col: 1},
Point{row: 0, col: 0},
Point{row: 0, col: 1},
}
case TPiece:
retShape = Shape{
Point{row: 1, col: 0},
Point{row: 1, col: 1},
Point{row: 1, col: 2},
Point{row: 0, col: 1},
}
case SPiece:
retShape = Shape{
Point{row: 0, col: 0},
Point{row: 0, col: 1},
Point{row: 1, col: 1},
Point{row: 1, col: 2},
}
case ZPiece:
retShape = Shape{
Point{row: 1, col: 0},
Point{row: 1, col: 1},
Point{row: 0, col: 1},
Point{row: 0, col: 2},
}
case JPiece:
retShape = Shape{
Point{row: 1, col: 0},
Point{row: 0, col: 1},
Point{row: 0, col: 0},
Point{row: 0, col: 2},
}
default:
panic("getShapeFromPiece(Piece): Invalid piece entered")
}
return retShape
}