⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 document.java

📁 手机拼图游戏... 成品..我上载..只为了下一个拼图游戏代码来参考一下
💻 JAVA
字号:
/*
 * Created on 2005-3-15
 * Author: Xuefeng, Copyright (C) 2005, Xuefeng.
 */
package com.crackj2ee.j2megame.puzzle;

import javax.microedition.lcdui.Image;

/**
 * +-----+-----+-----+
 * | 0,0 | 0,1 | 0,2 |
 * +-----+-----+-----+
 * | 1,0 | 1,1 | 1,2 |
 * +-----+-----+-----+
 * | 2,0 | 2,1 | 2,2 |
 * +-----+-----+-----+
 *
 * @author Xuefeng
 */
public class Document {

    // status:
    public static final int PUZZLE_STATE = 0;
    public static final int IMAGE_STATE  = 1;
    public static final int FINISH_STATE = 2;

    // hold the view:
    private Updatable updatable;
    // hold the state:
    private int state = PUZZLE_STATE;
    // hold the origin images:
    private Image[] images;
    // hold the image index:
    private int[][] current = new int[4][4];
    // hold the hidden position:
    private int hiddenX, hiddenY;
    // hold the steps:
    private int steps = 0;

    public Document(Updatable updatable, Image[] images, int hiddenX, int hiddenY) {
        // check arguments:
        if(images.length!=16)
            throw new IllegalArgumentException("Image array must contain 16 images.");
        if(hiddenX<0 || hiddenX>3)
            throw new IllegalArgumentException("Hidden image X index must be 0,1,2.3");
        if(hiddenY<0 || hiddenY>3)
            throw new IllegalArgumentException("Hidden image Y index must be 0,1,2.3");
        // init:
        this.updatable = updatable;
        this.images = images;
        this.hiddenX = hiddenX;
        this.hiddenY = hiddenY;
        // puzzle it:
        int[][] puzzle = {
            //{0, 1, 2, 3, 4, 5, 6, 7, 8},
            {2, 7, 5, 1, 0, 6, 4, 3, 8,13,12,11,10,14,9,15},
            {1, 6, 7, 5, 2, 3, 4, 0, 8,13,12,11,10,14,9,15},
            {1, 6, 0, 3, 2, 7, 5, 4, 8,13,12,11,10,14,9,15},
            {2, 5, 0, 6, 4, 7, 3, 1, 8,13,12,11,10,14,9,15}
        };
        int n = (int)(System.currentTimeMillis() % 4);
        for(int x=0; x<4; x++) {
            for(int y=0; y<4; y++) {
                current[x][y] = puzzle[n][4*x+y];
            }
        }
    }

    public int getState() {
        return state;
    }

    public void setImageState() {
        if(state==FINISH_STATE)
            return;
        state = IMAGE_STATE;
        updatable.update();
    }

    public void setPuzzleState() {
        if(state==FINISH_STATE)
            return;
        state = PUZZLE_STATE;
        updatable.update();
    }

    /**
     * Get the Image on the current position. If hidden, null will be return.
     *
     * @param x The x position.
     * @param y The y position.
     * @return Current image or null if it is hidden in PUZZLE_STATE.
     */
    public Image getImage(int x, int y) {
        if(state==PUZZLE_STATE) {
            if( (x==hiddenX) && (y==hiddenY) )
                return null;
            return images[current[x][y]];
        }
        return images[4*x+y];
    }

    private boolean isFinish() {
        for(int i=0; i<4; i++) {
            for(int j=0; j<4; j++) {
                if(current[i][j]!=(i*4+j))
                    return false;
            }
        }
        return true;
    }

    /**
     * Move the current (x, y) to the blank. If (x, y) is not near blank,
     * nothing will happen and false will return.
     *
     * @param x The x position.
     * @param y The y position.
     */
    public void move(int x, int y) {
        if(state!=PUZZLE_STATE)
            return;
        if(hiddenX==x && hiddenY==y)
            return;
        boolean moved = false;
        // find out the hidden position:
        if( ((x-1)==hiddenX) && (y==hiddenY) ) {
            sweep(x, y);
            moved = true;
        }
        if( ((x+1)==hiddenX) && (y==hiddenY) ) {
            sweep(x, y);
            moved = true;
        }
        if( (x==hiddenX) && ((y-1)==hiddenY) ) {
            sweep(x, y);
            moved = true;
        }
        if( (x==hiddenX) && ((y+1)==hiddenY) ) {
            sweep(x, y);
            moved = true;
        }
        if(moved) {
            steps++;
            if(isFinish()) {
                // set state:
                state = FINISH_STATE;
            }
      	    updatable.update();
    	}
    }

    // sweep the (x,y) and (hiddenX, hiddenY), and set the properate hidden position:
    private void sweep(int x, int y) {
        int temp = current[x][y];
        current[x][y] = current[hiddenX][hiddenY];
        current[hiddenX][hiddenY] = temp;
        hiddenX = x;
        hiddenY = y;
    }

    public int getSteps() {
        return steps;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -