首页 > 代码库 > 九格智能拼图

九格智能拼图

最近想业余做一款android游戏,发现我国一款古老好玩的智力游戏-九格智能拼图挺好玩的,相信大多80后小时玩过,因此有了开发的想法。

一、九格智能拼图

游戏规则:将一副图片分割为9个子图片,其中一个为空白图片,随机打乱。通过两两图片的交换,合并为一张图片,最后游戏完成。

二、开发步骤

1、将一个图片分割为9个子图片,放入ArrayList中。

利用Bitmap.createBitmap()进行图片切割, 根据参数坐标的不同,可以切图一张图片的任意部分。

2、采用自定义view,随机打乱的画出9个子图片

选生成0-9的随机排列。然后根据排列值画出对应的图片

3、在自定义view中响应点击图片是否可以移动。

遍历左右方块数字,如果为0,则可以移动。同时交换相连数字和数组中的位置。

4、判断游戏完成的结束算法。

依次遍历各个方块,如何数字呈依次递增排列,则游戏结束

代码:

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 = http://www.mamicode.com/j * pieceWidth;>三、运行结果