在上一篇《我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)》中提到的两个类:
- GameConf:负责管理游戏的初始化设置信息。
- GameService:负责游戏的逻辑实现。
其中GameConf的代码如下:cn\oyp\link\utils\GameConf.java
而GameService则是整个游戏逻辑实现的核心,而且GameService是一个可以复用的业务逻辑类,它于游戏平台无关,既可以在Java Swing中使用,也可以在Android游戏中使用,甚至只要稍作修改,GameService也可以移植到C#平台的连连看游戏中。
考虑到程序的可扩展行,先给GameService组件定义一个接口,代码如下:cn\oyp\link\board\GameService.java
- package cn.oyp.link.board;
-
- import cn.oyp.link.utils.LinkInfo;
- import cn.oyp.link.view.Piece;
-
-
-
-
-
-
- public interface GameService {
-
-
-
- public void start();
-
-
-
-
-
-
- public Piece[][] getPieces();
-
-
-
-
-
-
- public boolean hasPieces();
-
-
-
-
-
-
-
-
-
-
- public Piece findPiece(float touchX, float touchY);
-
-
-
-
-
-
-
-
-
-
- public LinkInfo link(Piece p1, Piece p2);
- }
下面来具体实现GameService组件,首先的public void start()方法,public Piece[][] getPieces()方法和public boolean hasPieces()方法很容易实现,具体实现如下:
cn\oyp\link\
board\impl\GameServiceImpl.java
-
-
-
-
-
- public class GameServiceImpl implements GameService {
-
-
-
- private Piece[][] pieces;
-
-
-
- private GameConf config;
-
-
-
-
-
-
-
- public GameServiceImpl(GameConf config) {
-
- this.config = config;
- }
-
- @Override
- public void start() {
-
- AbstractBoard board = null;
- Random random = new Random();
-
- int index = random.nextInt(4);
-
- switch (index) {
- case 0:
-
- board = new VerticalBoard();
- break;
- case 1:
-
- board = new HorizontalBoard();
- break;
- default:
-
- board = new FullBoard();
- break;
- }
-
- this.pieces = board.create(config);
- }
-
- @Override
- public Piece[][] getPieces() {
- return this.pieces;
- }
-
- @Override
- public boolean hasPieces() {
-
- for (int i = 0; i < pieces.length; i++) {
- for (int j = 0; j < pieces[i].length; j++) {
-
- if (pieces[i][j] != null) {
- return true;
- }
- }
- }
- return false;
- }
- ...
- }
1、获取触碰点的方块
首先当用户碰触游戏界面时,事件监听器获取的是该触碰到在游戏界面上的X、Y坐标,但是程序需要的是获取用户碰触的到底是那个方块,因此程序必须把界面上的X、Y坐标换算成在Piece[][]二维数组中的两个索引值。考虑到游戏界面上每个方块的高度和宽度都是相同的,因此想要将界面上的X、Y坐标换算成Piece[][]二维数组中的索引也比较简单,只要拿X、Y坐标值除以图片的宽、高即可。下面是根据触点X、Y坐标获取对于方块的代码:
-
-
-
- @Override
- public Piece findPiece(float touchX, float touchY) {
-
-
-
-
- int relativeX = (int) touchX - this.config.getBeginImageX();
- int relativeY = (int) touchY - this.config.getBeginImageY();
-
-
-
- if (relativeX < 0 || relativeY < 0) {
- return null;
- }
-
-
-
- int indexX = getIndex(relativeX, GameConf.PIECE_WIDTH);
-
-
-
- int indexY = getIndex(relativeY, GameConf.PIECE_HEIGHT);
-
- if (indexX < 0 || indexY < 0) {
- return null;
- }
-
- if (indexX >= this.config.getXSize()
- || indexY >= this.config.getYSize()) {
- return null;
- }
-
- return this.pieces[indexX][indexY];
- }
上面的方法调用了getIndex(int relative,int size)方法,该方法的实现就是拿relative除以size,程序需要判断可以整除和不能整除两种情况:如果可以整除,说明还在前一个方块内;如果不能整除,则对于于下一个方块,下面是
getIndex(int relative,int size)方法的代码:
-
-
-
-
-
-
-
-
-
- private int getIndex(int relative, int size) {
-
- int index = -1;
-
-
-
-
- if (relative % size == 0) {
- index = relative / size - 1;
- } else {
-
-
-
- index = relative / size;
- }
- return index;
- }
2、判断两个方块是否可以相连
两个方块可以相连的情况可以大致分为以下几种:
- 两个方块位于同一条水平线,可以直接相连。
- 两个方块位于同一条竖直线,可以直接相连。
- 两个方块以两条线段相连,也就是有1个拐角。
- 两个方块以三条线段相连,也就是有2个拐角。
下面的link(Piece p1, Piece p2)方法把这四种情况分开进行处理,代码如下:
- @Override
- public LinkInfo link(Piece p1, Piece p2) {
-
- if (p1.equals(p2))
- return null;
-
- if (!p1.isSameImage(p2))
- return null;
-
- if (p2.getIndexX() < p1.getIndexX())
- return link(p2, p1);
-
- Point p1Point = p1.getCenter();
-
- Point p2Point = p2.getCenter();
-
- if (p1.getIndexY() == p2.getIndexY()) {
-
- if (!isXBlock(p1Point, p2Point, GameConf.PIECE_WIDTH)) {
-
- return new LinkInfo(p1Point, p2Point);
- }
- }
-
- if (p1.getIndexX() == p2.getIndexX()) {
- if (!isYBlock(p1Point, p2Point, GameConf.PIECE_HEIGHT)) {
-
- return new LinkInfo(p1Point, p2Point);
- }
- }
-
-
-
- Point cornerPoint = getCornerPoint(p1Point, p2Point,
- GameConf.PIECE_WIDTH, GameConf.PIECE_HEIGHT);
-
- if (cornerPoint != null) {
- return new LinkInfo(p1Point, cornerPoint, p2Point);
- }
-
-
-
-
- Map<Point, Point> turns = getLinkPoints(p1Point, p2Point,
- GameConf.PIECE_WIDTH, GameConf.PIECE_WIDTH);
-
- if (turns.size() != 0) {
-
- return getShortcut(p1Point, p2Point, turns,
- getDistance(p1Point, p2Point));
- }
- return null;
- }
3、定义获取通道的方法
所谓通道,指的是一个方块上、下、左、右四个方向上的空白方块,如下图所示:
下面是获取某个坐标点四周通道的四个方法:
-
-
-
-
-
-
-
-
-
-
- private List<Point> getLeftChanel(Point p, int min, int pieceWidth) {
- List<Point> result = new ArrayList<Point>();
-
- for (int i = p.x - pieceWidth; i >= min; i = i - pieceWidth) {
-
- if (hasPiece(i, p.y)) {
- return result;
- }
- result.add(new Point(i, p.y));
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- private List<Point> getRightChanel(Point p, int max, int pieceWidth) {
- List<Point> result = new ArrayList<Point>();
-
- for (int i = p.x + pieceWidth; i <= max; i = i + pieceWidth) {
-
- if (hasPiece(i, p.y)) {
- return result;
- }
- result.add(new Point(i, p.y));
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- private List<Point> getUpChanel(Point p, int min, int pieceHeight) {
- List<Point> result = new ArrayList<Point>();
-
- for (int i = p.y - pieceHeight; i >= min; i = i - pieceHeight) {
-
- if (hasPiece(p.x, i)) {
-
- return result;
- }
- result.add(new Point(p.x, i));
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
- private List<Point> getDownChanel(Point p, int max, int pieceHeight) {
- List<Point> result = new ArrayList<Point>();
-
- for (int i = p.y + pieceHeight; i <= max; i = i + pieceHeight) {
-
- if (hasPiece(p.x, i)) {
-
- return result;
- }
- result.add(new Point(p.x, i));
- }
- return result;
- }
4、没有转折点的横向连接
如果两个Piece对象在Piece[][]数组中的第二维索引值相等,那么这两个Piece就在同一行,这时候需要判断两个Piece直接是否有障碍,调用isXBlock(Point p1,Point p2,int pieceWidth)方法,代码如下:
-
-
-
-
-
-
-
-
-
- private boolean isXBlock(Point p1, Point p2, int pieceWidth) {
- if (p2.x < p1.x) {
-
- return isXBlock(p2, p1, pieceWidth);
- }
- for (int i = p1.x + pieceWidth; i < p2.x; i = i + pieceWidth) {
- if (hasPiece(i, p1.y)) {
- return true;
- }
- }
- return false;
- }
如果两个方块位于同一行,且它们之间没有障碍,那么这两个方块就可以消除,两个方块的连接信息就是它们的中心。
5、没有转折点的纵向连接
如果两个Piece对象在Piece[][]数组中的第一维索引值相等,那么这两个Piece就在同一列,这时候需要判断两个Piece直接是否有障碍,调用isYBlock(Point p1,Point p2,int pieceWidth)方法,代码如下:
-
-
-
-
-
-
-
-
-
- private boolean isYBlock(Point p1, Point p2, int pieceHeight) {
- if (p2.y < p1.y) {
-
- return isYBlock(p2, p1, pieceHeight);
- }
- for (int i = p1.y + pieceHeight; i < p2.y; i = i + pieceHeight) {
- if (hasPiece(p1.x, i)) {
-
- return true;
- }
- }
- return false;
- }
如果两个方块位于同一列,且它们之间没有障碍,那么这两个方块就可以消除,两个方块的连接信息就是它们的中心。
6、一个转折点的连接
对于两个方块连接线上只有一个转折点的情况,程序需要先找到这个转折点。为了找到这个转折点,程序定义了一个遍历两个通道并获取它们交点的方法,getWrapPoint(List<Point> p1Chanel, List<Point> p2Chanel),代码如下:
-
-
-
-
-
-
-
-
-
- private Point getWrapPoint(List<Point> p1Chanel, List<Point> p2Chanel) {
- for (int i = 0; i < p1Chanel.size(); i++) {
- Point temp1 = p1Chanel.get(i);
- for (int j = 0; j < p2Chanel.size(); j++) {
- Point temp2 = p2Chanel.get(j);
- if (temp1.equals(temp2)) {
-
- return temp1;
- }
- }
- }
- return null;
- }
为了找出两个方块连接线上的连接点,程序需要分析p1和p2的位置分布。所以我们可以分析p2要么在p1的右上角,要么在p1的右下角。至于p2位于p1的左上角和左下角的情况,只要将p1、p2交换即可,如下图所示:
- 当p2位于p1右上角时候,应该计算p1的右通道和p2的下通道是否有交点,p1的上通道和p2的左通道是否有交点。
- 当p2位于p1右下角时候,应该计算p1的右通道和p2的上通道是否有交点,p1的下通道和p2的左通道是否有交点。
下面是具体是实现方法getCornerPoint(Point point1, Point point2, int pieceWidth,
int pieceHeight)的代码:
-
-
-
-
-
-
-
-
-
- private Point getCornerPoint(Point point1, Point point2, int pieceWidth,
- int pieceHeight) {
-
- if (isLeftUp(point1, point2) || isLeftDown(point1, point2)) {
-
- return getCornerPoint(point2, point1, pieceWidth, pieceHeight);
- }
-
- List<Point> point1RightChanel = getRightChanel(point1, point2.x,
- pieceWidth);
-
- List<Point> point1UpChanel = getUpChanel(point1, point2.y, pieceHeight);
-
- List<Point> point1DownChanel = getDownChanel(point1, point2.y,
- pieceHeight);
-
- List<Point> point2DownChanel = getDownChanel(point2, point1.y,
- pieceHeight);
-
- List<Point> point2LeftChanel = getLeftChanel(point2, point1.x,
- pieceWidth);
-
- List<Point> point2UpChanel = getUpChanel(point2, point1.y, pieceHeight);
-
- if (isRightUp(point1, point2)) {
-
- Point linkPoint1 = getWrapPoint(point1RightChanel, point2DownChanel);
-
- Point linkPoint2 = getWrapPoint(point1UpChanel, point2LeftChanel);
-
- return (linkPoint1 == null) ? linkPoint2 : linkPoint1;
- }
-
-
- if (isRightDown(point1, point2)) {
-
-
- Point linkPoint1 = getWrapPoint(point1DownChanel, point2LeftChanel);
-
- Point linkPoint2 = getWrapPoint(point1RightChanel, point2UpChanel);
- return (linkPoint1 == null) ? linkPoint2 : linkPoint1;
- }
- return null;
- }
上面方法调用了以下四个方法:
-
-
-
-
-
-
-
- private boolean isLeftUp(Point point1, Point point2) {
- return (point2.x < point1.x && point2.y < point1.y);
- }
-
-
-
-
-
-
-
-
- private boolean isLeftDown(Point point1, Point point2) {
- return (point2.x < point1.x && point2.y > point1.y);
- }
-
-
-
-
-
-
-
-
- private boolean isRightUp(Point point1, Point point2) {
- return (point2.x > point1.x && point2.y < point1.y);
- }
-
-
-
-
-
-
-
-
- private boolean isRightDown(Point point1, Point point2) {
- return (point2.x > point1.x && point2.y > point1.y);
- }
7、两个转折点的连接
两个转折点可以分为以下几种情况讨论:
- p1、p2位于同一行,不能直接相连,就必须有两个转折点,分向上和向下两种连接情况。
- p1、p2位于同一行,不能直接相连,就必须有两个转折点,分向左和向右两种连接情况。
- p2在p1的右下角,有6中转折情况。
- p2在p1的右上角,也有6种转折情况。
至于p2位于p1的左上角和左下角的情况,只要将p1、p2交换即可。
1)、p1、p2位于同一行,不能直接相连,就必须有两个转折点,如下图所示
当p1与p2位于同一行不能直接相连,这两个点既可以在上面相连,也可以在下面相连,这两种情况都代表他们可以相连,先把这两种情况加入到结果中,最后去计算最近的距离。
实现时先构建一个Map,Map的key为第一个转折点,Map的value为第二个转折点,如果Map的size()大于1,说明这两个Point有多种连接途径,那么程序还需要计算路径最小的连接方式。
2)p1、p2位于同一行,不能直接相连,就必须有两个转折点,如上图所示。
当p1与p2位于同一列不能直接相连,这两个点既可以在左边相连,也可以在右边相连,这两种情况都代表他们可以相连,先把这两种情况加入到结果中,最后去计算最近的距离。
实现时先构建一个Map,Map的key为第一个转折点,Map的value为第二个转折点,如果Map的size()大于1,说明这两个Point有多种连接途径,那么程序还需要计算路径最小的连接方式。
3)p2位于p1右下角的六种转折情况,如下图所示:
定义一个方法来处理上面具有两个连接点的情况,getLinkPoints(Point point1, Point point2,
int pieceWidth, int pieceHeight),代码如下所示:
-
-
-
-
-
-
-
- private Map<Point, Point> getLinkPoints(Point point1, Point point2,
- int pieceWidth, int pieceHeight) {
- Map<Point, Point> result = new HashMap<Point, Point>();
-
-
- List<Point> p1UpChanel = getUpChanel(point1, point2.y, pieceHeight);
-
- List<Point> p1RightChanel = getRightChanel(point1, point2.x, pieceWidth);
-
- List<Point> p1DownChanel = getDownChanel(point1, point2.y, pieceHeight);
-
- List<Point> p2DownChanel = getDownChanel(point2, point1.y, pieceHeight);
-
- List<Point> p2LeftChanel = getLeftChanel(point2, point1.x, pieceWidth);
-
- List<Point> p2UpChanel = getUpChanel(point2, point1.y, pieceHeight);
-
-
- int heightMax = (this.config.getYSize() + 1) * pieceHeight
- + this.config.getBeginImageY();
-
- int widthMax = (this.config.getXSize() + 1) * pieceWidth
- + this.config.getBeginImageX();
-
-
-
- if (isLeftUp(point1, point2) || isLeftDown(point1, point2)) {
-
- return getLinkPoints(point2, point1, pieceWidth, pieceHeight);
- }
-
- if (point1.y == point2.y) {
-
-
- p1UpChanel = getUpChanel(point1, 0, pieceHeight);
-
- p2UpChanel = getUpChanel(point2, 0, pieceHeight);
-
- Map<Point, Point> upLinkPoints = getXLinkPoints(p1UpChanel,
- p2UpChanel, pieceHeight);
-
-
-
- p1DownChanel = getDownChanel(point1, heightMax, pieceHeight);
-
- p2DownChanel = getDownChanel(point2, heightMax, pieceHeight);
-
- Map<Point, Point> downLinkPoints = getXLinkPoints(p1DownChanel,
- p2DownChanel, pieceHeight);
- result.putAll(upLinkPoints);
- result.putAll(downLinkPoints);
- }
-
- if (point1.x == point2.x) {
-
-
- List<Point> p1LeftChanel = getLeftChanel(point1, 0, pieceWidth);
-
- p2LeftChanel = getLeftChanel(point2, 0, pieceWidth);
-
- Map<Point, Point> leftLinkPoints = getYLinkPoints(p1LeftChanel,
- p2LeftChanel, pieceWidth);
-
-
-
- p1RightChanel = getRightChanel(point1, widthMax, pieceWidth);
-
- List<Point> p2RightChanel = getRightChanel(point2, widthMax,
- pieceWidth);
-
- Map<Point, Point> rightLinkPoints = getYLinkPoints(p1RightChanel,
- p2RightChanel, pieceWidth);
- result.putAll(leftLinkPoints);
- result.putAll(rightLinkPoints);
- }
-
- if (isRightUp(point1, point2)) {
-
- Map<Point, Point> upDownLinkPoints = getXLinkPoints(p1UpChanel,
- p2DownChanel, pieceWidth);
-
-
- Map<Point, Point> rightLeftLinkPoints = getYLinkPoints(
- p1RightChanel, p2LeftChanel, pieceHeight);
-
-
- p1UpChanel = getUpChanel(point1, 0, pieceHeight);
-
- p2UpChanel = getUpChanel(point2, 0, pieceHeight);
-
- Map<Point, Point> upUpLinkPoints = getXLinkPoints(p1UpChanel,
- p2UpChanel, pieceWidth);
-
-
- p1DownChanel = getDownChanel(point1, heightMax, pieceHeight);
-
- p2DownChanel = getDownChanel(point2, heightMax, pieceHeight);
-
- Map<Point, Point> downDownLinkPoints = getXLinkPoints(p1DownChanel,
- p2DownChanel, pieceWidth);
-
-
- p1RightChanel = getRightChanel(point1, widthMax, pieceWidth);
-
- List<Point> p2RightChanel = getRightChanel(point2, widthMax,
- pieceWidth);
-
- Map<Point, Point> rightRightLinkPoints = getYLinkPoints(
- p1RightChanel, p2RightChanel, pieceHeight);
-
-
- List<Point> p1LeftChanel = getLeftChanel(point1, 0, pieceWidth);
-
- p2LeftChanel = getLeftChanel(point2, 0, pieceWidth);
-
- Map<Point, Point> leftLeftLinkPoints = getYLinkPoints(p1LeftChanel,
- p2LeftChanel, pieceHeight);
-
- result.putAll(upDownLinkPoints);
- result.putAll(rightLeftLinkPoints);
- result.putAll(upUpLinkPoints);
- result.putAll(downDownLinkPoints);
- result.putAll(rightRightLinkPoints);
- result.putAll(leftLeftLinkPoints);
- }
-
- if (isRightDown(point1, point2)) {
-
- Map<Point, Point> downUpLinkPoints = getXLinkPoints(p1DownChanel,
- p2UpChanel, pieceWidth);
-
-
- Map<Point, Point> rightLeftLinkPoints = getYLinkPoints(
- p1RightChanel, p2LeftChanel, pieceHeight);
-
-
- p1UpChanel = getUpChanel(point1, 0, pieceHeight);
-
- p2UpChanel = getUpChanel(point2, 0, pieceHeight);
-
- Map<Point, Point> upUpLinkPoints = getXLinkPoints(p1UpChanel,
- p2UpChanel, pieceWidth);
-
-
- p1DownChanel = getDownChanel(point1, heightMax, pieceHeight);
-
- p2DownChanel = getDownChanel(point2, heightMax, pieceHeight);
-
- Map<Point, Point> downDownLinkPoints = getXLinkPoints(p1DownChanel,
- p2DownChanel, pieceWidth);
-
-
- List<Point> p1LeftChanel = getLeftChanel(point1, 0, pieceWidth);
-
- p2LeftChanel = getLeftChanel(point2, 0, pieceWidth);
-
- Map<Point, Point> leftLeftLinkPoints = getYLinkPoints(p1LeftChanel,
- p2LeftChanel, pieceHeight);
-
-
- p1RightChanel = getRightChanel(point1, widthMax, pieceWidth);
-
- List<Point> p2RightChanel = getRightChanel(point2, widthMax,
- pieceWidth);
-
- Map<Point, Point> rightRightLinkPoints = getYLinkPoints(
- p1RightChanel, p2RightChanel, pieceHeight);
-
- result.putAll(downUpLinkPoints);
- result.putAll(rightLeftLinkPoints);
- result.putAll(upUpLinkPoints);
- result.putAll(downDownLinkPoints);
- result.putAll(leftLeftLinkPoints);
- result.putAll(rightRightLinkPoints);
- }
- return result;
- }
上面调用的getXLinkPoints、getYLinkPoints方法代码如下:
-
-
-
-
-
-
-
-
-
- private Map<Point, Point> getYLinkPoints(List<Point> p1Chanel,
- List<Point> p2Chanel, int pieceHeight) {
- Map<Point, Point> result = new HashMap<Point, Point>();
- for (int i = 0; i < p1Chanel.size(); i++) {
- Point temp1 = p1Chanel.get(i);
- for (int j = 0; j < p2Chanel.size(); j++) {
- Point temp2 = p2Chanel.get(j);
-
- if (temp1.x == temp2.x) {
-
- if (!isYBlock(temp1, temp2, pieceHeight)) {
- result.put(temp1, temp2);
- }
- }
- }
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- private Map<Point, Point> getXLinkPoints(List<Point> p1Chanel,
- List<Point> p2Chanel, int pieceWidth) {
- Map<Point, Point> result = new HashMap<Point, Point>();
- for (int i = 0; i < p1Chanel.size(); i++) {
-
- Point temp1 = p1Chanel.get(i);
-
- for (int j = 0; j < p2Chanel.size(); j++) {
- Point temp2 = p2Chanel.get(j);
-
- if (temp1.y == temp2.y) {
- if (!isXBlock(temp1, temp2, pieceWidth)) {
-
- result.put(temp1, temp2);
- }
- }
- }
- }
- return result;
- }
8、找出最短距离
为了找出所有连接情况中的最短路径,程序可以分为以下2步骤来实现:
- 遍历转折点Map中的所有key-value对,与原来选择的两个点构成一个LinkInfo。每个LinkInfo代表一条完整的连接路径,并将这些LinkInfo搜集成一个List集合。
- 遍历第一步得到的List<LinkInfo>集合,计算每个LinkInfo中连接全部连接点的总距离,选与最短距离相差最小的LinkInfo返回。
-
-
-
-
-
-
-
-
-
-
-
- private LinkInfo getShortcut(Point p1, Point p2, Map<Point, Point> turns,
- int shortDistance) {
- List<LinkInfo> infos = new ArrayList<LinkInfo>();
-
- for (Point point1 : turns.keySet()) {
- Point point2 = turns.get(point1);
-
- infos.add(new LinkInfo(p1, point1, point2, p2));
- }
- return getShortcut(infos, shortDistance);
- }
-
-
-
-
-
-
-
- private LinkInfo getShortcut(List<LinkInfo> infos, int shortDistance) {
- int temp1 = 0;
- LinkInfo result = null;
- for (int i = 0; i < infos.size(); i++) {
- LinkInfo info = infos.get(i);
-
- int distance = countAll(info.getLinkPoints());
-
- if (i == 0) {
- temp1 = distance - shortDistance;
- result = info;
- }
-
- if (distance - shortDistance < temp1) {
- temp1 = distance - shortDistance;
- result = info;
- }
- }
- return result;
- }
-
-
-
-
-
-
-
- private int countAll(List<Point> points) {
- int result = 0;
- for (int i = 0; i < points.size() - 1; i++) {
-
- Point point1 = points.get(i);
-
- Point point2 = points.get(i + 1);
-
- result += getDistance(point1, point2);
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- private int getDistance(Point p1, Point p2) {
- int xDistance = Math.abs(p1.x - p2.x);
- int yDistance = Math.abs(p1.y - p2.y);
- return xDistance + yDistance;
- }