溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實現(xiàn)九格智能拼圖算法

發(fā)布時間:2022-03-24 14:03:35 來源:億速云 閱讀:240 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Android如何實現(xiàn)九格智能拼圖算法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

一、九格智能拼圖

游戲規(guī)則:將一副圖片分割為9個子圖片,其中一個為空白圖片,隨機打亂。通過兩兩圖片的交換,合并為一張圖片,最后游戲完成。

二、開發(fā)步驟

1、將一個圖片分割為9個子圖片,放入ArrayList中。

利用Bitmap.createBitmap()進行圖片切割, 根據(jù)參數(shù)坐標的不同,可以切圖一張圖片的任意部分。

2、采用自定義view,隨機打亂的畫出9個子圖片

選生成0-9的隨機排列。然后根據(jù)排列值畫出對應(yīng)的圖片

3、在自定義view中響應(yīng)點擊圖片是否可以移動。

遍歷左右方塊數(shù)字,如果為0,則可以移動。同時交換相連數(shù)字和數(shù)組中的位置。

4、判斷游戲完成的結(jié)束算法。

依次遍歷各個方塊,如何數(shù)字呈依次遞增排列,則游戲結(jié)束

代碼:

package org.diudiululu.magicSquare;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
 
/**
 * @author a1623z
 * 
 */
public class MagicSquareActivity extends Activity {
    private static final String TAG = "MagicSquare";
    public static final int SQUARE_WIDTH = 3;
 
    private int square[] = new int[SQUARE_WIDTH * SQUARE_WIDTH];
 
    private int steps = 0;
 
    private MagicSquareView magicSquareView;
 
    public int getTitleNumber(int x, int y) {
        return square[y * SQUARE_WIDTH + x];
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(this.TAG, "OnCreate");
 
        initGame();
 
        magicSquareView = new MagicSquareView(this);
 
        this.setContentView(magicSquareView);
        magicSquareView.requestFocus();
    }
 
    private void initGame() {
        generateMagicSquare();
        steps = 0;
    }
 
    private void generateMagicSquare() {
        java.util.ArrayList<Integer> numArray = new java.util.ArrayList<Integer>();
 
        for (int i = 0; i < square.length; i++) {
            numArray.add(new Integer(i));
        }
 
        int i = 0;
        while (numArray.size() > 0) {
            int index = (int) (Math.random() * numArray.size());
            Integer integer = numArray.get(index);
            square[i] = integer.intValue();
            i++;
            numArray.remove(index);
        }
    }
 
    public boolean moveable(int x, int y) {
        if (x < 0 || x >= SQUARE_WIDTH)
            return false;
 
        if (y < 0 || y >= SQUARE_WIDTH)
            return false;
 
        for (int i = x - 1; i <= x + 1; i++) {
            for (int j = y - 1; j <= y + 1; j++) {
                if (i == x && j == y) // it's myself, skip
                    continue;
 
                if (i != x && j != y)
                    continue;
 
                if (i < 0 || i >= SQUARE_WIDTH)
                    continue;
 
                if (j < 0 || j >= SQUARE_WIDTH)
                    continue;
 
                if (square[j * SQUARE_WIDTH + i] == 0)
                    return true;
            }
        }
 
        return false;
    }
 
    public Point move(int x, int y) {
        Log.d(TAG, "move" + ",x=" + x + ",y=" + y);
        if (!moveable(x, y))
            return new Point(-1, -1);
 
        steps++;
 
        for (int i = x - 1; i <= x + 1; i++) {
            for (int j = y - 1; j <= y + 1; j++) {
                if (i == x && j == y) // it's myself, skip
                    continue;
 
                if (i != x && j != y)
                    continue;
 
                if (i < 0 || i >= SQUARE_WIDTH)
                    continue;
 
                if (j < 0 || j >= SQUARE_WIDTH)
                    continue;
 
                if (square[j * SQUARE_WIDTH + i] == 0) {
                    int temp = square[j * SQUARE_WIDTH + i];
                    square[j * SQUARE_WIDTH + i] = square[y * SQUARE_WIDTH + x];
                    square[y * SQUARE_WIDTH + x] = temp;
                    return new Point(i, j);
                }
            }
        }
 
        return new Point(-1, -1);
    }
 
    public boolean win() {
        for (int i = 0; i < square.length - 1; i++) {
            if (square[i] != i + 1)
                return false;
        }
        return true;
    }
    
    public class Pic{
        private  int id;
        private  Bitmap subbitmap;
    }
}

MagicSquarView.java

/**
 * 
 */
package org.diudiululu.magicSquare;
 
import java.util.ArrayList;
 
import org.diudiululu.magicSquare.R;
 
import android.app.Dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.*;
import android.graphics.*;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.*;
 
/**
 * @author a1623z
 * 
 */
public class MagicSquareView extends View {
    private static final String TAG = "MagicSquare";
    private final MagicSquareActivity magicSquareActivity;
 
    private float width;
    private float height;
    private int selX;
    private int selY;
    private final Rect selRect = new Rect();
    private final int pingtuheight = 200;
 
    private ArrayList<Pic> pieces;
 
    /**
     * @param context
     */
    public MagicSquareView(Context context) {
        super(context);
        this.magicSquareActivity = (MagicSquareActivity) context;
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        // TODO Auto-generated constructor stub
    }
 
