消消乐游戏算法实现(三消乐)

http://img.bbs.csdn.net/upload/201504/28/1430189081_177749.gif


先上前端效果图

3消乐地图初始化的时候不允许有下面两种情况的发生,就是不允许3个(3个以上已经包含3个,所以只要判断3个就可以了)相同颜色的格子连在一起,

下图是两种情况,细分分为6种情况,就是初始点为3个格子中的一个为一种情况,所以细分2*3=6种情况


代码中的方法是

private boolean isLine(int x, int y){....}


代码过多,不写出来了

首先初始化地图,看代码注释应该看差不多了



 

3消规则,只要地图中包含其中以下3种情况就可以判断该地图不是死图,红色部分表示

相同颜色的格子,黄色代表如果这个位置如果也是相同颜色只要一动一个位置就可以

3个相同颜色格子并排在一起


比如第一张图,首先判断它上或者下是否有相同颜色

如果1格子是初始格子是红色

 

第一种

先判断标识2格子是否为红色,如果不是一图的情况不用判断了,如果也是红色

那么只要判断上面第一张图的4个黄色位置的格子只要有一个是红色,那么1格子就不是死格子,那么这个图就不是死图

第二种

2张图,只要判断任意两个相邻黄色位置的格子(4种情况:ab同时为红,bd,dcac)的颜色也是红色那么该格子不是死格子,该图不是死图

第三种

跟第二种很像,不过相邻变成了左右,我就不说了

细分的话应该有 2*4+4+2*4=20种情况,所以这个方法的代码量最大,不细说了

代码方法是private boolean isDie(int x, int y) {...}

 


判断这个格子是否是3个以上颜色相同格子相连

比如以1格子为起点,然后向前后左右4个方向扩张

用递归的方法,就有4个方法,每个方法添加相

代码大概如下

colSet =上下相邻颜色相同的格子=向上颜色的格子+向下颜色的格子       

rowSet =左右相邻颜色相同的格子=向左颜色的格子+向右颜色的格子     

如果他们等于3个或者3个以上,那么他们就要被消,先存起来  removeCellSet    

后面再一次性消玩  i为格子的x坐标,jy坐标

 


 

 

 

 




户端要求如果不相连的区域要分离出来发给他们,分离出来的列表都要排序,这个要求比较蛋疼

格子坐标(x,y)

格子还有颜色属性Color

比如上图,removeCellSet包含上面格子的key=x+_+y;

 

只能用递归,

向相邻的格子扩张,如果相同颜色并且在removeCellSet里面 



格子消掉并下降

















0 1 2 3

例如上图给子格子下降

获取所有要消除的给子的x轴

比如有x=0;x=2;x=3这3列中都有空格

然后给这3列的非空格子排序,并重新按顺序填充格子,y大排下面,排完后剩下就为空,效果如下(我这是最简单的方法,不易出错,这个可以优化,优化就比较复杂了)

















 

3消中最重要的的方法在这里,上面的方法都在这下面按顺序执行

 



上面代码的流程图

12交换



1





2













交换后





















消除后





















 

 

下降后





















补图





















因为能消,所以再消





















下降





















 

再补图





















 

再消,但不能再消乐,得移动其中的格子

代码结构

package com.eyugame.tade.module.glops.constant;

/** 
 * 宝石颜色
 *
 * @author  k60
 */
public enum Color {

	/**
	 * 红
	 */
	RED,
	
	/**
	 * 黄
	 */
	YELLOW,
	
	/**
	 * 蓝
	 */
	BLUE,
	
	/**
	 * 绿
	 */
	GREEN,
	
	/**
	 * 紫
	 */
	PURPLE
	
	
	
}

package com.eyugame.tade.module.glops.play;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.eyugame.tade.module.glops.constant.Color;
import com.eyugame.tade.module.glops.exception.NearCellException;
import com.eyugame.tade.module.glops.exception.NoSpoilageException;
import com.eyugame.tade.module.glops.model.Cell;
import com.eyugame.tade.module.glops.model.RemoveScaleResult;

/**
 * 
 * 
 * @author pengwei
 */
public class BasePlay {

	private final static String LINK = "_";
	/**
	 * 地图
	 */
	private Cell[][] maps;

	/**
	 * 横轴单元格数量
	 */
	private int xSize;

	/**
	 * 竖轴单元格数量
	 */
	private int ySize;
	/**
	 * 随机数
	 */
	private Random random;
	/**
	 * 可以供随机的颜色
	 */
	private List<String> liveColorList = new ArrayList<String>();

	/**
	 * 一次移动的一组(可能多次消除和生成)
	 */
	private List<RemoveScaleResult> removeScaleResultList;

	/**
	 * 要移除的位置
	 */
	private Set<String> removeCellSet = new HashSet<String>();

	/**
	 * 构造方法
	 * 
	 * @param xSize
	 * @param ySize
	 */
	public BasePlay(int xSize, int ySize) {
		super();
		this.xSize = xSize;
		this.ySize = ySize;
		this.maps = new Cell[xSize][ySize];
		random = new Random();
		this.initMaps();
		while (this.isDieMap()) {
			this.initMaps();
		}
	}

	/**
	 * 初始化地图,给地图上色
	 */
	private void initMaps() {
		this.initLiveColor();
		for (int i = 0; i < this.xSize; i++) {
			for (int j = 0; j < this.ySize; j++) {
				// 可供选择的颜色
				int liveSize = liveColorList.size();
				// 判断该位置是否有可供选择的颜色
				if (liveSize > 0) {
					// 随机颜色
					int tem = random.nextInt(liveSize);
					Cell cell = new Cell();
					String liveColor = liveColorList.get(tem);
					// 给格子上坐标跟颜色
					cell.setX(i);
					cell.setY(j);
					cell.setColor(Color.valueOf(liveColor));
					// 放进地图
					maps[i][j] = cell;
					// 判断该格子是否有3个连在一起
					if (this.isLine(i, j)) {
						// 如果是有颜色重叠,从供选择的颜色中去掉该颜色,并重新随机颜色
						j = j - 1;
						liveColorList.remove(liveColor);
					} else {
						// 如果颜色没有3个重复,则初始化可供选择颜色
						this.initLiveColor();
					}
				} else {
					// 如果没有可以选择的颜色,初始化地图
					this.maps = new Cell[xSize][ySize];
					this.initMaps();
					return;
				}
			}
		}
	}

	/**
	 * 初始化随机颜色
	 */
	private void initLiveColor() {
		liveColorList = new ArrayList<String>();
		Color[] colors = Color.values();
		for (Color color : colors) {
			liveColorList.add(new String(color.toString()));
		}
	}

