public void UnScramble() {
   mPuzzle.UnScramblePuzzle();
   mBrickGrid = mPuzzle.GetPuzzle();
   enabled = false;
   mStatusText.setText(mPuzzle.getScore());
   mStatusText.setVisibility(View.VISIBLE);
   invalidate();
 }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    Resources r = this.getContext().getResources();

    // mBrickGrid = mPuzzle.CreateNewPuzzle(mXBrickCount, mYBrickCount);

    mXBrickCount = mPuzzle.getXBrickCount();
    mYBrickCount = mPuzzle.getYBrickCount();

    mBrickGrid = mPuzzle.GetPuzzle();

    if (w > h) {
      mBrickSize = (int) Math.floor(h / mYBrickCount);
    } else {
      mBrickSize = (int) Math.floor(w / mXBrickCount);
    }

    resetBricks(29);

    loadBrick(NUM_0, r.getDrawable(R.drawable.pic_0_def));
    loadBrick(NUM_1, r.getDrawable(R.drawable.pic_1_def));
    loadBrick(NUM_2, r.getDrawable(R.drawable.pic_2_def));
    loadBrick(NUM_3, r.getDrawable(R.drawable.pic_3_def));
    loadBrick(NUM_4, r.getDrawable(R.drawable.pic_4_def));
    loadBrick(NUM_5, r.getDrawable(R.drawable.pic_5_def));
    loadBrick(NUM_6, r.getDrawable(R.drawable.pic_6_def));
    loadBrick(NUM_7, r.getDrawable(R.drawable.pic_7_def));
    loadBrick(NUM_8, r.getDrawable(R.drawable.pic_8_def));
    loadBrick(NUM_9, r.getDrawable(R.drawable.pic_9_def));
    loadBrick(OP_PL, r.getDrawable(R.drawable.pic_add_def));
    loadBrick(OP_MN, r.getDrawable(R.drawable.pic_min_def));
    loadBrick(OP_MT, r.getDrawable(R.drawable.pic_mul_def));
    loadBrick(OP_EQ, r.getDrawable(R.drawable.pic_eq_def));
    loadBrick(BLANK, r.getDrawable(R.drawable.pic_blk));
    loadBrick(NUM_0_D, r.getDrawable(R.drawable.pic_0_d));
    loadBrick(NUM_1_D, r.getDrawable(R.drawable.pic_1_d));
    loadBrick(NUM_2_D, r.getDrawable(R.drawable.pic_2_d));
    loadBrick(NUM_3_D, r.getDrawable(R.drawable.pic_3_d));
    loadBrick(NUM_4_D, r.getDrawable(R.drawable.pic_4_d));
    loadBrick(NUM_5_D, r.getDrawable(R.drawable.pic_5_d));
    loadBrick(NUM_6_D, r.getDrawable(R.drawable.pic_6_d));
    loadBrick(NUM_7_D, r.getDrawable(R.drawable.pic_7_d));
    loadBrick(NUM_8_D, r.getDrawable(R.drawable.pic_8_d));
    loadBrick(NUM_9_D, r.getDrawable(R.drawable.pic_9_d));
    loadBrick(OP_PL_D, r.getDrawable(R.drawable.pic_add_d));
    loadBrick(OP_MN_D, r.getDrawable(R.drawable.pic_min_d));
    loadBrick(OP_MT_D, r.getDrawable(R.drawable.pic_mul_d));
    loadBrick(OP_EQ_D, r.getDrawable(R.drawable.pic_eq_d));

    mXOffset = ((w - (mBrickSize * mXBrickCount)) / 2);
    mYOffset = ((h - (mBrickSize * mYBrickCount)) / 2);
  }
  public boolean onTouch(View v, MotionEvent event) {
    // Don't do anything until started.
    if (!enabled) return true;

    Index index;

    float x = event.getX();
    float y = event.getY();

    // The touch was outside the grid, ignore it
    index = CoordinateToIndex(x, y);
    if (index.x == 2012 || index.y == 2012) {
      clearSelected();
      return true;
    }

    // The initial touch downward
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:

        // Record the initial down coordinates
        downX = event.getX();
        downY = event.getY();

        break;
      case MotionEvent.ACTION_MOVE:
        // What to do while the finger is moving.

        // Commenting out the break will fall through to ACTION_UP.

        // This will change the tiles while moving.
        // Need to undo this on ACTION_UP
        if (mBrickGrid[index.x][index.y] < 2012) {
          if (mBrickGrid[index.x][index.y] < BLANK) {
            mBrickGrid[index.x][index.y] = mBrickGrid[index.x][index.y] + 15;

            invalidate();
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        clearSelected();

        // Record the up coordinates
        upX = event.getX();
        upY = event.getY();

        // ((TextView)findViewById(R.id.score)).setText("Up");

        Index downIndex;
        Index upIndex;

        downIndex = CoordinateToIndex(downX, downY);
        upIndex = CoordinateToIndex(upX, upY);

        // Check for drag on column
        if ((upIndex.x == downIndex.x) && (Math.abs(upIndex.y - downIndex.y) == 4)) {
          int points = mPuzzle.submit(columnToString(upIndex.x));

          if (points == -2) {
            AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());

            alert.setMessage("You Win!");
            CharSequence ok = "Ok";
            alert.setPositiveButton(
                ok,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface arg0, int arg1) {
                    arg0.dismiss();
                  }
                });
            alert.show();
            mStatusText.setText(mPuzzle.getScore());
          } else if (points > 0) {
            popUpPoints(v, Integer.toString(points));
            mStatusText.setText(mPuzzle.getScore());
            mStatusText.setVisibility(View.VISIBLE);
            popUpPoints(v, Integer.toString(points));
          } else {
            // Play a sound and vibrate?
          }

          // Break out of the on up to avoid tile press detection.
          break;
        }

        // Check for drag on row
        if ((upIndex.y == downIndex.y) && (Math.abs(upIndex.x - downIndex.x) == 4)) {
          int points = mPuzzle.submit(rowToString(upIndex.y));
          if (points > 0) {
            popUpPoints(v, Integer.toString(points));
            mStatusText.setText(mPuzzle.getScore());
            mStatusText.setVisibility(View.VISIBLE);
          }

          // Break out of the on up to avoid tile press detection.
          break;
        }

        // No drag, then it should be a tile press.
        if (mBrickGrid[index.x][index.y] < 2012) {
          if (mBrickGrid[index.x][index.y] > BLANK) {
            mBrickGrid[index.x][index.y] = mBrickGrid[index.x][index.y] - 15;
            invalidate();
          }
        }
        mPuzzle.ChangePuzzle(index.x, index.y);
        mBrickGrid = mPuzzle.GetPuzzle();
        invalidate();

        break;
      default:
        break;
    }

    return true;
  }
 /*
  *
  * 28 October 2011 - FWS Added the Scramble() and UnScramble() methods
  */
 public void Scramble() {
   enabled = true;
   mPuzzle.ScramblePuzzle();
   mBrickGrid = mPuzzle.GetPuzzle();
   invalidate();
 }