go-tetris/minos.go

105 lines
3.0 KiB
Go
Raw Permalink Normal View History

2017-03-27 15:07:29 -05:00
package main
import (
"math/rand"
2019-12-01 15:03:27 -06:00
"github.com/gdamore/tcell"
2017-03-27 15:07:29 -05:00
)
2018-10-02 20:21:08 -05:00
// NewMinos creates the minos and minoBag
func NewMinos() {
2017-03-27 15:07:29 -05:00
minoI := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorBlank, colorCyan, colorBlank, colorBlank},
[]tcell.Color{colorBlank, colorCyan, colorBlank, colorBlank},
[]tcell.Color{colorBlank, colorCyan, colorBlank, colorBlank},
[]tcell.Color{colorBlank, colorCyan, colorBlank, colorBlank},
2017-03-27 15:07:29 -05:00
}
minoJ := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorBlue, colorBlue, colorBlank},
[]tcell.Color{colorBlank, colorBlue, colorBlank},
[]tcell.Color{colorBlank, colorBlue, colorBlank},
2017-03-27 15:07:29 -05:00
}
minoL := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorBlank, colorWhite, colorBlank},
[]tcell.Color{colorBlank, colorWhite, colorBlank},
[]tcell.Color{colorWhite, colorWhite, colorBlank},
2017-03-27 15:07:29 -05:00
}
minoO := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorYellow, colorYellow},
[]tcell.Color{colorYellow, colorYellow},
2017-03-27 15:07:29 -05:00
}
minoS := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorBlank, colorGreen, colorBlank},
[]tcell.Color{colorGreen, colorGreen, colorBlank},
[]tcell.Color{colorGreen, colorBlank, colorBlank},
2017-03-27 15:07:29 -05:00
}
minoT := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorBlank, colorMagenta, colorBlank},
[]tcell.Color{colorMagenta, colorMagenta, colorBlank},
[]tcell.Color{colorBlank, colorMagenta, colorBlank},
2017-03-27 15:07:29 -05:00
}
minoZ := MinoBlocks{
2019-12-01 15:03:27 -06:00
[]tcell.Color{colorRed, colorBlank, colorBlank},
[]tcell.Color{colorRed, colorRed, colorBlank},
[]tcell.Color{colorBlank, colorRed, colorBlank},
2017-03-27 15:07:29 -05:00
}
var minoRotationI MinoRotation
minoRotationI[0] = minoI
for i := 1; i < 4; i++ {
minoRotationI[i] = minosCloneRotateRight(minoRotationI[i-1])
2017-03-27 15:07:29 -05:00
}
var minoRotationJ MinoRotation
minoRotationJ[0] = minoJ
for i := 1; i < 4; i++ {
minoRotationJ[i] = minosCloneRotateRight(minoRotationJ[i-1])
2017-03-27 15:07:29 -05:00
}
var minoRotationL MinoRotation
minoRotationL[0] = minoL
for i := 1; i < 4; i++ {
minoRotationL[i] = minosCloneRotateRight(minoRotationL[i-1])
2017-03-27 15:07:29 -05:00
}
var minoRotationO MinoRotation
minoRotationO[0] = minoO
minoRotationO[1] = minoO
minoRotationO[2] = minoO
minoRotationO[3] = minoO
var minoRotationS MinoRotation
minoRotationS[0] = minoS
for i := 1; i < 4; i++ {
minoRotationS[i] = minosCloneRotateRight(minoRotationS[i-1])
2017-03-27 15:07:29 -05:00
}
var minoRotationT MinoRotation
minoRotationT[0] = minoT
for i := 1; i < 4; i++ {
minoRotationT[i] = minosCloneRotateRight(minoRotationT[i-1])
2017-03-27 15:07:29 -05:00
}
var minoRotationZ MinoRotation
minoRotationZ[0] = minoZ
for i := 1; i < 4; i++ {
minoRotationZ[i] = minosCloneRotateRight(minoRotationZ[i-1])
2017-03-27 15:07:29 -05:00
}
minos = &Minos{
minoBag: [7]MinoRotation{minoRotationI, minoRotationJ, minoRotationL, minoRotationO, minoRotationS, minoRotationT, minoRotationZ},
bagRand: rand.Perm(7),
}
2017-03-27 15:07:29 -05:00
}
2018-10-02 20:21:08 -05:00
// minosCloneRotateRight clones a mino and rotates the mino to the right
func minosCloneRotateRight(minoBlocks MinoBlocks) MinoBlocks {
2017-03-27 15:07:29 -05:00
length := len(minoBlocks)
newMinoBlocks := make(MinoBlocks, length, length)
for i := 0; i < length; i++ {
2019-12-01 15:03:27 -06:00
newMinoBlocks[i] = make([]tcell.Color, length, length)
2017-03-27 15:07:29 -05:00
}
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
newMinoBlocks[length-j-1][i] = minoBlocks[i][j]
}
}
return newMinoBlocks
}