java移动拼图游戏模拟

[size=large]自己写的一个移动拼图的雏形,自己可以在此基础上发展[/size]

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);
}
}
}


[size=large]下面是test类:[/size]

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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值