This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmino.go
188 lines (165 loc) · 4.22 KB
/
mino.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
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
187
188
package main
import (
"math/rand"
"github.com/gdamore/tcell"
)
// NewMino creates a new Mino
func NewMino() *Mino {
minoRotation := minos.minoBag[minos.bagRand[minos.bagIndex]]
minos.bagIndex++
if minos.bagIndex > 6 {
minos.bagIndex = 0
minos.bagRand = rand.Perm(7)
}
mino := &Mino{
minoRotation: minoRotation,
length: len(minoRotation[0]),
}
mino.x = board.width/2 - (mino.length+1)/2
mino.y = -1
return mino
}
// CloneMoveLeft creates copy of the mino and moves it left
func (mino *Mino) CloneMoveLeft() *Mino {
newMino := *mino
newMino.MoveLeft()
return &newMino
}
// MoveLeft moves the mino left
func (mino *Mino) MoveLeft() {
mino.x--
}
// CloneMoveRight creates copy of the mino and moves it right
func (mino *Mino) CloneMoveRight() *Mino {
newMino := *mino
newMino.MoveRight()
return &newMino
}
// MoveRight moves the mino right
func (mino *Mino) MoveRight() {
mino.x++
}
// CloneRotateRight creates copy of the mino and rotates it right
func (mino *Mino) CloneRotateRight() *Mino {
newMino := *mino
newMino.RotateRight()
return &newMino
}
// RotateRight rotates the mino right
func (mino *Mino) RotateRight() {
mino.rotation++
if mino.rotation > 3 {
mino.rotation = 0
}
}
// CloneRotateLeft creates copy of the mino and rotates it left
func (mino *Mino) CloneRotateLeft() *Mino {
newMino := *mino
newMino.RotateLeft()
return &newMino
}
// RotateLeft rotates the mino left
func (mino *Mino) RotateLeft() {
if mino.rotation < 1 {
mino.rotation = 3
return
}
mino.rotation--
}
// CloneMoveDown creates copy of the mino and moves it down
func (mino *Mino) CloneMoveDown() *Mino {
newMino := *mino
newMino.MoveDown()
return &newMino
}
// MoveDown moves the mino down
func (mino *Mino) MoveDown() {
mino.y++
}
// MoveUp moves the mino up
func (mino *Mino) MoveUp() {
mino.y--
}
// ValidLocation check if the mino is in a valid location
func (mino *Mino) ValidLocation(mustBeOnBoard bool) bool {
minoBlocks := mino.minoRotation[mino.rotation]
for i := 0; i < mino.length; i++ {
for j := 0; j < mino.length; j++ {
if minoBlocks[i][j] == colorBlank {
continue
}
if !board.ValidBlockLocation(mino.x+i, mino.y+j, mustBeOnBoard) {
return false
}
}
}
return true
}
// SetOnBoard attaches mino to the board
func (mino *Mino) SetOnBoard() {
minoBlocks := mino.minoRotation[mino.rotation]
for i := 0; i < mino.length; i++ {
for j := 0; j < mino.length; j++ {
if minoBlocks[i][j] != colorBlank {
board.SetColor(mino.x+i, mino.y+j, minoBlocks[i][j], mino.rotation)
}
}
}
}
// DrawMino draws the mino on the board
func (mino *Mino) DrawMino(minoType MinoType) {
minoBlocks := mino.minoRotation[mino.rotation]
for i := 0; i < mino.length; i++ {
for j := 0; j < mino.length; j++ {
if minoBlocks[i][j] != colorBlank {
switch minoType {
case MinoPreview:
view.DrawPreviewMinoBlock(i, j, minoBlocks[i][j], mino.rotation, mino.length)
case MinoDrop:
view.DrawBlock(mino.x+i, mino.y+j, colorBlank, mino.rotation)
case MinoCurrent:
if ValidDisplayLocation(mino.x+i, mino.y+j) {
view.DrawBlock(mino.x+i, mino.y+j, minoBlocks[i][j], mino.rotation)
}
}
}
}
}
}
// minoOverlap check if a mino overlaps another mino
func (mino *Mino) minoOverlap(mino1 *Mino) bool {
minoBlocks := mino.minoRotation[mino.rotation]
for i := 0; i < mino.length; i++ {
for j := 0; j < mino.length; j++ {
if minoBlocks[i][j] == colorBlank {
continue
}
if mino1.isMinoAtLocation(mino.x+i, mino.y+j) {
return true
}
}
}
return false
}
// isMinoAtLocation check if a mino block is in a location
func (mino *Mino) isMinoAtLocation(x int, y int) bool {
xIndex := x - mino.x
yIndex := y - mino.y
if xIndex < 0 || xIndex >= mino.length || yIndex < 0 || yIndex >= mino.length {
return false
}
if mino.minoRotation[mino.rotation][xIndex][yIndex] != colorBlank {
return true
}
return false
}
// getMinoColorAtLocation gets the mino color at a location
func (mino *Mino) getMinoColorAtLocation(x int, y int) tcell.Color {
xIndex := x - mino.x
yIndex := y - mino.y
if xIndex < 0 || xIndex >= mino.length || yIndex < 0 || yIndex >= mino.length {
return colorBlank
}
minoBlocks := mino.minoRotation[mino.rotation]
return minoBlocks[xIndex][yIndex]
}