疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

android开发—连连看开发技巧(10)


 

android游戏开发之连连看(10)

实现GameService组件

对于GameService组件的前面3个方法,实现起来都比较简单,
程序清单:codes18LinksrcorgcrazyitlinkoardimplGameServiceImpl.java
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;
         }
         ...
}
上面三个方法实现都很简单,相信读者很容易理解。下面详细介绍剩下的两个方法的实现。(未完.摘自[疯狂Android讲义.李刚])