两个特殊元素在一起也可以消除

This commit is contained in:
meixiongfeng 2023-11-07 17:48:48 +08:00
parent 22442bfb79
commit 86acb03ad9
2 changed files with 150 additions and 90 deletions

View File

@ -295,22 +295,28 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
this.player2.Ps = 0 this.player2.Ps = 0
} }
} else { } else {
bCanChange := false
oid1 = this.chessboard.Plat[req.Curid].Oid oid1 = this.chessboard.Plat[req.Curid].Oid
oid2 = this.chessboard.Plat[req.Targetid].Oid oid2 = this.chessboard.Plat[req.Targetid].Oid
if b, m := this.chessboard.CheckSpecialElemChange(req.Curid, req.Targetid, color); !b {
// 交换元素 // 交换元素
if b := this.chessboard.SwapGirde(req.Curid, req.Targetid); !b { // 交换格子 if b = this.chessboard.SwapGirde(req.Curid, req.Targetid); !b { // 交换格子
errdata = &pb.ErrorData{ errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回 Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回
Title: pb.ErrorCode_EntertainCantSwap.ToString(), Title: pb.ErrorCode_EntertainCantSwap.ToString(),
} }
return return
} }
} else {
bCanChange = true
szMap = append(szMap, m...)
}
if m, b := this.chessboard.CheckMap(color, true); len(m) > 0 { if m, b := this.chessboard.CheckMap(color, true); len(m) > 0 {
// curScore += score
szMap = append(szMap, m...) szMap = append(szMap, m...)
bAddPs = b bAddPs = b
} else { // 不能消除 bCanChange = true
}
if !bCanChange { // 不能消除
this.chessboard.SwapGirde(req.Targetid, req.Curid) // 换到原来的位置 this.chessboard.SwapGirde(req.Targetid, req.Curid) // 换到原来的位置
errdata = &pb.ErrorData{ errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回 Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回

View File

@ -51,73 +51,32 @@ func (this *MapData) GetRandType() int32 {
} }
// 玩法带入 // 玩法带入
func (this *MapData) CreateGride(index int32, bDrop bool) (girde *pb.GirdeData) { func (this *MapData) CreateGride(count int) (girdes []*pb.GirdeData) {
var ( var (
id int32 id int32
conf *cfg.GameBlockData conf *cfg.GameBlockData
err error err error
) // 权重带入 ) // 权重带入
for i := 0; i < count; i++ {
this.oid++ this.oid++
girde = &pb.GirdeData{ // 默认值 girde := &pb.GirdeData{ // 默认值
Oid: this.oid, Oid: this.oid,
Color: 1, Color: 1,
Cid: 1, Cid: 1,
Score: 1, Score: 1,
Special: 0, Special: 0,
} }
if this.iType == 4 && bDrop {
var count int32
for _, v := range this.Plat {
if v.Special == 5 || v.Special == 6 ||
v.Special == 7 || v.Special == 8 {
count++
if count >= 2 {
break
}
}
}
if count < 2 {
n, _ := rand.Int(rand.Reader, big.NewInt(6)) // 随机一个颜色
n1, _ := rand.Int(rand.Reader, big.NewInt(4)) // 随机一个消除类型
if conf, err := this.module.configure.GetGameBlock(int32(n.Int64()+1), int32(n1.Int64()+5)); err == nil {
id = conf.Key
}
} else {
id = this.GetRandType()
}
} else if this.iType == 3 && bDrop { // 创建棋盘的时候此玩法类型执行效率低下 稍后采用同颜色替换模式处理
// 最多2个
var count int32
for _, v := range this.Plat {
if v.Special == 4 {
count++
if count >= 2 {
break
}
}
}
if count < 2 {
n, _ := rand.Int(rand.Reader, big.NewInt(6)) // 随机一个颜色
if conf, err := this.module.configure.GetGameBlock(int32(n.Int64()+1), 4); err == nil {
id = conf.Key
}
} else {
id = this.GetRandType() id = this.GetRandType()
}
} else {
id = this.GetRandType()
}
conf, err = this.module.configure.GetGameBlockByKey(id) conf, err = this.module.configure.GetGameBlockByKey(id)
if err != nil { if err == nil {
return
}
girde.Color = conf.Color girde.Color = conf.Color
girde.Cid = conf.Key girde.Cid = conf.Key
girde.Special = conf.Type girde.Special = conf.Type
girde.Score = conf.Score girde.Score = conf.Score
girdes = append(girdes, girde)
}
}
return return
} }
@ -127,8 +86,8 @@ func (this *MapData) InitMap(module *Entertainment, iType int32) {
this.iType = iType this.iType = iType
this.oid = 1000 // 方便观察 从1000开始 this.oid = 1000 // 方便观察 从1000开始
this.Plat = make([]*pb.GirdeData, Width*Height) this.Plat = make([]*pb.GirdeData, Width*Height)
for i := 0; i < Width*Height; i++ { for i, v := range this.CreateGride(Total) {
this.Plat[i] = this.CreateGride(int32(i), false) this.Plat[i] = v
} }
if iType > 1 { if iType > 1 {
var sp int32 var sp int32
@ -161,6 +120,60 @@ func (this *MapData) InitMap(module *Entertainment, iType int32) {
//this.SetMap() // 方便测试固定地图 //this.SetMap() // 方便测试固定地图
} }
// 交换之前先判断是不是特殊元素的交换
func (this *MapData) CheckSpecialElemChange(oldId, newId int32, color int32) (bSwap bool, szMap []*pb.MapData) {
this.operElem = []int32{} // 初始化
if (oldId%Height+1 < Total && oldId+1 == newId) ||
(oldId%Height > 0 && oldId-1 == newId) ||
(oldId+Width < Total && oldId+Width == newId) ||
(oldId-Width >= 0 && oldId-Width == newId) {
bSwap = true
x := make(map[int]struct{})
if this.Plat[newId].Special != 0 && this.Plat[oldId].Special != 0 { // 都不为0
tmp := new(pb.GirdeData)
*tmp = *this.Plat[newId]
this.Plat[newId] = this.Plat[oldId]
this.Plat[oldId] = tmp
this.operElem = append(this.operElem, newId)
this.operElem = append(this.operElem, oldId)
x = this.SpecialElem(int(oldId), this.Plat[oldId].Special)
xc := this.SpecialElem(int(newId), this.Plat[newId].Special)
for k := range xc {
x[k] = struct{}{}
}
var energy int32
var curScore int32
for id := range x {
if this.Plat[id].Color == color {
energy++
}
curScore += this.Plat[id].Score
this.Plat[id] = &pb.GirdeData{}
this.operElem = append(this.operElem, int32(id))
}
for id := range x {
if this.Plat[id].Color == color {
energy++
}
curScore += this.Plat[id].Score
this.Plat[id] = &pb.GirdeData{}
this.operElem = append(this.operElem, int32(id))
}
if this.DropGirde() {
szMap = append(szMap, &pb.MapData{
Data: this.GetPalatData(),
CurSocre: curScore,
CurEnergy: energy,
})
}
} else {
bSwap = false
}
}
return
}
// 交换2个元素(参数 oid ) // 交换2个元素(参数 oid )
func (this *MapData) SwapGirde(oldId, newId int32) (bSwap bool) { func (this *MapData) SwapGirde(oldId, newId int32) (bSwap bool) {
this.operElem = []int32{} // 初始化 this.operElem = []int32{} // 初始化
@ -198,13 +211,7 @@ func (this *MapData) Check5X() (bEliminate bool, xiaochu []int, s map[int]int) {
//var xiaochu []int // 即将消除的key //var xiaochu []int // 即将消除的key
s = make(map[int]int) s = make(map[int]int)
for k, v := range this.Plat { for k, v := range this.Plat {
// b := false
// for _, e := range this.operElem {
// if e == int32(k) {
// b = true
// break
// }
// }
if v.Cid == 0 { if v.Cid == 0 {
continue continue
} }
@ -256,15 +263,8 @@ func (this *MapData) Check4X() (bEliminate bool, xiaochu []int, s map[int]int) {
) )
s = make(map[int]int) s = make(map[int]int)
//var xiaochu []int // 即将消除的key
for k, v := range this.Plat { for k, v := range this.Plat {
// b := false
// for _, e := range this.operElem {
// if e == int32(k) {
// b = true
// break
// }
// }
if v.Cid == 0 { if v.Cid == 0 {
continue continue
} }
@ -478,8 +478,6 @@ func (this *MapData) CheckMap(color int32, bSkill bool) (szMap []*pb.MapData, xc
CurSocre: curScore, CurSocre: curScore,
CurEnergy: energy, CurEnergy: energy,
}) })
//this.Debugf()
} }
// 检查掉落 // 检查掉落
@ -493,6 +491,8 @@ func (this *MapData) CheckMap(color int32, bSkill bool) (szMap []*pb.MapData, xc
// 下落 生成新的格子 (返回掉落所获得的分数) // 下落 生成新的格子 (返回掉落所获得的分数)
func (this *MapData) DropGirde() bool { func (this *MapData) DropGirde() bool {
// 需要填充的格子
var fill []int
bDrop := false bDrop := false
for i := 0; i < Width; i++ { for i := 0; i < Width; i++ {
for j := 0; j < Height; j++ { for j := 0; j < Height; j++ {
@ -510,12 +510,66 @@ func (this *MapData) DropGirde() bool {
} }
for m := j + add; m < Height; m++ { for m := j + add; m < Height; m++ {
k1 := i*Width + m k1 := i*Width + m
this.Plat[k1] = this.CreateGride(int32(k1), true) fill = append(fill, k1)
//this.Plat[k1] = this.CreateGride(true)
} }
break break
} }
} }
} }
sz := this.CreateGride(len(fill))
for pos, id := range fill {
this.Plat[id] = sz[pos]
}
if this.iType > 1 {
var sp int32
var mp map[int]struct{}
mp = make(map[int]struct{}, 0)
var count int32
for pos, v := range this.Plat {
if this.iType == 4 {
if v.Special == 5 || v.Special == 6 ||
v.Special == 7 || v.Special == 8 {
mp[pos] = struct{}{}
count++
if count >= 2 {
break
}
}
} else if this.iType == 3 {
if v.Special == 4 {
mp[pos] = struct{}{}
count++
if count >= 2 {
break
}
}
}
}
if len(mp) < 2 {
for {
n1, _ := rand.Int(rand.Reader, big.NewInt(int64(len(fill))))
mp[fill[n1.Int64()]] = struct{}{}
if len(mp) >= 2 {
break
}
}
if this.iType == 3 {
sp = 4
} else if this.iType == 4 {
n1, _ := rand.Int(rand.Reader, big.NewInt(4)) // 随机一个消除类型
sp = int32(n1.Int64()) + 5
}
for key := range mp {
if conf, err := this.module.configure.GetGameBlock(this.Plat[key].Color, sp); err == nil {
this.Plat[key].Cid = conf.Key
this.Plat[key].Special = conf.Type
this.Plat[key].Score = conf.Score
}
}
}
}
return bDrop return bDrop
} }
@ -690,9 +744,9 @@ func (this *MapData) SetMap() {
5, 1, 2, 3, 1, 2, 2, 5, 1, 2, 3, 1, 2, 2,
3, 4, 3, 5, 6, 1, 6, 3, 4, 3, 5, 6, 1, 6,
1, 4, 1, 4, 2, 3, 6, 1, 4, 1, 4, 2, 3, 6,
1, 5, 1, 5, 6, 1, 4, 1, 5, 6, 5, 6, 1, 4,
6, 6, 4, 6, 4, 3, 3, 6, 6, 3, 6, 4, 3, 3,
3, 3, 1, 2, 1, 2, 5, 3, 3, 1, 3, 1, 2, 5,
// 1, 4, 2, 5, 4, 5, 2, // 1, 4, 2, 5, 4, 5, 2,
// 3, 2, 3, 5, 2, 1, 4, // 3, 2, 3, 5, 2, 1, 4,
// 2, 5, 5, 4, 5, 4, 1, // 2, 5, 5, 4, 5, 4, 1,