Java二维数组实现数字拼图效果代码示例

作者:袖梨 2022-06-29

本篇文章小编给大家分享一下Java二维数组实现数字拼图效果代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

二维数组可以自己随意定义大小,通过方法判断来实现对所有的数字进行随机打乱,并可以通过移动来正确还原,并可以判断0(表示空格)是否可以移动,是否在范围内。

public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  int[][] arrays = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
  int sum = 1;
  String direction;
  bianLi(arrays);
  daLuanErWeiShuZu(arrays);
  System.out.println("========================================");
  while (true) {
   bianLi(arrays);
   if (isOk(arrays)) {
    break;
   }
   sum++;
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println("请输入移动方向(W 上、S 下、A 左、D 右)");
   direction = scanner.next();
   switch (direction) {
   case "W":
   case "w":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "上", arrays);

    break;
   case "S":
   case "s":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "下", arrays);
    break;
   case "A":
   case "a":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "左", arrays);
    break;
   case "D":
   case "d":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "右", arrays);
    break;
   default:
    System.out.println("非法输入,重新输入");
    break;
   }
  }
  System.out.println("一共走了" + sum + "步");
  System.out.println("挑战成功");

}

判断当前坐标是否可以移动

/**
  * 判断当前坐标是否可以移动
  * 
  * @param arrays
  * @return 可以移动返回true
  */
 public static boolean isYiDong(int[][] arrays) {
  int returnX = returnX(arrays);
  int returnY = returnY(arrays);
  System.out.println(returnX + ":" + returnY);
  if (returnX >= 0 && returnX + 1 < arrays.length && returnY >= 0 && returnY + 1 < arrays.length) {
   return true;
  }
  return false;
 }

获取当前0所在行的具体地址

 // 获取0所在行的位置
 public static int returnX(int[][] arrays) {
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays[i].length; j++) {
    if (0 == arrays[i][j]) {
     return i;
    }
   }
  }
  return -1;
 }

获取当前0所在列的具体地址

 // 获取0所在列的位置
 public static int returnY(int[][] arrays) {
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays[i].length; j++) {
    if (0 == arrays[i][j]) {
     return j;
    }
   }
  }
  return -1;
 }

二维数组随机打乱,需要判断左上角、右上角、左下角、右下角、中间、上中、下种、左中、右中,那些方向可以移动,生成随机数来确定移动方向

// 二维数组随机打乱
 public static void daLuanErWeiShuZu(int[][] arrays) {
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays[i].length; j++) {
    // 左上
    if (i == 0 && j == 0) {
     // 根据生成的随机数确定向右边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
     }
    }

    // 右上
    if (i == 0 && j == arrays[0].length - 1) {
     // 根据生成的随机数确定向左边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
     }
    }

    // 左下
    if (i == arrays.length - 1 && j == 0) {
     // 根据生成的随机数确定向左边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
     }
    }

    // 右下
    if (i == arrays.length - 1 && j == arrays[i].length - 1) {
     // 根据生成的随机数确定向左边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
     }
    }

    // 上中
    if (i == 0 && j > 0 && j < arrays[i].length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;

     default:
      break;
     }
    }
    // 左中
    if (j == 0 && i > 0 && i < arrays.length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;

     default:
      break;
     }
    }
    // 下中
    if (i == arrays.length - 1 && j > 0 && j < arrays[i].length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;

     default:
      break;
     }
    }
    // 右中
    if (j == arrays[i].length - 1 && i > 0 && i < arrays[i].length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;

     default:
      break;
     }
    }
    if (i > 0 && j > 0 && i < arrays.length - 2 && j < arrays[i].length - 2) {
     switch (oneToThree(4)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;
     case 3:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;

     default:
      break;
     }
    }
   }
  }
 }

该方法实现对0的位置和需要替换位置数据的替换,并对0范围进行验证,怕0出现数组下标越位。

/**
  * 根据输入的数据,对二维数组进行数据替换
  * 
  * @param i         高位坐标
  * @param j         地位坐标
  * @param direction 移动方向
  * @param arrays    需要交换数据的数组
  */
 public static void tiHuanShuZuWeiZhi(int i, int j, String direction, int[][] arrays) {
  int tem = -1;
  switch (direction) {
  case "上":
   if (i > 0) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i - 1][j];
    arrays[i - 1][j] = tem;
   }
   break;
  case "下":
   if (i < arrays.length - 1) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i + 1][j];
    arrays[i + 1][j] = tem;
   }
   break;
  case "左":
   if (j > 0) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i][j - 1];
    arrays[i][j - 1] = tem;
   }
   break;
  case "右":
   if (j < arrays.length - 1) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i][j + 1];
    arrays[i][j + 1] = tem;
   }
   break;

  default:
   break;
  }
 }

完整代码如下

import java.util.Random;
import java.util.Scanner;

public class Demo {
 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  int[][] arrays = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
  int sum = 1;
  String direction;
  bianLi(arrays);
  daLuanErWeiShuZu(arrays);
  System.out.println("========================================");
  while (true) {
   bianLi(arrays);
   if (isOk(arrays)) {
    break;
   }
   sum++;
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println("请输入移动方向(W 上、S 下、A 左、D 右)");
   direction = scanner.next();
   switch (direction) {
   case "W":
   case "w":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "上", arrays);

    break;
   case "S":
   case "s":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "下", arrays);
    break;
   case "A":
   case "a":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "左", arrays);
    break;
   case "D":
   case "d":
    tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), "右", arrays);
    break;
   default:
    System.out.println("非法输入,重新输入");
    break;
   }
  }
  System.out.println("一共走了" + sum + "步");
  System.out.println("挑战成功");

 }

// /**
//  * 判断当前坐标是否可以移动
//  * 
//  * @param arrays
//  * @return 可以移动返回true
//  */
// public static boolean isYiDong(int[][] arrays) {
//  int returnX = returnX(arrays);
//  int returnY = returnY(arrays);
//  System.out.println(returnX + ":" + returnY);
//  if (returnX >= 0 && returnX + 1 < arrays.length && returnY >= 0 && returnY + 1 < arrays.length) {
//   return true;
//  }
//  return false;
// }

 /**
  * 
  * @param arrays 需要验证的数据
  * @return 成功返回true
  */
 public static boolean isOk(int[][] arrays) {
  int sum = 1;
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays.length; j++) {
    if (sum == 9) {
     sum = 0;
    }
    if (arrays[i][j] != sum) {
     return false;
    }
    sum++;
   }
  }
  return true;
 }

 // 获取0所在行的位置
 public static int returnX(int[][] arrays) {
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays[i].length; j++) {
    if (0 == arrays[i][j]) {
     return i;
    }
   }
  }
  return -1;
 }

 // 获取0所在列的位置
 public static int returnY(int[][] arrays) {
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays[i].length; j++) {
    if (0 == arrays[i][j]) {
     return j;
    }
   }
  }
  return -1;
 }

 // 遍历二维数组
 public static void bianLi(int[][] arrays) {
  for (int[] is : arrays) {
   for (int is2 : is) {
    System.out.print(is2 + "t");
   }
   System.out.println();
  }
 }

 // 二维数组随机打乱
 public static void daLuanErWeiShuZu(int[][] arrays) {
  for (int i = 0; i < arrays.length; i++) {
   for (int j = 0; j < arrays[i].length; j++) {
    // 左上
    if (i == 0 && j == 0) {
     // 根据生成的随机数确定向右边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
     }
    }

    // 右上
    if (i == 0 && j == arrays[0].length - 1) {
     // 根据生成的随机数确定向左边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
     }
    }

    // 左下
    if (i == arrays.length - 1 && j == 0) {
     // 根据生成的随机数确定向左边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
     }
    }

    // 右下
    if (i == arrays.length - 1 && j == arrays[i].length - 1) {
     // 根据生成的随机数确定向左边边移动还是向下移动
     if (ouShuOrJiShu()) {
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
     } else {
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
     }
    }

    // 上中
    if (i == 0 && j > 0 && j < arrays[i].length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;

     default:
      break;
     }
    }
    // 左中
    if (j == 0 && i > 0 && i < arrays.length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;

     default:
      break;
     }
    }
    // 下中
    if (i == arrays.length - 1 && j > 0 && j < arrays[i].length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;

     default:
      break;
     }
    }
    // 右中
    if (j == arrays[i].length - 1 && i > 0 && i < arrays[i].length - 1) {
     switch (oneToThree(3)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;

     default:
      break;
     }
    }
    if (i > 0 && j > 0 && i < arrays.length - 2 && j < arrays[i].length - 2) {
     switch (oneToThree(4)) {
     case 0:
      tiHuanShuZuWeiZhi(i, j, "上", arrays);
      break;
     case 1:
      tiHuanShuZuWeiZhi(i, j, "右", arrays);
      break;
     case 2:
      tiHuanShuZuWeiZhi(i, j, "下", arrays);
      break;
     case 3:
      tiHuanShuZuWeiZhi(i, j, "左", arrays);
      break;

     default:
      break;
     }
    }
   }
  }
 }

 /**
  * 判断是否是偶数
  * 
  * @return 偶数返回true
  */
 public static boolean ouShuOrJiShu() {
  return new Random().nextInt(1000) % 2 == 0 ? true : false;
 }

 /**
  * 
  * @param n 需要模的值
  * @return 返回0-(n-1)的值
  */
 public static int oneToThree(int n) {
  return new Random().nextInt(1000) % n;
 }

 /**
  * 根据输入的数据,对二维数组进行数据替换
  * 
  * @param i         高位坐标
  * @param j         地位坐标
  * @param direction 移动方向
  * @param arrays    需要交换数据的数组
  */
 public static void tiHuanShuZuWeiZhi(int i, int j, String direction, int[][] arrays) {
  int tem = -1;
  switch (direction) {
  case "上":
   if (i > 0) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i - 1][j];
    arrays[i - 1][j] = tem;
   }
   break;
  case "下":
   if (i < arrays.length - 1) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i + 1][j];
    arrays[i + 1][j] = tem;
   }
   break;
  case "左":
   if (j > 0) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i][j - 1];
    arrays[i][j - 1] = tem;
   }
   break;
  case "右":
   if (j < arrays.length - 1) {
    tem = arrays[i][j];
    arrays[i][j] = arrays[i][j + 1];
    arrays[i][j + 1] = tem;
   }
   break;

  default:
   break;
  }
 }

}

相关文章

精彩推荐