您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Java二維數(shù)組怎么實現(xiàn)數(shù)字拼圖效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Java二維數(shù)組怎么實現(xiàn)數(shù)字拼圖效果”吧!
二維數(shù)組實現(xiàn)數(shù)字拼圖,供大家參考,具體內(nèi)容如下
二維數(shù)組可以自己隨意定義大小,通過方法判斷來實現(xiàn)對所有的數(shù)字進(jìn)行隨機(jī)打亂,并可以通過移動來正確還原,并可以判斷0(表示空格)是否可以移動,是否在范圍內(nèi)。
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("挑戰(zhàn)成功"); }
判斷當(dāng)前坐標(biāo)是否可以移動
/** * 判斷當(dāng)前坐標(biāo)是否可以移動 * * @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; }
獲取當(dāng)前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; }
獲取當(dāng)前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; }
二維數(shù)組隨機(jī)打亂,需要判斷左上角、右上角、左下角、右下角、中間、上中、下種、左中、右中,那些方向可以移動,生成隨機(jī)數(shù)來確定移動方向
// 二維數(shù)組隨機(jī)打亂 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) { // 根據(jù)生成的隨機(jī)數(shù)確定向右邊邊移動還是向下移動 if (ouShuOrJiShu()) { tiHuanShuZuWeiZhi(i, j, "下", arrays); } else { tiHuanShuZuWeiZhi(i, j, "右", arrays); } } // 右上 if (i == 0 && j == arrays[0].length - 1) { // 根據(jù)生成的隨機(jī)數(shù)確定向左邊邊移動還是向下移動 if (ouShuOrJiShu()) { tiHuanShuZuWeiZhi(i, j, "下", arrays); } else { tiHuanShuZuWeiZhi(i, j, "左", arrays); } } // 左下 if (i == arrays.length - 1 && j == 0) { // 根據(jù)生成的隨機(jī)數(shù)確定向左邊邊移動還是向下移動 if (ouShuOrJiShu()) { tiHuanShuZuWeiZhi(i, j, "上", arrays); } else { tiHuanShuZuWeiZhi(i, j, "右", arrays); } } // 右下 if (i == arrays.length - 1 && j == arrays[i].length - 1) { // 根據(jù)生成的隨機(jī)數(shù)確定向左邊邊移動還是向下移動 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; } } } } }
該方法實現(xiàn)對0的位置和需要替換位置數(shù)據(jù)的替換,并對0范圍進(jìn)行驗證,怕0出現(xiàn)數(shù)組下標(biāo)越位。
/** * 根據(jù)輸入的數(shù)據(jù),對二維數(shù)組進(jìn)行數(shù)據(jù)替換 * * @param i 高位坐標(biāo) * @param j 地位坐標(biāo) * @param direction 移動方向 * @param arrays 需要交換數(shù)據(jù)的數(shù)組 */ 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("挑戰(zhàn)成功"); } // /** // * 判斷當(dāng)前坐標(biāo)是否可以移動 // * // * @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 需要驗證的數(shù)據(jù) * @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; } // 遍歷二維數(shù)組 public static void bianLi(int[][] arrays) { for (int[] is : arrays) { for (int is2 : is) { System.out.print(is2 + "\t"); } System.out.println(); } } // 二維數(shù)組隨機(jī)打亂 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) { // 根據(jù)生成的隨機(jī)數(shù)確定向右邊邊移動還是向下移動 if (ouShuOrJiShu()) { tiHuanShuZuWeiZhi(i, j, "下", arrays); } else { tiHuanShuZuWeiZhi(i, j, "右", arrays); } } // 右上 if (i == 0 && j == arrays[0].length - 1) { // 根據(jù)生成的隨機(jī)數(shù)確定向左邊邊移動還是向下移動 if (ouShuOrJiShu()) { tiHuanShuZuWeiZhi(i, j, "下", arrays); } else { tiHuanShuZuWeiZhi(i, j, "左", arrays); } } // 左下 if (i == arrays.length - 1 && j == 0) { // 根據(jù)生成的隨機(jī)數(shù)確定向左邊邊移動還是向下移動 if (ouShuOrJiShu()) { tiHuanShuZuWeiZhi(i, j, "上", arrays); } else { tiHuanShuZuWeiZhi(i, j, "右", arrays); } } // 右下 if (i == arrays.length - 1 && j == arrays[i].length - 1) { // 根據(jù)生成的隨機(jī)數(shù)確定向左邊邊移動還是向下移動 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; } } } } } /** * 判斷是否是偶數(shù) * * @return 偶數(shù)返回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; } /** * 根據(jù)輸入的數(shù)據(jù),對二維數(shù)組進(jìn)行數(shù)據(jù)替換 * * @param i 高位坐標(biāo) * @param j 地位坐標(biāo) * @param direction 移動方向 * @param arrays 需要交換數(shù)據(jù)的數(shù)組 */ 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; } } }
到此,相信大家對“Java二維數(shù)組怎么實現(xiàn)數(shù)字拼圖效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。