SoFunction
Updated on 2025-03-09

Android implementation puzzle game

This article shares the specific code of Android implementation puzzle game for your reference. The specific content is as follows

I've done it with android studio

Source code

package packageName;

import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;

import MyImg;

public class MainActivity extends AppCompatActivity {
    // Show the width of the picture    public static final int W = 250;
    // Top left margin    public static final int MARGIN = 200;
    // Index of empty pictures    public static final int NULLINDEX = 0;
    private MyImg[] imgs = new MyImg[9];
    // Map of image location    private int[] map = new int[9];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        // Nothing is set up in the main layout        setContentView(.activity_main);
        // Used to set the width and height of the generated view object         params = new (.WRAP_CONTENT, .WRAP_CONTENT);
        initImg(params);
        randomMap();
        addImg(params);
        // Start a new game        Button newBtn = new Button(this);
        ("New Game");
        (16);
        (40);
        // Add controls to use         p1= new (.WRAP_CONTENT, .WRAP_CONTENT);
        addContentView(newBtn, p1);
        (new () {
            @Override
            public void onClick(View v) {
                newGame();
            }
        });
    }
    // Add an image to the layout and set a click event    private void addImg( params) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int index = i * 3 + j;
                // Calculate x, y coordinates                int x = j * W + MARGIN;
                int y = i * W + MARGIN;
                ImageView imgView = imgs[map[index]].getImg();
                (x);
                (y);
                addContentView(imgView, params);
                (new () {
                    @Override
                    public void onClick(View v) {
                        ImageView tempImg = imgs[NULLINDEX].getImg();
                        int x = (int) ();
                        int y = (int) ();
                        // goal image
                        int x1 = (int) ();
                        int y1 = (int) ();
                        // move top
                        if (y - y1 == W && x == x1) {
                            top((ImageView) v);
                        } else if (y - y1 == -W && x == x1) {
                            down((ImageView) v);
                        } else if (x - x1 == W && y == y1) {
                            left((ImageView) v);
                        } else if (x - x1 == -W && y == y1) {
                            right((ImageView) v);
                        }
                        if (isWin()) {
                            (, "You Win!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
    }

    private void newGame() {
        randomMap();
        // Set the x and y coordinates of the picture        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int index = i * 3 + j;
                int x = j * W + MARGIN;
                int y = i * W + MARGIN;
                ImageView imgView = imgs[map[index]].getImg();
                (x);
                (y);
            }
        }
    }

    private void left(ImageView img) {
        (() - W);
        imgs[NULLINDEX].getImg().setX(() + W);
    }

    private void right(ImageView img) {
        (() + W);
        imgs[NULLINDEX].getImg().setX(() - W);
    }

    private void top(ImageView img) {
        (() - W);
        imgs[NULLINDEX].getImg().setY(() + W);
    }

    private void down(ImageView img) {
        (() + W);
        imgs[NULLINDEX].getImg().setY(() - W);
    }

    private boolean isWin() {
        // Calculate the position of the picture based on the coordinates of x and y. If one corresponds one by one, then it will be a long time to win.        for (int i = 0; i < 9; i++) {
            ImageView img = imgs[i].getImg();
            int x = (int) ();
            int y = (int) ();
            int index = (y - MARGIN) / W * 3 + (x - MARGIN) / W;
            // There is one that doesn't match, it's not winning            if (index != imgs[i].getType()) {
                return false;
            }
        }
        return true;
    }

    private void randomMap() {
        // Disrupt the location of the map        int a, b;
        for (int i = 0; i < 50; i++) {
            a = (int) (() * 9);
            b = (int) (() * 9);
            int t = map[a];
            map[a] = map[b];
            map[b] = t;
        }
    }
    // Arrange the image array    private void initImg( params) {
        int[] imgId = {.img10, .img2, .img3, .img4, .img5,
                .img6, .img7, .img8, .img9};
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int index = i * 3 + j;
                imgs[index] = new MyImg(index);
                MyImg img = imgs[index];
                ImageView image = new ImageView(this);
                 = W;
                 = W;
                (params);
                (imgId[index]);
                (image);
                // Initialize the map                map[index] = index;
            }
        }
    }
}

MyImg class

package packageName;

import ;

public class MyImg {
    // Index for storing image location    private int type;
    private ImageView img;

    public MyImg(int type) {
         = type;
    }

    public void setImg(ImageView img) {
         = img;
    }

    public ImageView getImg() {
        return img;
    }
    // Get the image index    public int getType() {
        return type;
    }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.