    // 切割圖片,放入ArrayList中
    {
        pieces = new ArrayList<MagicSquareView.Pic>();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.pingtu);
        int bitmapwidth = bitmap.getWidth();
        int bitmapheight = bitmap.getHeight();
        int pieceWidth = bitmapwidth / 3;
        int pieceHeight = bitmapheight / 3;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                Pic piece = new Pic();
                piece.index = j + i * 3;
                int xValue = j * pieceWidth;
                int yValue = i * pieceHeight;
                piece.piece = Bitmap.createBitmap(bitmap, xValue, yValue,
                        pieceWidth, pieceHeight);
                pieces.add(piece);
            }
        }
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 
        width = w / 3f;
        height = (h - pingtuheight) / 3f;
        getRect(selX, selY, selRect);
        Log.d(TAG, "onSizeChanged: width=" + width + ", height=" + height
                + ",selX=" + selX + ",selY=" + selY);
        super.onSizeChanged(w, h, oldw, oldh);
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
            select(selX, selY - 1);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            select(selX, selY + 1);
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            select(selX - 1, selY);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            select(selX + 1, selY);
            break;
        case KeyEvent.KEYCODE_ENTER:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            Point point = magicSquareActivity.move(selX, selY);
            if (point.x >= 0 && point.y >= 0) {
                this.invalidate(selRect);
                Rect targetRect = new Rect();
                this.getRect(point.x, point.y, targetRect);
                this.invalidate(targetRect);
            }
            break;
        default:
            return super.onKeyDown(keyCode, event);
        }
        return true;
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN)
            return super.onTouchEvent(event);
        if (event.getY() <= pingtuheight)
            return false;
        Log.i(TAG,
                "event.getX()=" + event.getX() + ",event.getY=" + event.getY());
        select((int) (event.getX() / width),
                (int) ((event.getY() - pingtuheight) / height));
 
        Point point = magicSquareActivity.move(selX, selY);
        if (point.x >= 0 && point.y >= 0) {
            this.invalidate(selRect);
            Rect targetRect = new Rect();
            this.getRect(point.x, point.y, targetRect);
            this.invalidate(targetRect);
        }
        return true;
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        Paint backround = new Paint();
        backround.setColor(getResources().getColor(R.color.ms_backgroud));
        canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backround);
        // 畫出原圖及圖片的介紹
        Rect dst = new Rect();// 屏幕 >>目標矩形
        Bitmap pic = BitmapFactory.decodeResource(getResources(),
                R.drawable.pingtu);
 
        dst.left = 0;
        dst.top = 0;
        dst.right = (int) (width * 3) / 2;
        dst.bottom = pingtuheight;
        canvas.drawBitmap(pic, null, dst, null);
        // 繪制出圖片的介紹
        Paint textpaint = new Paint();
        textpaint.setTextSize(25);
        canvas.drawText("一副美麗的圖片,", dst.right, 30, textpaint);
        canvas.drawText("但已支離破碎......", dst.right, 70, textpaint);
 
        // draw the board
        Paint dark = new Paint();
        dark.setColor(getResources().getColor(R.color.ms_dark));
 
        Paint hilite = new Paint();
        hilite.setColor(getResources().getColor(R.color.ms_hilite));
 
        Paint light = new Paint();
        light.setColor(getResources().getColor(R.color.ms_light));
 
        // draw the minor grid lines
        for (int i = 0; i < 3; i++) {
            canvas.drawLine(0, i * height + pingtuheight, getWidth(), i
                    * height + pingtuheight, light);
            canvas.drawLine(0, i * height + 1 + pingtuheight, getWidth(), i
                    * height + pingtuheight + 1, hilite);
            canvas.drawLine(i * width, pingtuheight, i * width, getHeight()
                    + pingtuheight, light);
            canvas.drawLine(i * width + 1, pingtuheight, i * width + 1,
                    getHeight() + pingtuheight, hilite);
        }
        Rect picrect = new Rect();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
            int n = this.magicSquareActivity.getTitleNumber(i, j);//根據(jù)坐標依次取出0,1,2,3,4,5,6,7,8下標數(shù)組對應(yīng)的值
                if (n == 0)
                    continue;
                picrect.left = (int) (i * width);
                picrect.top = pingtuheight + (int) (j * height);
                picrect.right = (int) (i * width + width);
                picrect.bottom = (int) (pingtuheight + j * height + height);
                canvas.drawBitmap(pieces.get(n).getPiece(), null, picrect, null);
 
            }
        }
 
        Paint selected = new Paint();
        selected.setColor(getResources().getColor(R.color.ms_selected));
        canvas.drawRect(selRect, selected);
 
        if (magicSquareActivity.win()) {
            Dialog winDlg = new Win(magicSquareActivity);
            winDlg.show();
            magicSquareActivity.finish();
        }
    }
 
    private void getRect(int x, int y, Rect rect) {
        Log.i(TAG, "getRect" + x + "y" + y);
        rect.set((int) (x * width), (int) (y * height + pingtuheight), (int) (x
                * width + width), (int) (y * height + height + pingtuheight));
    }
 
    private void select(int x, int y) {
        invalidate(selRect);
        selX = Math.min(Math.max(x, 0), 2);
        selY = Math.min(Math.max(y, 0), 2);
        getRect(selX, selY, selRect);
        invalidate(selRect);
    }
 
    public class Pic {
        public int getIndex() {
            return index;
        }
 
        public void setIndex(int index) {
            this.index = index;
        }
 
        public Bitmap getPiece() {
            return piece;
        }
 
        public void setPiece(Bitmap piece) {
            this.piece = piece;
        }
 
        int index;
        Bitmap piece;
    }
}

三、運行結(jié)果

Android如何實現(xiàn)九格智能拼圖算法

感謝各位的閱讀!關(guān)于“Android如何實現(xiàn)九格智能拼圖算法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI