开心消消乐简单消除检测
最近在b站上看一个Unity教学视频,制作开心消消乐。个人感觉视频中的消除方法写得很随意,有点不爽,于是自己新写了个方法。
基本规则:三个或三个以上元素,横着连一起算消除,竖着连一起算消除。
L形和T形也算。
函数返回匹配成功的待消除对象。
视频中的消除方法大概意思是:
返回列表 匹配(待匹配元素){横向检测如果横向检测成功,加入结果列表如果横向检测成功,纵向检测如果纵向成功,加入结果列表如果结果成立,返回结果列表//同上纵向检测如果纵向检测成功,加入结果列表如果纵向检测成功,横向检测如果横向成功,加入结果列表如果结果成立,返回结果列表返回空
}
但是这样一来,弓字形是探测不到的。感觉跟我想的不太一样,于是我写了包括弓形的方法:
void 匹配 (待匹配元素){横匹配对于横匹配的每一个结果{标记为横匹配过如果没有纵匹配过{纵匹配对于纵匹配的每一个结果{标记为纵匹配过如果没有横匹配过{匹配;}}}}
}
匹配结果就是横匹配标记列表或者纵匹配标记列表
虽然是递归,但是最后,所有结果元素也只会被匹配两次,所以算法复杂度并不高
具体代码如下(color组件的color相等即为元素相等):
原函数:
public List<GamePiece> GetMatch(GamePiece piece, int newX, int newY){if (piece.IsColored()){ColorPiece.ColorType color = piece.ColorComponent.Color;List<GamePiece> horizontalPieces = new List<GamePiece>();List<GamePiece> verticalPieces = new List<GamePiece>();List<GamePiece> matchingPieces = new List<GamePiece>();horizontalPieces.Add(piece);//First check horizontal//direction: dir = 0 is left,dir = 1 is rightfor(int dir = 0;dir <= 1; dir++){for(int xOffset = 1;xOffset < xDim; xOffset++){int x;if(dir == 0){x = newX - xOffset;}else{x = newX + xOffset;}if (x < 0 || x >= xDim){break;}if(pieces[x,newY].IsColored() && pieces[x,newY].ColorComponent.Color == color){horizontalPieces.Add(pieces[x, newY]);}else{break;}}}if(horizontalPieces.Count >= 3){for(int i = 0; i < horizontalPieces.Count; i++){matchingPieces.Add(horizontalPieces[i]);}}if(horizontalPieces.Count >= 3){for(int i = 0; i < horizontalPieces.Count; i++){for(int dir = 0; dir <= 1; dir++){for(int yOffset = 1; yOffset < yDim; yOffset++){int y;if(dir == 0){y = newY - yOffset;}else{y = newY + yOffset;}if (y < 0 || y >= yDim){break;}if(pieces[horizontalPieces[i].X,y].IsColored() && pieces[horizontalPieces[i].X, y].ColorComponent.Color == color){verticalPieces.Add(pieces[horizontalPieces[i].X, y]);}else{break;}}}if(verticalPieces.Count < 2){verticalPieces.Clear();}else{for(int j = 0;j < verticalPieces.Count; j++){matchingPieces.Add(verticalPieces[j]);}break;}}}if(matchingPieces.Count >= 3){return matchingPieces;}horizontalPieces.Clear();verticalPieces.Clear();verticalPieces.Add(piece);//then check verticallyfor (int dir = 0; dir <= 1; dir++){for (int yOffset = 1; yOffset < yDim; yOffset++){int y;if (dir == 0){y = newY - yOffset;}else{y = newY + yOffset;}if (y < 0 || y >= yDim){break;}if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color){verticalPieces.Add(pieces[newX, y]);}else{break;}}}if (verticalPieces.Count >= 3){for (int i = 0; i < verticalPieces.Count; i++){matchingPieces.Add(verticalPieces[i]);}}if (verticalPieces.Count >= 3){for (int i = 0; i < verticalPieces.Count; i++){for (int dir = 0; dir <= 1; dir++){for (int xOffset = 1; xOffset < xDim; xOffset++){int x;if (dir == 0){x = newX - xOffset;}else{x = newX + xOffset;}if (x < 0 || x >= xDim){break;}if (pieces[x,verticalPieces[i].Y].IsColored() && pieces[x, verticalPieces[i].Y].ColorComponent.Color == color){verticalPieces.Add(pieces[x,verticalPieces[i].Y]);}else{break;}}}if (horizontalPieces.Count < 2){horizontalPieces.Clear();}else{for (int j = 0; j < horizontalPieces.Count; j++){matchingPieces.Add(horizontalPieces[j]);}break;}}}if (matchingPieces.Count >= 3){return matchingPieces;}}return null;}
新函数:
private List<GamePiece> horizonMatched = new List<GamePiece>();
private List<GamePiece> verticalMatched = new List<GamePiece>();public List<GamePiece> GetMatch2(GamePiece piece,int newX,int newY)
{if (!piece.IsColored()){return null;}horizonMatched.Clear();verticalMatched.Clear();Matching(piece,newX,newY);if (horizonMatched.Count < 3){return null;}return horizonMatched;
}private void Matching(GamePiece piece, int newX, int newY)
{List<GamePiece> hMatching = HorizontalMatching(piece,newX,newY);foreach(var hM in hMatching){horizonMatched.Add(hM);if (!verticalMatched.Contains(hM)){List<GamePiece> vMatching = VerticalMatching(hM, hM.X, hM.Y);foreach(var vM in vMatching){verticalMatched.Add(vM);if (!horizonMatched.Contains(vM)){Matching(vM, vM.X, vM.Y);}}}}
}private List<GamePiece> HorizontalMatching(GamePiece piece, int newX, int newY)
{List<GamePiece> horizontalMatching = new List<GamePiece>();horizontalMatching.Add(piece);for (int x = newX - 1; x >= 0; x--){if (pieces[x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == piece.ColorComponent.Color){horizontalMatching.Add(pieces[x,newY]);}else{break;}}for (int x = newX + 1; x < xDim; x++){if (pieces[x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == piece.ColorComponent.Color){horizontalMatching.Add(pieces[x,newY]);}else{break;}}if (horizontalMatching.Count < 3){horizontalMatching.Clear();horizontalMatching.Add(piece);}return horizontalMatching;
}private List<GamePiece> VerticalMatching(GamePiece piece, int newX, int newY)
{List<GamePiece> verticalMatching = new List<GamePiece>();verticalMatching.Add(piece);for (int y = newY - 1; y >= 0; y--){if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == piece.ColorComponent.Color){verticalMatching.Add(pieces[newX,y]);}else{break;}}for (int y = newY + 1; y < yDim; y++){if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == piece.ColorComponent.Color){verticalMatching.Add(pieces[newX,y]);}else{break;}}if (verticalMatching.Count < 3){verticalMatching.Clear();verticalMatching.Add(piece);}return verticalMatching;
}
完
开心消消乐简单消除检测相关推荐
- 用Python3和Pygame实现简单的开心消消乐游戏
用Python3和Pygame实现简单的开心消消乐游戏 项目简介 项目思路 现存Bug 项目截图 项目简介 项目Git:https://github.com/Accright/py-icehappy. ...
- 怎么把照片做成消消乐_开心消消乐特效制作如何快速的消除过关
开心消消乐特效制作如何快速的消除过关.在闯关的时候,我们想要制作三星过关,特效的制作是我们必须的过程.如果没有特效帮助我们大量的消除,想要得到3星的分数是比较困难的.但是在释放特效的时候,我们是需要一 ...
- [Android] 开心消消乐代码(写的比较简单)
突然想要在android上写一个消消乐的代码,在此之前没有系统地学过java的面向对象,也没有任何android相关知识,不过还是会一点C++.8月初开始搭建环境,在这上面花了相当多的时间,然后看了一 ...
- 开心消消乐java下载_开心消消乐下载_开心消消乐下载最新iPhone版-太平洋下载中心...
<开心消消乐>,以三消游戏的方式,让国民连接在一起!2021年,8亿玩家的共同选择! [App Store下载量榜首,多次精选推荐] 全民级三消游戏<开心消消乐>不仅是中国区A ...
- android飞翔的小鸟游戏素材包_开心消消乐×愤怒的小鸟:为开心而战
手机里总有那么一些游戏,是你一旦不小心打开,就完全停不下来的.在这份"一直玩一直爽游戏清单"里,绝对少不了开心消消乐和愤怒的小鸟的身影. 神奇的是,在2020的夏天,它们合体了!在 ...
- java开心消消乐代码_Vue实现开心消消乐游戏算法
摘要:这篇Vue栏目下的"Vue实现开心消消乐游戏算法",介绍的技术点是"开心消消乐.Vue.开心.游戏.算法.实现",希望对大家开发技术学习和问题解决有帮助. ...
- 用Python写个开心消消乐小游戏!自己写的游戏就是好玩!
提到开心消消乐这款小游戏,相信大家都不陌生,其曾在 2015 年获得过玩家最喜爱的移动单机游戏奖,受欢迎程度可见一斑,本文我们使用 Python 来做个简单的消消乐小游戏. 实现 消消乐的构成主要包括 ...
- 开心消消乐java下载_开心消消乐原版下载安装
开心消消乐原版最新版是非常受欢迎的手机消除游戏,在这里你可以随时随地体验正版消除游戏带来的乐趣,还有海量关卡等你来挑战,游戏操作简单,感兴趣的玩家赶快来下载体验吧! 开心消消乐原版游戏介绍 开心消消乐 ...
- 用Python 写个 开心消消乐小游戏
源码在python学习交流q群:733089476 获取 提到开心消消乐这款小游戏,相信大家都不陌生,它曾在 2015 年获得过玩家最喜爱的移动单机游戏奖,受欢迎程度可见一斑,本文我们使用 Pytho ...
最新文章
- asp.net中长内容自动分页的实现
- Python——阶段总结(一)
- src is not broadcastable to dst, but they have the same number of elements
- USTC English Club Note20171019(2)
- 20150728月度会议
- 【渝粤题库】国家开放大学2021春3924★汽车电器设备构造与检修题目
- laravel邮件服务
- python全0序列_Python合集之Python序列(一)
- office 公式编辑器 插入花体格式字母
- JMeter(三):后置处理器[Regular Expression Extractor]
- linux下替代windows的软件列表
- 中国工程院院士、中国人工智能学会理事长李德毅:人工智能研究新进展
- 淘宝网热浪引擎平台资费规则
- Leetcode|MySQL|数据库刷题记录(601~627)
- DataWorks数据建模 - 一揽子数据模型管理解决方案
- Python3时间戳转换为指定格式的日期
- stp实验心得_STP 实验
- 计算机模拟飞行,模拟飞行 DCS F-14B Tomcat雄猫 中文指南 3.22计算机地址面板
- miui android 7.1,小米4初入Android7.1 比MIUI更流畅
- 自动化立体库中WMS、WCS、PLC之间关系
热门文章
- java生成带星号条形码_Code39生成条形码加星号的解决办法以及当扫描枪扫描不到条码的解决办法 | 学步园...
- 台式计算机找不到无线连接,我的win7台式机找不到无线网卡解决方法介绍
- 已知IP地址,如何计算其子网掩码,默认网关地址,网络地址等。
- 权限系统的设计模式 ACL RBAC ABAC
- RBAC vs ABAC
- anaconda创建一个新的虚拟环境
- 高级电子技能及生产工艺流水线实训台QY-GY01A
- opengl光照效果的三棱锥+键盘上下左右控制旋转(学习笔记-仅供参考)
- 开发app需要什么技术?手机app制作方式、价格及性能盘点
- 思科、IBM、甲骨文、Uber相继裁员,寒冬将至 ?