	/**
	 * 填充地图 不允许3格一排或者一列
	 * 
	 * @param x
	 *            填充格子的x轴
	 * @param y
	 *            填充格子的y轴
	 * @return 是否填充成功
	 */
	private boolean isLine(int x, int y) {
		boolean lx1 = x - 1 > -1;
		boolean lx2 = x - 2 > -1;
		boolean bx1 = x + 1 < this.xSize;
		boolean bx2 = x + 2 < this.xSize;
		boolean ly1 = y - 1 > -1;
		boolean ly2 = y - 2 > -1;
		boolean by1 = y + 1 < this.ySize;
		boolean by2 = y + 2 < this.ySize;
		if (ly1 && by1) {
			if (isCellColorEqual(maps[x][y - 1], maps[x][y], maps[x][y + 1])) {
				return true;
			}
		}
		if (lx1 && bx1) {
			if (isCellColorEqual(maps[x - 1][y], maps[x][y], maps[x + 1][y])) {
				return true;
			}
		}
		if (ly2) {
			if (isCellColorEqual(maps[x][y], maps[x][y - 1], maps[x][y - 2])) {
				return true;
			}
		}
		if (by2) {
			if (isCellColorEqual(maps[x][y], maps[x][y + 1], maps[x][y + 2])) {
				return true;
			}
		}

		if (lx2) {
			if (isCellColorEqual(maps[x][y], maps[x - 1][y], maps[x - 2][y])) {
				return true;
			}
		}
		if (bx2) {
			if (isCellColorEqual(maps[x][y], maps[x + 1][y], maps[x + 2][y])) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 相邻3个格子是否同一颜色
	 * 
	 * @param cell1
	 *            格子1
	 * @param cell2
	 *            格子2
	 * @param cell3
	 *            格子3
	 * @return 统一颜色为true,不同为false
	 */
	private boolean isCellColorEqual(Cell cell1, Cell cell2, Cell cell3) {
		if (cell1 != null && cell2 != null && cell3 != null) {
			Color color1 = cell1.color;
			Color color2 = cell2.color;
			Color color3 = cell3.color;
			if (color1 != null && color2 != null && color3 != null) {
				return (color1 == color2 && color1 == color3);
			}
		}
		return false;
	}

	/**
	 * 在补图要添加的格子中相邻3个格子是否同一颜色
	 * 
	 * @param cell1
	 *            格子1
	 * @param cell2
	 *            格子2
	 * @param cell3
	 *            格子3
	 * @return 统一颜色为true,不同为false
	 */
	private boolean isCellColorEqualInAddCell(Cell cell1, Cell cell2, Cell cell3, Set<Cell> set) {
		if (cell1 != null && cell2 != null && cell3 != null) {
			if (set.contains(cell1) && set.contains(cell2) && set.contains(cell3)) {
				Color color1 = cell1.color;
				Color color2 = cell2.color;
				Color color3 = cell3.color;
				if (color1 != null && color2 != null && color3 != null) {
					return (color1 == color2 && color1 == color3);
				}
			}
		}
		return false;
	}

	/**
	 * 右边颜色一样的格子
	 */
	private void isCellColorEqualRight(int x, int y, Color color, Set<String> set) {
		set.add(this.getKey(x, y));
		int newX = x + 1;
		if (newX < this.xSize) {
			if (maps[newX][y] != null && maps[newX][y].color == color) {
				this.isCellColorEqualRight(newX, y, color, set);
			}
		}

	}

	/**
	 * 左边颜色一样的格子
	 */
	private void isCellColorEqualLeft(int x, int y, Color color, Set<String> set) {
		set.add(this.getKey(x, y));
		int newX = x - 1;
		if (newX >= 0) {
			if (maps[newX][y] != null && maps[newX][y].color == color) {
				this.isCellColorEqualLeft(newX, y, color, set);
			}
		}

	}

	/**
	 * 主键生成
	 * 
	 * @param x
	 *            x坐标
	 * @param y
	 *            y坐标
	 * @return
	 */
	private String getKey(int x, int y) {
		return x + BasePlay.LINK + y;
	}

	/**
	 * 上边颜色一样的格子
	 */
	private void isCellColorEqualUp(int x, int y, Color color, Set<String> set) {
		set.add(this.getKey(x, y));
		int newY = y - 1;
		if (newY >= 0) {
			if (maps[x][newY] != null && maps[x][newY].color == color) {
				this.isCellColorEqualUp(x, newY, color, set);
			}
		}
	}

	/**
	 * 下边颜色一样的格子
	 */
	private void isCellColorEqualDown(int x, int y, Color color, Set<String> set) {
		set.add(this.getKey(x, y));
		int newY = y + 1;
		if (newY < this.ySize) {
			if (maps[x][newY] != null && maps[x][newY].color == color) {
				this.isCellColorEqualDown(x, newY, color, set);

			}
		}
	}

	/**
	 * 在删除的节点中,找到相邻的相同颜色的格子
	 * 
	 * @param x
	 * @param y
	 * @param color
	 * @param set
	 * @param cSet
	 */
	private void nearAdd(int x, int y, Color color, Set<String> set, Set<String> cSet) {
		if (!cSet.isEmpty()) {
			String nKey = this.getKey(x, y);
			cSet.remove(nKey);
			set.add(nKey);
			if (x - 1 > -1) {
				String key = this.getKey(x - 1, y);
				if (removeCellSet.contains(key) && !set.contains(key) && maps[x - 1][y].color == color) {
					this.nearAdd(x - 1, y, color, set, cSet);
				}
			}
			if (x + 1 < this.xSize) {
				String key = this.getKey(x + 1, y);
				if (removeCellSet.contains(key) && !set.contains(key) && maps[x + 1][y].color == color) {
					this.nearAdd(x + 1, y, color, set, cSet);
				}
			}
			if (y - 1 > -1) {
				String key = this.getKey(x, y - 1);
				if (removeCellSet.contains(key) && !set.contains(key) && maps[x][y - 1].color == color) {
					this.nearAdd(x, y - 1, color, set, cSet);
				}
			}
			if (y + 1 < this.ySize) {
				String key = this.getKey(x, y + 1);
				if (removeCellSet.contains(key) && !set.contains(key) && maps[x][y + 1].color == color) {
					this.nearAdd(x, y + 1, color, set, cSet);
				}
			}
		}
	}


	/**
	 * 移动 将source 移动至target
	 * 
	 * @param source
	 * @param target
	 * @throws Exception
	 */
	public List<RemoveScaleResult> move(Cell source, Cell target) {
		if (source != null && target != null) {
			if (this.near(source, target)) {
				Color targetColor = maps[target.X][target.Y].color;
				Color sourceColor = maps[source.X][source.Y].color;
				maps[source.X][source.Y].color = targetColor;
				maps[target.X][target.Y].color = sourceColor;
				if (!this.isLine(source.X, source.Y) && !this.isLine(target.X, target.Y)) {
					maps[source.X][source.Y].color = sourceColor;
					maps[target.X][target.Y].color = targetColor;
					throw new NoSpoilageException("这次移动没有可消除的格子");
				} else {
					removeScaleResultList = new ArrayList<RemoveScaleResult>();
					this.fadeCircle();
				}
			} else {
				throw new NearCellException("目标不在起点旁边");
			}
		} else {
			throw new NullPointerException("起点或者目标为空");
		}
		return removeScaleResultList;
	}

	/**
	 * 起点跟目标点是否相邻
	 * 
	 * @param source
	 * @param target
	 * @return
	 */
	private boolean near(Cell source, Cell target) {
		if (this.isInMap(source) && this.isInMap(target) && source.nearCell(target)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断该点是否超界
	 * 
	 * @param cell
	 * @return
	 */
	private boolean isInMap(Cell cell) {
		if (cell.X > -1 && cell.X < this.xSize && cell.Y > -1 && cell.Y < this.ySize) {
			return true;
		}
		return false;
	}

	/**
	 * 补图 随机添加格子
	 * 
	 * @return
	 */
	private Set<Cell> addCell(RemoveScaleResult result) {
		Set<Cell> addCellSet = this.getNonePoint();
		if (!addCellSet.isEmpty()) {
			this.addCell(addCellSet, result);
		}
		return addCellSet;
	}

	/**
	 * 补图
	 * 
	 * @param addCellSet
	 */

	private void addCell(Set<Cell> addCellSet, RemoveScaleResult result) {
		List<Cell> list = new ArrayList<Cell>();
		this.initLiveColor();
		for (Cell cell : addCellSet) {
			while (true) {
				if (!this.liveColorList.isEmpty()) {
					int tem = random.nextInt(liveColorList.size());
					String liveColor = liveColorList.get(tem);
					cell.setColor(Color.valueOf(liveColor));
					if (!this.isLineOnAddCell(cell, addCellSet)) {
						maps[cell.X][cell.Y] = cell;
						list.add(cell);
						break;
					} else {
						liveColorList.remove(liveColor);
					}
				} else {
					this.addCell(addCellSet, result);
					return;
				}
			}
		}
		
		if (this.isDieMap()) {
			this.addCell(addCellSet, result);
		} else {
			if (!list.isEmpty()) {
				result.setNewCellList(list);
			}
		}
	}

	
	/**
	 * 判断在补图要添加的给子中是否有3个连线
	 * 
	 * @param x
	 * @param y
	 * @param set
	 * @return
	 */
	private boolean isLineOnAddCell(Cell cell, Set<Cell> set) {
		int x=cell.X;
		int y=cell.Y;
		boolean lx1 = x - 1 > -1;
		boolean lx2 = x - 2 > -1;
		boolean bx1 = x + 1 < this.xSize;
		boolean bx2 = x + 2 < this.xSize;
		boolean ly1 = y - 1 > -1;
		boolean ly2 = y - 2 > -1;
		boolean by1 = y + 1 < this.ySize;
		boolean by2 = y + 2 < this.ySize;
		if (ly1 && by1) {
			if (isCellColorEqualInAddCell(maps[x][y - 1], cell, maps[x][y + 1], set)) {
				return true;
			}
		}
		if (lx1 && bx1) {
			if (isCellColorEqualInAddCell(maps[x - 1][y], cell, maps[x + 1][y], set)) {
				return true;
			}
		}
		if (ly2) {
			if (isCellColorEqualInAddCell(cell, maps[x][y - 1], maps[x][y - 2], set)) {
				return true;
			}
		}
		if (by2) {
			if (isCellColorEqualInAddCell(cell, maps[x][y + 1], maps[x][y + 2], set)) {
				return true;
			}
		}

		if (lx2) {
			if (isCellColorEqualInAddCell(cell, maps[x - 1][y], maps[x - 2][y], set)) {
				return true;
			}
		}
		if (bx2) {
			if (isCellColorEqualInAddCell(cell, maps[x + 1][y], maps[x + 2][y], set)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 3消
	 */
	private void fadeCircle() {
		removeCellSet = new HashSet<String>();
		RemoveScaleResult result = new RemoveScaleResult();
		List<List<Cell>> removeCellList = new ArrayList<List<Cell>>();
		// 判断选出要消除的格子
		this.createRemoveCell();
		// 给要消除的给子分块
		this.blockRemoveCell(removeCellList);
		// 消除格子,并且降落
		this.removeCellAndDown();
		if (!removeCellList.isEmpty()) {
			result.setRemoveCellList(removeCellList);
		}
		// 添加格子
		if (!removeCellSet.isEmpty()) {
			this.addCell(result);
			removeScaleResultList.add(result);
			// 添加格子后再消除格子
			this.fadeCircle();
		}
	}

	/**
	 * 生成要消掉的节点 同颜色同列或者同行超过3个的都要消掉
	 */
	private void createRemoveCell() {
		for (int i = 0; i < this.xSize; i++) {
			for (int j = 0; j < this.ySize; j++) {
				Cell source = maps[i][j];
				String cellKey = this.getKey(i, j);
				if (source != null && !removeCellSet.contains(cellKey)) {
					source.setX(i);
					source.setY(j);
					Set<String> rowSet = new HashSet<String>();
					Set<String> colSet = new HashSet<String>();
					this.isCellColorEqualLeft(i, j, source.color, rowSet);
					this.isCellColorEqualRight(i, j, source.color, rowSet);
					this.isCellColorEqualUp(i, j, source.color, colSet);
					this.isCellColorEqualDown(i, j, source.color, colSet);
					if (rowSet.size() > 2) {
						for (String key : rowSet) {
							removeCellSet.add(key);
						}
					}
					if (colSet.size() > 2) {
						for (String key : colSet) {
							removeCellSet.add(key);
						}
					}
				}
			}
		}
	}

	/**
	 * 给要消除的给子分区域
	 */
	private void blockRemoveCell(List<List<Cell>> removeCellList) {
		// 复制一份要消掉的格子的集合
		Set<String> cSet = new HashSet<String>(removeCellSet);
		for (String key : removeCellSet) {
			// 不在cSet里面的格子说明被归某一区域了,不需要在分区域了
			if (!cSet.isEmpty() && cSet.contains(key)) {
				String[] xy = key.split(BasePlay.LINK);
				int x = Integer.parseInt(xy[0]);
				int y = Integer.parseInt(xy[1]);
				Set<String> set = new HashSet<String>();
				// 为该格子相邻的格子迭代扩张,并从cSet中移除掉
				this.nearAdd(x, y, maps[x][y].color, set, cSet);
				if (!set.isEmpty()) {
					List<Cell> list = new ArrayList<Cell>();
					for (String key2 : set) {
						String[] xy2 = key2.split(BasePlay.LINK);
						int x2 = Integer.parseInt(xy2[0]);
						int y2 = Integer.parseInt(xy2[1]);
						maps[x2][y2].X = x2;
						maps[x2][y2].Y = y2;
						list.add(maps[x2][y2]);
					}
					// 对同属于同一区域的要消除的格子排序
					Collections.sort(list, new Comparator<Cell>() {
						@Override
						public int compare(Cell o1, Cell o2) {
							if (o1.Y == o2.Y) {
								return 0;
							} else if (o1.Y > o2.Y) {
								return -1;
							} else {
								return 1;
							}
						}
					});
					removeCellList.add(list);
				}
			}
		}
	}

	/**
	 * 消除要消除的格子跟并且地图格子下降
	 */
	private void removeCellAndDown() {
		Set<Integer> set = new HashSet<Integer>();
		for (String key : removeCellSet) {
			String[] xy = key.split(BasePlay.LINK);
			int x = Integer.parseInt(xy[0]);
			int y = Integer.parseInt(xy[1]);
			maps[x][y] = null;
			if (!set.contains(x)) {
				set.add(x);
			}
		}
		for (Integer x : set) {
			List<Cell> list = new ArrayList<Cell>();
			for (int j = this.ySize - 1; j > -1; j--) {
				Cell cell = maps[x][j];
				if (cell != null) {
					cell.setX(x);
					cell.setY(j);
					list.add(cell.clone());
					maps[x][j] = null;
				}
			}
			int j = this.ySize - 1;
			for (Cell cell : list) {
				cell.setX(x);
				maps[x][j] = cell;
				j--;
			}
		}
	}

	/**
	 * 获取空的节点
	 * 
	 * @return
	 */
	private Set<Cell> getNonePoint() {
		Set<Cell> set = new HashSet<Cell>();
		for (int i = 0; i < this.xSize; i++) {
			for (int j = 0; j < this.ySize; j++) {
				if (maps[i][j] == null) {
					Cell cell = new Cell();
					cell.setX(i);
					cell.setY(j);
					set.add(cell);
				}
			}
		}
		return set;
	}

	/**
	 * 是否为死图
	 * 
	 * @return
	 */
	private boolean isDieMap() {
		for (int i = 0; i < this.xSize; i++) {
			for (int j = 0; j < this.ySize; j++) {
				maps[i][j].X = i;
				maps[i][j].Y = j;
				if (isDie(i, j) == false) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 判断该格子是否为死格子
	 * 
	 * @param x
	 *            格子的x坐标
	 * @param y
	 *            格子的y坐标
	 * @return
	 */
	private boolean isDie(int x, int y) {
		boolean lx1 = x - 1 > -1;
		boolean lx2 = x - 2 > -1;
		boolean lx3 = x - 3 > -1;
		boolean bx1 = x + 1 < this.xSize;
		boolean bx2 = x + 2 < this.xSize;
		boolean bx3 = x + 3 < this.xSize;
		boolean ly1 = y - 1 > -1;
		boolean ly2 = y - 2 > -1;
		boolean ly3 = y - 3 > -1;
		boolean by1 = y + 1 < this.ySize;
		boolean by2 = y + 2 < this.ySize;
		boolean by3 = y + 3 < this.ySize;
		Color color = maps[x][y].color;
		if (bx1) {
			if (maps[x + 1][y].color == color) {
				if (bx3) {
					if (maps[x + 3][y].color == color) {
						return false;
					}
				}
				if (bx2 && by1) {
					if (maps[x + 2][y + 1].color == color) {
						return false;
					}
				}
				if (bx2 && ly1) {
					if (maps[x + 2][y - 1].color == color) {
						return false;
					}
				}
				if (lx2) {
					if (maps[x - 2][y].color == color) {
						return false;
					}
				}
				if (lx1 && ly1) {
					if (maps[x - 1][y - 1].color == color) {
						return false;
					}
				}
				if (lx1 && by1) {
					if (maps[x - 1][y + 1].color == color) {
						return false;
					}
				}
			}
			if (ly1 && by1) {
				if (maps[x + 1][y - 1].color == color && maps[x + 1][y + 1].color == color) {
					return false;
				}
			}
		}
		if (lx1) {
			if (maps[x - 1][y].color == color) {
				if (lx3) {
					if (maps[x - 3][y].color == color) {
						return false;
					}
				}
				if (lx2 && by1) {
					if (maps[x - 2][y + 1].color == color) {
						return false;
					}
				}
				if (lx2 && ly1) {
					if (maps[x - 2][y - 1].color == color) {
						return false;
					}
				}
				if (bx2) {
					if (maps[x + 2][y].color == color) {
						return false;
					}
				}
				if (bx1 && ly1) {
					if (maps[x + 1][y - 1].color == color) {
						return false;
					}
				}
				if (bx1 && by1) {
					if (maps[x + 1][y + 1].color == color) {
						return false;
					}
				}
			}
			if (ly1 && by1) {
				if (maps[x - 1][y - 1].color == color && maps[x - 1][y + 1].color == color) {
					return false;
				}
			}
		}
		if (by1) {
			if (maps[x][y + 1].color == color) {
				if (by3) {
					if (maps[x][y + 3].color == color) {
						return false;
					}
				}
				if (lx1 && by2) {
					if (maps[x - 1][y + 2].color == color) {
						return false;
					}
				}
				if (bx1 && by2) {
					if (maps[x + 1][y + 2].color == color) {
						return false;
					}
				}
				if (ly2) {
					if (maps[x][y - 2].color == color) {
						return false;
					}
				}
				if (bx1 && ly1) {
					if (maps[x + 1][y - 1].color == color) {
						return false;
					}
				}
				if (lx1 && ly1) {
					if (maps[x - 1][y - 1].color == color) {
						return false;
					}
				}
			}
			if (lx1 && bx1) {
				if (maps[x - 1][y + 1].color == color && maps[x + 1][y + 1].color == color) {
					return false;
				}
			}
		}
		if (ly1) {
			if (maps[x][y - 1].color == color) {
				if (ly3) {
					if (maps[x][y - 3].color == color) {
						return false;
					}
				}
				if (lx1 && ly2) {
					if (maps[x - 1][y - 2].color == color) {
						return false;
					}
				}
				if (bx1 && ly2) {
					if (maps[x + 1][y - 2].color == color) {
						return false;
					}
				}
				if (by2) {
					if (maps[x][y + 2].color == color) {
						return false;
					}
				}
				if (bx1 && by1) {
					if (maps[x + 1][y + 1].color == color) {
						return false;
					}
				}
				if (lx1 && by1) {
					if (maps[x - 1][y + 1].color == color) {
						return false;
					}
				}
			}
			if (lx1 && bx1) {
				if (maps[x - 1][y - 1].color == color && maps[x + 1][y - 1].color == color) {
					return false;
				}
			}
		}
		return true;
	}

	public Cell[][] getMaps() {
		return maps;
	}

	public void setMaps(Cell[][] maps) {
		this.maps = maps;
	}

	public int getxSize() {
		return xSize;
	}

	public void setxSize(int xSize) {
		this.xSize = xSize;
	}

	public int getySize() {
		return ySize;
	}

	public void setySize(int ySize) {
		this.ySize = ySize;
	}

}

package com.eyugame.tade.module.glops.model;

import com.eyugame.tade.module.glops.constant.Color;

/**
 * 单元格
 * 
 * @author k60
 */
public class Cell {

	/**
	 * x坐标
	 */
	public int X;

	/**
	 * y坐标
	 */
	public int Y;

	/**
	 * 颜色
	 */
	public Color color;

	public Cell() {
		super();
	}

	public Cell(int x, int y) {
		super();
		X = x;
		Y = y;
	}

	public int getX() {
		return X;
	}

	public void setX(int x) {
		X = x;
	}

	public int getY() {
		return Y;
	}

	public void setY(int y) {
		Y = y;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public boolean nearCell(Cell cell) {
		if (cell != null) {
			if (this.X == cell.X && this.Y == (cell.Y + 1)) {
				return true;
			} else if (this.X == cell.X && this.Y == (cell.Y - 1)) {
				return true;
			} else if (this.X == (cell.X + 1) && this.Y == cell.Y) {
				return true;
			} else if (this.X == (cell.X - 1) && this.Y == cell.Y) {
				return true;
			}
		}
		return false;
	}
    
	@Override
	public String toString() {
		return this.X+"_"+this.Y+":"+this.color;
				
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + X;
		result = prime * result + Y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Cell other = (Cell) obj;
		if (X != other.X)
			return false;
		if (Y != other.Y)
			return false;
		return true;
	}
	
	public Cell clone(){
		Cell cell=new Cell();
		cell.setX(this.X);
		cell.setY(this.Y);
		cell.setColor(this.color);
		return cell;
	}
	
	
}

package com.eyugame.tade.module.glops.model;

import java.util.List;

/** 
 * 消除刻度结果
 *
 * @author  k60
 */
public class RemoveScaleResult {

	/**
	 * 消除的单元格
	 */
	private List<List<Cell>> removeCellList;
	
	/**
	 * 新产生的单元格颜色列表
	 * 
	 * 产生规则:
	 * X轴,由左至右补
	 * Y轴,由下至上补
	 */
	private List<Cell> newCellList;
	

	public List<List<Cell>> getRemoveCellList() {
		return removeCellList;
	}

	public void setRemoveCellList(List<List<Cell>> removeCellList) {
		this.removeCellList = removeCellList;
	}

	public List<Cell> getNewCellList() {
		return newCellList;
	}

	public void setNewCellList(List<Cell> newCellList) {
		this.newCellList = newCellList;
	}


}

package com.eyugame.tade.module.glops.exception;
/**
 * 
 * 当起点向目标移动,目标跟起点不是相邻时异常
 *
 */
public class NearCellException extends RuntimeException {

	private static final long serialVersionUID = -5973332015600566849L;

	public NearCellException(String message){
		super(message);
	}
	
}

package com.eyugame.tade.module.glops.exception;
/**
 * 
 * 当起点向目标移动,但是不能3消异常
 *
 */
public class NoSpoilageException extends RuntimeException {
	
	private static final long serialVersionUID = 3129338536664414593L;
	
	public NoSpoilageException(String message) {
		super(message);
	}
}


  • 29
    点赞
  • 138
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
### 回答1: 游戏是一种益智休闲游戏,玩家需要通过消除相同图案的方块来完成关卡目标。在游戏中,玩家会看到一个盛满了方块的游戏界面,在这些方块中,可能会有不同的图案,例如水果、宝石或其他物品。 玩家需要通过交换相邻的方块,使得至少个相同的方块连在一起。当这些方块连成一条直线或者横排时,它们就会消除,并在消除的同时获得分数。消除方块后,上面的方块会下落填补空缺,同时可能会形成新的连线和消除。玩家需要在有限的步数或时间内,消除特定数量的方块或达到特定得分才能过关。 箱子是游戏中的一种特殊元素。一些方块上可能会有箱子,这些箱子会被锁住,玩家需要通过消除它们周围的方块来解锁。当玩家成功地将所有箱子解锁并消除时,就可以通过关卡。箱子在游戏中起着重要的作用,因为它们常常会阻挡其他连线的形成,需要玩家巧妙地利用消除方块的顺序和方向,来解锁和消除这些箱子,从而达到过关的目标。 Unity是一种强大的游戏引擎,许多游戏也是使用Unity开发的。Unity提供了丰富的开发工具和资源,使得开发者能够轻松创建精美的游戏画面和流畅的游戏体验。借助Unity的强大功能,开发者可以有效地设计和调试游戏中的各种元素和特效,从而让玩家获得更好的游戏体验。 总之,消箱子游戏是一种有趣的休闲游戏,玩家需要消除相同的方块来解锁箱子并完成关卡。Unity作为游戏开发引擎,为开发者提供了强大的开发工具和资源,使他们能够创建精美且流畅的游戏体验。 ### 回答2: 游戏是一种益智类游戏,玩家需要通过消除游戏画面中的方块或者球体来获得高分。通常情况下,玩家需要在有限的步数内达到特定目标,如消除特定数量的方块或者获得足够的分数。 而"箱子"是游戏中的一种常见元素,通常以方块的形式呈现。玩家需要通过移动箱子的位置,使相同颜色的箱子排列在一起来达到消除的效果。当一行或一列的箱子成为相同颜色的连续序列时,这些箱子就会被消除,玩家就可以获得分数。 Unity是一种广泛使用的游戏引擎,可以帮助开发者创建游戏。通过Unity的工具和功能,开发者可以轻松地设计游戏的界面、操作方式和游戏规则,并且可以实现一些特殊的特效效果,提升游戏的可玩性和视觉效果。 在Unity中,开发者可以使用脚本编写游戏逻辑,包括箱子的生成、移动、消除和得分等功能。同时,Unity还提供了一些内置的物理引擎和碰撞检测机制,可以使箱子的移动和消除更加真实和流畅。 总之,消箱子游戏是一种益智类游戏,玩家需要通过移动和排列箱子来消除。Unity是一种强大的游戏引擎,可以帮助开发者轻松地创建和设计消箱子游戏,提供丰富的工具和功能,实现游戏的逻辑、物理效果和可视化效果。 ### 回答3: 消是一种流行的益智类游戏,是指通过消除方块或物品来达到游戏目标的一种玩法。而箱子则是一种游戏中常见的元素,可以用来堆叠、移动或隐藏其他物品。 《消 箱子 Unity》是基于Unity引擎开发的一款具有消和箱子元素的游戏游戏的核心目标是通过消除特定的方块或物品,解开不同关卡的谜题和难题。 在游戏中,玩家需要以逻辑和策略来规划自己的移动和消除,通过交换箱子的位置来实现相同颜色或形状的方块的匹配。一旦匹配的方块或物品达到一定数量,它们将会消失并获得分数。 游戏的关卡设置多样化,会有不同的限制和规则。有些关卡需要在有限的步数内完成目标,有些则需要在规定的时间内尽快消除方块。同时,游戏可能还添加了特殊道具或障碍物,增加了游戏的挑战性和乐趣。 《消 箱子 Unity》在视觉效果和音效上通常会进行精心设计,使整个游戏过程更加美观和令人愉悦。游戏还可能提供多个游戏模式和难度选择,以满足不同玩家的需求和挑战。 总结来说,《消 箱子 Unity》是一款有趣而富有挑战性的游戏,玩家需要通过消除方块或物品,解开谜题和难题。这款游戏通过使用Unity引擎并结合消和箱子元素,为玩家带来了独特的游戏体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值