From 785b2f7881aefa58808ce9ca8401fa15b6ebc8b9 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Thu, 19 Oct 2023 16:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E5=88=99=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/entertainment/room.go | 19 ++-- modules/entertainment/xxl_test.go | 5 +- modules/entertainment/xxlmap.go | 161 +++++++++++++++++++----------- 3 files changed, 114 insertions(+), 71 deletions(-) diff --git a/modules/entertainment/room.go b/modules/entertainment/room.go index 4c215dff8..45f8e2eca 100644 --- a/modules/entertainment/room.go +++ b/modules/entertainment/room.go @@ -105,18 +105,15 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr // 交换元素 this.chessboard.SwapGirde(req.Curid, req.Targetid) // 交换格子 - for { - if b, score := this.chessboard.CheckMap(); b { - break - } else { - curScore += score // 统计积分 - } - - this.chessboard.DropGirde() - szMap = append(szMap, &pb.MapData{ - Data: this.chessboard.Data, - }) + szMap = append(szMap, &pb.MapData{ + Data: this.chessboard.Data, + }) + this.chessboard.CheckMap() + if score, m := this.chessboard.DropGirde(); score > 0 { + curScore += score + szMap = append(szMap, m...) } + // 操作消息返回 session.SendMsg(string(this.module.GetType()), "operator", &pb.EntertainOperatorResp{ Success: true, diff --git a/modules/entertainment/xxl_test.go b/modules/entertainment/xxl_test.go index 84898375f..be490e3b6 100644 --- a/modules/entertainment/xxl_test.go +++ b/modules/entertainment/xxl_test.go @@ -72,8 +72,11 @@ func Test_Main(t *testing.T) { m := new(entertainment.MapData) m.InitMap() - m.SwapGirde(0, 1) + m.SwapGirde(1, 11) + fmt.Printf("得分:%d\n", m.CheckMap()) + m.DropGirde() flag.Parse() + s := NewService( rpcx.SetConfPath(*conf), rpcx.SetVersion("1.0.0.0"), diff --git a/modules/entertainment/xxlmap.go b/modules/entertainment/xxlmap.go index 22d46b1f4..9b0a01c54 100644 --- a/modules/entertainment/xxlmap.go +++ b/modules/entertainment/xxlmap.go @@ -100,7 +100,7 @@ func (this *MapData) InitMap() { 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, + 5, 6, 5, 5, 1, 3, 1, 6, 5, 5, 1, 2, 1, 4, } var index = 0 @@ -111,7 +111,7 @@ func (this *MapData) InitMap() { index++ } } - //this.Debugf() + this.Debugf() } // 交换2个元素(参数 id ) @@ -139,13 +139,13 @@ func (this *MapData) SwapGirde(i, j int32) bool { } this.Data[i] = g2 this.Data[j] = tmp - if !this.CheckSwape() { // 交换后不能消除 + if !this.CheckSwape(i, j) { // 交换后不能消除 this.Data[i] = tmp this.Data[j] = g2 bSwap = false } } - + this.Debugf() return bSwap } @@ -162,20 +162,23 @@ func (this *MapData) Debugf() { // 检查5消 func (this *MapData) Check5X() (bEliminate bool, score int32) { - m := make(map[int32]struct{}, 0) - + m := make(map[int32]struct{}, 5) bEliminate = true del := make(map[int32]int32) for i := 0; i < Width; i++ { for j := 0; j < Height; j++ { + m = make(map[int32]struct{}, 0) key := int32(i*10 + j) iType := this.GetKeyType(key) if iType == 0 { continue } curKey := this.Data[key].Itype + if curKey == 0 { + continue + } m[curKey] = struct{}{} - if j+4 < Height { + if i+4 < Width { i1 := this.Data[int32((i+1)*10+j)] i2 := this.Data[int32((i+2)*10+j)] i3 := this.Data[int32((i+3)*10+j)] @@ -186,7 +189,7 @@ func (this *MapData) Check5X() (bEliminate bool, score int32) { m[i3.Itype] = struct{}{} m[i4.Itype] = struct{}{} - if len(m) == 5 { + if len(m) == 1 { del[int32((i+1)*10+j)] = i1.Score del[int32((i+2)*10+j)] = i2.Score del[int32((i+3)*10+j)] = i3.Score @@ -194,7 +197,8 @@ func (this *MapData) Check5X() (bEliminate bool, score int32) { del[key] = this.Data[key].Score } } - if i+4 < Width { + m = map[int32]struct{}{} + if j+4 < Height { i1 := this.Data[int32(i*10+j+1)] i2 := this.Data[int32(i*10+j+2)] i3 := this.Data[int32(i*10+j+3)] @@ -203,7 +207,7 @@ func (this *MapData) Check5X() (bEliminate bool, score int32) { m[i2.Itype] = struct{}{} m[i3.Itype] = struct{}{} m[i4.Itype] = struct{}{} - if len(m) == 5 { + if len(m) == 1 { // 1 2 3 4 5 del[int32(i*10+j+1)] = i1.Score del[int32(i*10+j+2)] = i2.Score del[int32(i*10+j+3)] = i3.Score @@ -220,12 +224,13 @@ func (this *MapData) Check5X() (bEliminate bool, score int32) { } if len(del) > 0 { bEliminate = false + fmt.Printf("5x消除格子===del:%v\n", del) } - fmt.Printf("===del:%v", del) + return } func (this *MapData) Check4X() (bEliminate bool, score int32) { - bEliminate = false + bEliminate = true del := make(map[int32]int32) for i := 0; i < Width; i++ { for j := 0; j < Height; j++ { @@ -234,7 +239,7 @@ func (this *MapData) Check4X() (bEliminate bool, score int32) { if iType == 0 { continue } - if j+3 < Height { + if i+3 < Width { i1 := this.Data[int32((i+1)*10+j)] i2 := this.Data[int32((i+2)*10+j)] i3 := this.Data[int32((i+3)*10+j)] @@ -243,9 +248,10 @@ func (this *MapData) Check4X() (bEliminate bool, score int32) { del[int32((i+2)*10+j)] = i2.Score del[int32((i+3)*10+j)] = i3.Score del[key] = this.Data[key].Score + } } - if i+3 < Width { + if j+3 < Height { i1 := this.Data[int32(i*10+j+1)] i2 := this.Data[int32(i*10+j+2)] i3 := this.Data[int32(i*10+j+3)] @@ -254,6 +260,7 @@ func (this *MapData) Check4X() (bEliminate bool, score int32) { del[int32(i*10+j+2)] = i2.Score del[int32(i*10+j+3)] = i3.Score del[key] = this.Data[key].Score + } } } @@ -264,9 +271,10 @@ func (this *MapData) Check4X() (bEliminate bool, score int32) { score += v } if len(del) > 0 { - bEliminate = true + bEliminate = false + fmt.Printf("4x消除格子===del:%v\n", del) } - fmt.Printf("===del:%v", del) + return } @@ -280,7 +288,8 @@ func (this *MapData) Check3X() (bEliminate bool, score int32) { if iType == 0 { continue } - if j+2 < Height { + + if i+2 < Width { i1 := this.Data[int32((i+1)*10+j)] i2 := this.Data[int32((i+2)*10+j)] if i1.Itype == i2.Itype && i1.Itype == iType { @@ -289,7 +298,7 @@ func (this *MapData) Check3X() (bEliminate bool, score int32) { del[key] = i1.Score } } - if i+2 < Width { + if j+2 < Height { i1 := this.Data[int32(i*10+j+1)] i2 := this.Data[int32(i*10+j+2)] if i1.Itype == i2.Itype && i1.Itype == iType { @@ -307,12 +316,12 @@ func (this *MapData) Check3X() (bEliminate bool, score int32) { } if len(del) > 0 { bEliminate = false + fmt.Printf("3x消除格子===del:%v\n", del) } - fmt.Printf("===del:%v", del) return } -func (this *MapData) CheckSwape() (bEliminate bool) { +func (this *MapData) CheckSwape(g1, g2 int32) (bEliminate bool) { for i := 0; i < Width; i++ { for j := 0; j < Height; j++ { @@ -322,17 +331,27 @@ func (this *MapData) CheckSwape() (bEliminate bool) { continue } if j+2 < Height { - i1 := this.Data[int32((i+1)*10+j)] - i2 := this.Data[int32((i+2)*10+j)] + k1 := int32((i+1)*10 + j) + k2 := int32((i+2)*10 + j) + i1 := this.Data[k1] + i2 := this.Data[k2] if i1.Itype == i2.Itype && i1.Itype == iType { - return true + if g1 == key || k1 == g1 || k2 == g1 || + g2 == key || k1 == g2 || k2 == g2 { + return true + } } } if i+2 < Width { - i1 := this.Data[int32(i*10+j+1)] - i2 := this.Data[int32(i*10+j+2)] + k1 := int32(i*10 + j + 1) + k2 := int32(i*10 + j + 2) + i1 := this.Data[k1] + i2 := this.Data[k2] if i1.Itype == i2.Itype && i1.Itype == iType { - return true + if g1 == key || k1 == g1 || k2 == g1 || + g2 == key || k1 == g2 || k2 == g2 { + return true + } } } } @@ -342,54 +361,78 @@ func (this *MapData) CheckSwape() (bEliminate bool) { } // 校验地图可消除的 判断各组上面2个和右边两个是否三个相等 -func (this *MapData) CheckMap() (bEliminate bool, score int32) { +func (this *MapData) CheckMap() (score int32) { + score = 0 if bRet, s := this.Check5X(); !bRet { + fmt.Printf("=====检测消除5x===========\n") score += s } if bRet, s := this.Check4X(); !bRet { + fmt.Printf("=====检测消除4x===========\n") score += s } if bRet, s := this.Check3X(); !bRet { + fmt.Printf("=====检测消除3x===========\n") score += s } - if score > 0 { - bEliminate = true - } - //this.Debugf() + + this.Debugf() return } -// 下落 生成新的格子 -func (this *MapData) DropGirde() { - 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 // 完成该列 +// 下落 生成新的格子 (返回掉落所获得的分数) +func (this *MapData) DropGirde() (score int32, szMap []*pb.MapData) { + var bDrop bool + for { + bDrop = false + for _, v := range this.Data { // 校验是否有下落 + if v.Itype == 0 { + fmt.Printf("检测有掉落\n") + bDrop = true // 有空位置 说明可以掉落 + break } } + if !bDrop { + break + } + 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 // 完成该列 + } + } + } + if s := this.CheckMap(); s > 0 { + fmt.Printf("本次掉落得分:%d\n", s) + score += s + szMap = append(szMap, &pb.MapData{ + Data: this.Data, + }) + } } - //this.Debugf() + + this.Debugf() return }