go_dreamfactory/modules/entertainment/xxlmap.go

204 lines
3.9 KiB
Go

package entertainment
import (
"crypto/rand"
"fmt"
"math/big"
)
const (
Width = 7
Height = 7
)
type Girde struct {
X int32 // x
Y int32
ID int32
Itype int32
}
//地图数据
type MapData struct {
Data map[int32]*Girde // 地图数据
}
// func (this *MapData) init() {
// this.Data = make(map[int32]*Girde, Width*Height)
// }
// 1~6随机一个数
func GetRandType() int32 {
n, _ := rand.Int(rand.Reader, big.NewInt(6))
return int32(n.Int64() + 1)
}
func (this *MapData) GetKeyType(key int32) int32 {
var itype int32
if v, ok := this.Data[key]; ok {
itype = v.Itype
}
return itype
}
func (this *MapData) GetKeyData(key int32) (data *Girde) {
if v, ok := this.Data[key]; ok {
return v
}
return
}
// 初始化地图数据
func (this *MapData) InitMap() {
this.Data = make(map[int32]*Girde, Width*Height)
for i := 0; i < Width; i++ {
for j := 0; j < Height; j++ {
tmp := GetRandType()
key := int32(i*10 + j)
bOk := true // OK 的
this.Data[key] = &Girde{
X: int32(i),
Y: int32(j),
ID: key,
Itype: tmp,
}
// 校验 检查格子的左边的左边 和下边和下下边
if j-2 > 0 {
i1 := this.GetKeyType(int32(i*10 + j - 1))
i2 := this.GetKeyType(int32(i*10 + j - 2))
if i1 == i2 && tmp == i1 {
bOk = false
}
}
if i-2 > 0 {
i1 := this.GetKeyType(int32((i-1)*10 + j))
i2 := this.GetKeyType(int32((i-2)*10 + j))
if i1 == i2 && tmp == i1 {
bOk = false
}
}
if !bOk {
for i := 0; i < Width*Height; i++ {
itype := GetRandType()
if tmp != itype {
tmp = itype
break
}
}
this.Data[key].Itype = tmp
}
}
}
}
// 交换2个元素
func (this *MapData) SwapGirde(i, j int32) bool {
var (
bSwap bool // 能否交换
tmp *Girde
)
g1 := this.GetKeyData(i)
g2 := this.GetKeyData(j)
if g1 == nil || g2 == nil {
return bSwap
}
// 校验是不是挨着的
if g1.X+1 == g2.X {
bSwap = true
}
if g1.X+1 == g2.X {
bSwap = true
}
if g1.Y+1 == g2.Y {
bSwap = true
}
if g1.Y+1 == g2.Y {
bSwap = true
}
// 更新地图数据
tmp = new(Girde)
*tmp = *g1
this.Data[i] = g2
this.Data[j] = tmp
return bSwap
}
// 校验地图可消除的 判断各组上面2个和右边两个是否三个相等
func (this *MapData) CheckMap() {
var (
del map[int32]struct{}
)
del = map[int32]struct{}{}
for i := 0; i < Width; i++ {
for j := 0; j < Height; j++ {
key := int32(i*10 + j)
iType := this.GetKeyType(key)
if iType == 0 {
continue
}
if j+2 < Height {
i1 := this.GetKeyType(int32((i+1)*10 + j))
i2 := this.GetKeyType(int32((i+2)*10 + j))
if i1 == i2 && i1 == iType {
del[int32((i+1)*10+j)] = struct{}{}
del[int32((i+2)*10+j)] = struct{}{}
del[key] = struct{}{}
}
}
if i+2 < Width {
i1 := this.GetKeyType(int32(i*10 + j + 1))
i2 := this.GetKeyType(int32(i*10 + j + 2))
if i1 == i2 && i1 == iType {
del[int32(i*10+j+1)] = struct{}{}
del[int32(i*10+j+2)] = struct{}{}
del[key] = struct{}{}
}
}
}
}
// 顺着删除列表删除各组
for k := range del {
this.Data[k] = &Girde{}
}
fmt.Printf("%v", del)
return
}
// 下落 生成新的格子
func (this *MapData) DropGirde() (Data map[int32]*Girde) {
for i := 0; i < Width; i++ {
for j := 0; j < Height; j++ {
key := int32(i*10 + j)
var sz []*Girde
var l int // 落下多少个
if this.GetKeyType(key) == 0 { // 找上面的
for m := j; m < Height; m++ {
key1 := int32(i*10 + m)
t := this.GetKeyData(key1)
if t.Itype != 0 {
sz = append(sz, t)
l++
}
}
for i, v := range sz {
key := int32(i*10 + j + i)
this.Data[key] = v
}
for n := j + l; n < Height; n++ {
key := int32(i*10 + n)
tmp := GetRandType()
this.Data[key] = &Girde{
X: int32(i),
Y: int32(j),
ID: key,
Itype: tmp,
}
}
break // 完成该列
}
}
}
return this.Data
}