package org.crazyit.link.board.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.crazyit.link.board.AbstractBoard;
import org.crazyit.link.board.GameService;
import org.crazyit.link.object.GameConf;
import org.crazyit.link.object.LinkInfo;
import org.crazyit.link.view.Piece;
import android.graphics.Point;
/**
* Description: 游戏逻辑的实现类 <br/>
* site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
* Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
*
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class GameServiceImpl implements GameService
{
// 定义一个Piece[][]数组,只提供getter方法
private Piece[][] pieces;
// 游戏配置对象
private GameConf config;
public GameServiceImpl(GameConf config)
{
// 将游戏的配置对象设置本类中
this.config = config;
}
@Override
public void start()
{
// 定义一个AbstractBoard对象
AbstractBoard board = null;
Random random = new Random();
// 获取一个随机数, 可取值0、1、2、3四值。
int index = random.nextInt(4);
// 随机生成AbstractBoard的子类实例
switch (index)
{
case 0:
// 0返回VerticalBoard(竖向)
board = new VerticalBoard();
break;
case 1:
// 1返回HorizontalBoard(横向)
board = new HorizontalBoard();
break;
default:
// 默认返回FullBoard
board = new FullBoard();
break;
}
// 初始化Piece[][]数组
this.pieces = board.create(config);
}
// 直接返回本对象的Piece[][]数组
@Override
public Piece[][] getPieces()
{
return this.pieces;
}
// 实现接口的hasPieces方法
@Override
public boolean hasPieces()
{
// 遍历Piece[][]数组的每个元素
for (int i = 0; i < pieces.length; i++)
{
for (int j = 0; j < pieces[i].length; j++)
{
// 只要任意一个数组元素不为null,也就是还剩有非空的Piece对象
if (pieces[i][j] != null)
{
return true;
}
}
}
return false;
}
// 根据触碰点的位置查找相应的方块
@Override
public Piece findPiece(float touchX, float touchY)
{
// 由于在创建Piece对象的时候, 将每个Piece的开始座标加了
// GameConf中设置的beginImageX/beginImageY值, 因此这里要减去这个值
int relativeX = (int) touchX - this.config.getBeginImageX();
int relativeY = (int) touchY - this.config.getBeginImageY();
// 如果鼠标点击的地方比board中第一张图片的开始x座标和开始y座标要小, 即没有找到相应的方块
if (relativeX < 0 || relativeY < 0)
{
return null;
}
// 获取relativeX座标在Piece[][]数组中的第一维的索引值
// 第二个参数为每张图片的宽
int indexX = getIndex(relativeX, GameConf.PIECE_WIDTH);
// 获取relativeY座标在Piece[][]数组中的第二维的索引值
// 第二个参数为每张图片的高
int indexY = getIndex(relativeY, GameConf.PIECE_HEIGHT);
// 这两个索引比数组的最小索引还小, 返回null
if (indexX < 0 || indexY < 0)
{
return null;
}
// 这两个索引比数组的最大索引还大(或者等于), 返回null
if (indexX >= this.config.getXSize()
|| indexY >= this.config.getYSize())
{
return null;
}
// 返回Piece[][]数组的指定元素
return this.pieces[indexX][indexY];
}
// 工具方法, 根据relative座标计算相对于Piece[][]数组的第一维
// 或第二维的索引值 ,size为每张图片边的长或者宽
private int getIndex(int relative, int size)
{
// 表示座标relative不在该数组中
int index = -1;
// 让座标除以边长, 没有余数, 索引减1
// 例如点了x座标为20, 边宽为10, 20 % 10 没有余数,
// index为1, 即在数组中的索引为1(第二个元素)
if (relative % size == 0)
{
index = relative / size - 1;
}
else
{
// 有余数, 例如点了x座标为21, 边宽为10, 21 % 10有余数, index为2
// 即在数组中的索引为2(第三个元素)
index = relative / size;
}
return index;
}
// 实现接口的link方法
@Override
public LinkInfo link(Piece p1, Piece p2)
{
// 两个Piece是同一个, 即选中了同一个方块, 返回null
if (p1.equals(p2))
return null;
// 如果p1的图片与p2的图片不相同, 则返回null
if (!p1.isSameImage(p2))
return null;
// 如果p2在p1的左边, 则需要重新执行本方法, 两个参数互换
if (p2.getIndexX() < p1.getIndexX())
return link(p2, p1);
// 获取p1的中心点
Point p1Point = p1.getCenter();
// 获取p2的中心点
Point p2Point = p2.getCenter();
// 如果两个Piece在同一行
if (p1.getIndexY() == p2.getIndexY())
{
// 它们在同一行并可以相连
if (!isXBlock(p1Point, p2Point, GameConf.PIECE_WIDTH))
{
return new LinkInfo(p1Point, p2Point);
}
}
// 如果两个Piece在同一列
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的key存放第一个转折点, value存放第二个转折点,
// map的size()说明有多少种可以连的方式
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;
}
/**
* 获取两个转折点的情况
*
* @param point1
* @param point2
* @return Map对象的每个key-value对代表一种连接方式,
* 其中key、value分别代表第1个、第2个连接点
*/
private Map<Point, Point> getLinkPoints(Point point1, Point point2,
int pieceWidth, int pieceHeight)
{
Map<Point, Point> result = new HashMap<Point, Point>();
// 获取以point1为中心的向上, 向右, 向下的通道
List<Point> p1UpChanel = getUpChanel(point1, point2.y, pieceHeight);
List<Point> p1RightChanel = getRightChanel(point1, point2.x, pieceWidth);
List<Point> p1DownChanel = getDownChanel(point1, point2.y, pieceHeight);
// 获取以point2为中心的向下, 向左, 向上的通道
List<Point> p2DownChanel = getDownChanel(point2, point1.y, pieceHeight);
List<Point> p2LeftChanel = getLeftChanel(point2, point1.x, pieceWidth);
List<Point> p2UpChanel = getUpChanel(point2, point1.y, pieceHeight);
// 获取Board的最大高度
int heightMax = (this.config.getYSize() + 1) * pieceHeight
+ this.config.getBeginImageY();
// 获取Board的最大宽度
int widthMax = (this.config.getXSize() + 1) * pieceWidth
+ this.config.getBeginImageX();
// 先确定两个点的关系
// point2在point1的左上角或者左下角
if (isLeftUp(point1, point2) || isLeftDown(point1, point2))
{
// 参数换位, 调用本方法
return getLinkPoints(point2, point1, pieceWidth, pieceHeight);
}
// p1、p2位于同一行不能直接相连
if (point1.y == point2.y)
{
// 在同一行
// 向上遍历
// 以p1的中心点向上遍历获取点集合
p1UpChanel = getUpChanel(point1, 0, pieceHeight);
// 以p2的中心点向上遍历获取点集合
p2UpChanel = getUpChanel(point2, 0, pieceHeight);
Map<Point, Point> upLinkPoints = getXLinkPoints(p1UpChanel,
p2UpChanel, pieceHeight);
// 向下遍历, 不超过Board(有方块的地方)的边框
// 以p1中心点向下遍历获取点集合
p1DownChanel = getDownChanel(point1, heightMax, pieceHeight);
// 以p2中心点向下遍历获取点集合
p2DownChanel = getDownChanel(point2, heightMax, pieceHeight);
Map<Point, Point> downLinkPoints = getXLinkPoints(p1DownChanel,
p2DownChanel, pieceHeight);
result.putAll(upLinkPoints);
result.putAll(downLinkPoints);
}
// p1、p2位于同一列不能直接相连
if (point1.x == point2.x)
{
// 在同一列
// 向左遍历
// 以p1的中心点向左遍历获取点集合
List<Point> p1LeftChanel = getLeftChanel(point1, 0, pieceWidth);
// 以p2的中心点向左遍历获取点集合
p2LeftChanel = getLeftChanel(point2, 0, pieceWidth);
Map<Point, Point> left
没有合适的资源?快使用搜索试试~ 我知道了~
Android游戏疯狂连连看源代码
共224个文件
html:91个
png:56个
class:34个
5星 · 超过95%的资源 需积分: 16 2.1k 下载量 168 浏览量
2013-03-05
08:54:04
上传
评论 8
收藏 9.72MB RAR 举报
温馨提示
Android系统的疯狂连连看游戏源代码,代码注释比较丰富,而且资源文件完整,还有文档,看上去一切都很规范,压缩包有9M多,值得参阅的Android游戏源代码。源代码内包括了封装图片ID与图片本身的工具类,来自疯狂Java联盟。 根据游戏状态来绘制游戏界面上的全部方块。
资源推荐
资源详情
资源评论
收起资源包目录
Android游戏疯狂连连看源代码 (224个子文件)
resources.ap_ 3.25MB
18_Link.apk 3.27MB
jarlist.cache 119B
proguard.cfg 1KB
GameServiceImpl.class 13KB
Link.class 9KB
GameView.class 5KB
ImageUtil.class 4KB
AbstractBoard.class 2KB
Piece.class 2KB
Link$1.class 2KB
LinkInfo.class 1KB
R$drawable.class 1KB
GameConf.class 1KB
HorizontalBoard.class 1KB
VerticalBoard.class 1KB
Link$3.class 1KB
Link$2.class 1KB
FullBoard.class 1KB
Link$8.class 1KB
Welcome.class 1KB
Welcome$1.class 1017B
Link$5.class 985B
Link$4.class 965B
Link$6.class 894B
Link$7.class 894B
PieceImage.class 855B
Welcome$2.class 759B
Link$9.class 690B
R$id.class 620B
R.class 559B
Sound.class 477B
R$raw.class 461B
R$string.class 454B
R$layout.class 415B
GameService.class 398B
BuildConfig.class 341B
R$attr.class 334B
.classpath 380B
stylesheet.css 1KB
classes.dex 32KB
success.gif 46KB
lost.gif 30KB
inherit.gif 57B
GameServiceImpl.html 41KB
Piece.html 28KB
R.drawable.html 25KB
Link.html 24KB
index-6.html 21KB
constant-values.html 20KB
Piece.html 19KB
GameConf.html 18KB
GameView.html 17KB
LinkInfo.html 17KB
GameConf.html 15KB
ImageUtil.html 15KB
PieceImage.html 13KB
index-12.html 13KB
HorizontalBoard.html 12KB
LinkInfo.html 12KB
GameService.html 12KB
AbstractBoard.html 12KB
VerticalBoard.html 12KB
index-14.html 12KB
FullBoard.html 12KB
GameService.html 12KB
package-use.html 12KB
package-use.html 11KB
R.id.html 11KB
index-8.html 11KB
PieceImage.html 11KB
R.string.html 11KB
R.html 11KB
R.layout.html 10KB
BuildConfig.html 10KB
index-9.html 9KB
package-use.html 9KB
index-3.html 9KB
AbstractBoard.html 9KB
index-13.html 9KB
R.attr.html 9KB
overview-tree.html 9KB
index-7.html 9KB
help-doc.html 8KB
index-11.html 8KB
index-2.html 8KB
package-summary.html 8KB
GameView.html 7KB
index-5.html 7KB
package-summary.html 7KB
index-16.html 7KB
index-1.html 7KB
package-tree.html 7KB
index-4.html 7KB
package-summary.html 7KB
package-summary.html 7KB
index-15.html 7KB
package-summary.html 7KB
package-tree.html 7KB
index-17.html 6KB
共 224 条
- 1
- 2
- 3
lx76939412
- 粉丝: 261
- 资源: 396
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页