Example #1
0
 @Override
 public boolean onTouch(View v, MotionEvent event) {
   switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
       saveHistoryMatrix();
       mStartX = (int) event.getX();
       mStartY = (int) event.getY();
       break;
     case MotionEvent.ACTION_MOVE:
       break;
     case MotionEvent.ACTION_UP:
       mEndX = (int) event.getX();
       mEndY = (int) event.getY();
       judgeDirection(mEndX - mStartX, mEndY - mStartY);
       if (isMoved()) {
         addRandomNum();
         // 修改显示分数
         Game.getGameActivity().setScore(Config.SCROE, 0);
       }
       checkCompleted();
       break;
     default:
       break;
   }
   return true;
 }
Example #2
0
 /** 撤销上次移动 */
 public void revertGame() {
   // 第一次不能撤销
   int sum = 0;
   for (int[] element : mGameMatrixHistory) {
     for (int i : element) {
       sum += i;
     }
   }
   if (sum != 0) {
     Game.getGameActivity().setScore(mScoreHistory, 0);
     Config.SCROE = mScoreHistory;
     for (int i = 0; i < mGameLines; i++) {
       for (int j = 0; j < mGameLines; j++) {
         mGameMatrix[i][j].setNum(mGameMatrixHistory[i][j]);
       }
     }
   }
 }
Example #3
0
  /**
   * 判断是否结束
   *
   * <p>0:结束 1:正常 2:成功
   */
  private void checkCompleted() {
    int result = checkNums();
    if (result == 0) {
      if (Config.SCROE > mHighScore) {
        Editor editor = Config.mSp.edit();
        editor.putInt(Config.KEY_HIGH_SCROE, Config.SCROE);
        editor.apply();
        Game.getGameActivity().setScore(Config.SCROE, 1);
        Config.SCROE = 0;
      }
      AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
      builder
          .setTitle("Game Over")
          .setPositiveButton(
              "Again",
              new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                  startGame();
                }
              })
          .create()
          .show();
      Config.SCROE = 0;
    } else if (result == 2) {
      AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
      builder
          .setTitle("Mission Accomplished")
          .setPositiveButton(
              "Again",
              new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                  // 重新开始
                  startGame();
                }
              })
          .setNegativeButton(
              "Continue",
              new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                  // 继续游戏 修改target
                  Editor editor = Config.mSp.edit();
                  if (mTarget == 1024) {
                    editor.putInt(Config.KEY_GAME_GOAL, 2048);
                    mTarget = 2048;
                    Game.getGameActivity().setGoal(2048);
                  } else if (mTarget == 2048) {
                    editor.putInt(Config.KEY_GAME_GOAL, 4096);
                    mTarget = 4096;
                    Game.getGameActivity().setGoal(4096);
                  } else {
                    editor.putInt(Config.KEY_GAME_GOAL, 4096);
                    mTarget = 4096;
                    Game.getGameActivity().setGoal(4096);
                  }
                  editor.apply();
                }
              })
          .create()
          .show();
      Config.SCROE = 0;
    }
  }