java移动拼图游戏模拟_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java移动拼图游戏模拟

java移动拼图游戏模拟

 2011/12/13 9:16:36  ling凌yue月  http://chen498402552-163-com.iteye.com  我要评论(0)
  • 摘要:自己写的一个移动拼图的雏形,自己可以在此基础上发展publicclassMovePuzzle{/****/staticintrank[][];/***0数组的索引*/publicstaticintZERO_INDEX_Y;publicstaticintZERO_INDEX_X;/****/privatestaticintx=3;privatestaticinty=3;/****@paramrak*/publicMovePuzzle(intx,inty){this
  • 标签:Java 游戏
自己写的一个移动拼图的雏形,自己可以在此基础上发展
public class MovePuzzle {

	/**
	 * 
	 */
	static int rank[][];

	/**
	 * 0数组的索引
	 */
	public static int ZERO_INDEX_Y;
	public static int ZERO_INDEX_X;

	/**
	 * 
	 */
	private static int x = 3;
	private static int y = 3;

	/**
	 * 
	 * @param rak
	 */
	public MovePuzzle(int x, int y) {
		this.rank = new int[x][y];
		this.x = x;
		this.y = y;

	}

	/**
	 * 初始化this.rank
	 * 
	 * @return 返回一个初始化的二维数组
	 */
	public int[][] initRank() {
		return this.initRank(initRank(this.rank));
	}

	/**
	 * 初始化参数1
	 * 
	 * @param rak
	 * @return 将参数1的二维数组初始化成一个 从0开始的一个二维数组 0 1 2 3 4 5 6 7 8
	 */
	private int[][] initRank(int[][] rak) {
		for (int i = 0; i < rak.length; i++) {
			for (int j = 0; j < rak[i].length; j++) {
				rak[i][j] = i * rak.length + j;
				if (rak[i][j] == 0) {
					this.ZERO_INDEX_X = i;
					this.ZERO_INDEX_Y = j;
				}
			}
		}
		return rak;
	}

	/**
	 * 打印数组
	 */
	public void printArr() {
		this.print2DArr(this.rank);
	}

	/**
	 * 打印出所有的二维数组
	 */
	public void print2DArr(int[][] twoDArr) {
		for (int i = 0; i < twoDArr.length; i++) {
			for (int j = 0; j < twoDArr[i].length; j++) {
				System.out.print(twoDArr[i][j] + "\t");
			}
			System.out.println();
		}
	}

	/**
	 * 得到0值得x索引和y索引
	 * 
	 * @param twoDArr
	 */
	public void getZero_index(int[][] twoDArr) {
		for (int i = 0; i < twoDArr.length; i++) {
			for (int j = 0; j < twoDArr[i].length; j++) {
				if (twoDArr[i][j] == 0) {
					this.ZERO_INDEX_X = i;
					this.ZERO_INDEX_Y = j;
				}
			}
		}
	}

	/**
	 * 调换this.rank[oldX][oldY] 和 this.rank[newX][newY]的值
	 * 
	 * @param oldX
	 * @param oldY
	 * @param newX
	 * @param newY
	 * @return 是否调换成功
	 */
	public boolean exchangeBlock(int oldX, int oldY, int newX, int newY) {
		int tmp = 1;
		tmp = this.rank[oldX][oldY];
		this.rank[oldX][oldY] = this.rank[newX][newY];
		this.rank[newX][newY] = tmp;
		if (tmp == 0) {
			this.ZERO_INDEX_X = newX;
			this.ZERO_INDEX_Y = newY;
		}
		return true;
	}

	/**
	 * 检查是否可以移动
	 * 
	 * @param oldX
	 * @param oldY
	 * @param newX
	 * @param newY
	 * @return
	 */
	public boolean checkCanMove(int oldX, int oldY, int newX, int newY) {
		boolean result = true;
		if (this.rank[oldX][oldY] != 0 && this.rank[newX][newY] != 0) {
			result = false;
		} else if ((oldX != newX) && (oldY != newY)) {
			result = false;
		}
		return result;
	}

	/**
	 * 移动数字index
	 * 
	 * @param index
	 */
	public void moveBlock(int index) {
		int index_x = 0;
		int index_y = 0;
		/**
		 * 找出要移动的数字的位置
		 */
		for (int i = 0; i < this.rank.length; i++) {
			for (int j = 0; j < this.rank[i].length; j++) {
				if (this.rank[i][j] == index) {
					index_x = i;
					index_y = j;
				}
			}
		}
		if (checkCanMove(this.ZERO_INDEX_X, this.ZERO_INDEX_Y, index_x, index_y)) {
			exchangeBlock(this.ZERO_INDEX_X, this.ZERO_INDEX_Y, index_x,
					index_y);
		}
	}
}


下面是test类:
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MovePuzzle movePuzzle = new MovePuzzle(3, 3);
		movePuzzle.initRank();
		movePuzzle.printArr();
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,5);
		move(movePuzzle,4);
		move(movePuzzle,3);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,4);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,6);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,5);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,6);
		move(movePuzzle,4);
		move(movePuzzle,5);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,6);
		move(movePuzzle,4);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,8);
		move(movePuzzle,5);
		move(movePuzzle,6);
		move(movePuzzle,4);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,6);
		move(movePuzzle,5);
		move(movePuzzle,7);
		move(movePuzzle,4);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,6);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,4);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,6);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,4);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,6);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,4);
		move(movePuzzle,1);
		move(movePuzzle,2);
		move(movePuzzle,3);
		move(movePuzzle,6);
		move(movePuzzle,8);
		move(movePuzzle,7);
		move(movePuzzle,7);
		move(movePuzzle,8);
	}

	public static void move(MovePuzzle movePuzzle,int i){
		movePuzzle.moveBlock(i);
		System.out.println("-----调换后"+i+"后------");
		movePuzzle.printArr();
	}
}
发表评论
用户名: 匿名