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

}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: