go_dreamfactory/modules/entertainment/xxlmap.go
2023-10-18 09:40:37 +08:00

239 lines
4.7 KiB
Go

package entertainment
import (
"crypto/rand"
"fmt"
"go_dreamfactory/lego/core"
"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(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.Data = make(map[int32]*Girde, Width*Height)
return
}
// 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
}
}
}
sz2 := []int32{
5, 1, 3, 5, 1, 5, 2,
3, 1, 5, 4, 2, 4, 4,
4, 4, 1, 5, 6, 4, 1,
6, 3, 1, 1, 3, 6, 3,
6, 3, 5, 2, 4, 6, 1,
5, 2, 4, 2, 1, 3, 1,
6, 5, 5, 1, 2, 1, 4,
}
var index = 0
for j := Height - 1; j >= 0; j-- {
for i := 0; i < Width; i++ {
key := int32(i*10 + j)
this.Data[key].Itype = sz2[index]
index++
}
}
this.Debugf()
}
// 交换2个元素(参数 id )
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 = &Girde{
X: g1.X,
Y: g1.Y,
ID: g1.ID,
Itype: g1.Itype,
}
this.Data[i] = g2
this.Data[j] = tmp
this.Debugf()
return bSwap
}
func (this *MapData) Debugf() {
fmt.Printf("================\n")
for j := Height - 1; j >= 0; j-- {
for i := 0; i < Width; i++ {
key := int32(i*10 + j)
fmt.Printf("%d ", this.Data[key].Itype)
}
fmt.Printf("\n")
}
}
// 校验地图可消除的 判断各组上面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].Itype = 0
}
fmt.Printf("===del:%v", del)
this.Debugf()
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 []int32
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.Itype)
l++
}
}
for pos, v := range sz {
key := int32(i*10 + j + pos)
this.Data[key].Itype = v
}
for n := j + l; n < Height; n++ {
key := int32(i*10 + n)
tmp := GetRandType()
this.Data[key].Itype = tmp
}
break // 完成该列
}
}
}
this.Debugf()
return this.Data
}