Exemplo n.º 1
0
  /** 产生一个数字 */
  public void generateNum() {

    if (checkOver()) {
      Log.e("TAG", "GAME OVER");
      if (mGame2048Listener != null) {
        mGame2048Listener.onGameOver();
      }
      return;
    }
    if (!isFull()) {
      if (isMoveHappen || isMergeHappen) {
        Random random = new Random();
        int next = random.nextInt(16);
        Game2048Item item = mGame2048Items[next];

        while (item.getmNumber() != 0) {
          next = random.nextInt(16);
          item = mGame2048Items[next];
        }

        item.setmNumber(Math.random() > 0.75 ? 4 : 2);

        isMergeHappen = isMoveHappen = false;
      }
    }
  }
Exemplo n.º 2
0
  /**
   * 合并相同的Item
   *
   * @param row
   */
  private void mergeItem(List<Game2048Item> row) {
    if (row.size() < 2) return;

    for (int j = 0; j < row.size() - 1; j++) {
      Game2048Item item1 = row.get(j);
      Game2048Item item2 = row.get(j + 1);

      if (item1.getmNumber() == item2.getmNumber()) {
        isMergeHappen = true;

        int val = item1.getmNumber() + item2.getmNumber();
        item1.setmNumber(val);

        // 加分
        mScore += val;
        if (mGame2048Listener != null) {
          mGame2048Listener.onScoreChange(mScore);
        }

        // 向前移动
        for (int k = j + 1; k < row.size() - 1; k++) {
          row.get(k).setmNumber(row.get(k + 1).getmNumber());
        }

        row.get(row.size() - 1).setmNumber(0);
        return;
      }
    }
  }
Exemplo n.º 3
0
 /** 重新开始游戏 */
 public void restart() {
   for (Game2048Item item : mGame2048Items) {
     item.setmNumber(0);
   }
   mScore = 0;
   if (mGame2048Listener != null) {
     mGame2048Listener.onScoreChange(mScore);
   }
   isMoveHappen = isMergeHappen = true;
   generateNum();
 }