Realization of digital puzzle effect by Java two dimensional array
- 2021-11-02 01:09:48
- OfStack
2-D array to achieve digital puzzle, for your reference, the specific contents are as follows
2-D array can define its own size at will, through method judgment to achieve random disruption of all numbers, and can be correctly restored by moving, and can judge whether 0 (representing space) can be moved or not, whether it is within the range.
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(" Please enter the moving direction (W Go, S Under, A Left, D Right )");
direction = scanner.next();
switch (direction) {
case "W":
case "w":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Upper ", arrays);
break;
case "S":
case "s":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Under ", arrays);
break;
case "A":
case "a":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Left ", arrays);
break;
case "D":
case "d":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Right ", arrays);
break;
default:
System.out.println(" Illegal input, re-input ");
break;
}
}
System.out.println("1 Gone together " + sum + " Step ");
System.out.println(" Challenge success ");
}
Judge whether the current coordinates can be moved
/**
* Judge whether the current coordinates can be moved
*
* @param arrays
* @return You can move back 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;
}
Gets the specific address of the row where the current 0 is located
// Get 0 The position of the row on which it is located
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;
}
Gets the specific address of the column where the current 0 is located
// Get 0 The position of the column in which it is located
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;
}
2-D arrays are randomly disturbed. It is necessary to judge the upper left corner, upper right corner, lower left corner, lower right corner, middle corner, upper middle corner, lower middle corner, left middle corner and right middle corner, which directions can be moved, and generate random numbers to determine the moving direction
// 2 Random scrambling of dimension array
public static void daLuanErWeiShuZu(int[][] arrays) {
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
// Upper left
if (i == 0 && j == 0) {
// Determine whether to move to the right or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
}
}
// Upper right
if (i == 0 && j == arrays[0].length - 1) {
// Determine whether to move to the left or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
}
}
// Lower left
if (i == arrays.length - 1 && j == 0) {
// Determine whether to move to the left or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
}
}
// Lower right
if (i == arrays.length - 1 && j == arrays[i].length - 1) {
// Determine whether to move to the left or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
}
}
// Upper middle
if (i == 0 && j > 0 && j < arrays[i].length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
default:
break;
}
}
// Left center
if (j == 0 && i > 0 && i < arrays.length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
break;
default:
break;
}
}
// Lower middle
if (i == arrays.length - 1 && j > 0 && j < arrays[i].length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
default:
break;
}
}
// Right center
if (j == arrays[i].length - 1 && i > 0 && i < arrays[i].length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Under ", 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, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
break;
case 3:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
default:
break;
}
}
}
}
}
This method realizes the replacement of the position of 0 and the position data that needs to be replaced, and verifies the range of 0, for fear that the array subscript of 0 will be offside.
/**
* According to the input data, the 2 Dimensional array for data replacement
*
* @param i High position coordinates
* @param j Position coordinate
* @param direction Moving direction
* @param arrays Array of data to be exchanged
*/
public static void tiHuanShuZuWeiZhi(int i, int j, String direction, int[][] arrays) {
int tem = -1;
switch (direction) {
case " Upper ":
if (i > 0) {
tem = arrays[i][j];
arrays[i][j] = arrays[i - 1][j];
arrays[i - 1][j] = tem;
}
break;
case " Under ":
if (i < arrays.length - 1) {
tem = arrays[i][j];
arrays[i][j] = arrays[i + 1][j];
arrays[i + 1][j] = tem;
}
break;
case " Left ":
if (j > 0) {
tem = arrays[i][j];
arrays[i][j] = arrays[i][j - 1];
arrays[i][j - 1] = tem;
}
break;
case " Right ":
if (j < arrays.length - 1) {
tem = arrays[i][j];
arrays[i][j] = arrays[i][j + 1];
arrays[i][j + 1] = tem;
}
break;
default:
break;
}
}
The complete code is as follows
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(" Please enter the moving direction (W Go, S Under, A Left, D Right )");
direction = scanner.next();
switch (direction) {
case "W":
case "w":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Upper ", arrays);
break;
case "S":
case "s":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Under ", arrays);
break;
case "A":
case "a":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Left ", arrays);
break;
case "D":
case "d":
tiHuanShuZuWeiZhi(returnX(arrays), returnY(arrays), " Right ", arrays);
break;
default:
System.out.println(" Illegal input, re-input ");
break;
}
}
System.out.println("1 Gone together " + sum + " Step ");
System.out.println(" Challenge success ");
}
// /**
// * Judge whether the current coordinates can be moved
// *
// * @param arrays
// * @return You can move back 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 Data to be validated
* @return Successful 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;
}
// Get 0 The position of the row on which it is located
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;
}
// Get 0 The position of the column in which it is located
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;
}
// Traversal 2 Dimensional array
public static void bianLi(int[][] arrays) {
for (int[] is : arrays) {
for (int is2 : is) {
System.out.print(is2 + "\t");
}
System.out.println();
}
}
// 2 Random scrambling of dimension array
public static void daLuanErWeiShuZu(int[][] arrays) {
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
// Upper left
if (i == 0 && j == 0) {
// Determine whether to move to the right or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
}
}
// Upper right
if (i == 0 && j == arrays[0].length - 1) {
// Determine whether to move to the left or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
}
}
// Lower left
if (i == arrays.length - 1 && j == 0) {
// Determine whether to move to the left or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
}
}
// Lower right
if (i == arrays.length - 1 && j == arrays[i].length - 1) {
// Determine whether to move to the left or down according to the generated random number
if (ouShuOrJiShu()) {
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
} else {
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
}
}
// Upper middle
if (i == 0 && j > 0 && j < arrays[i].length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
default:
break;
}
}
// Left center
if (j == 0 && i > 0 && i < arrays.length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
break;
default:
break;
}
}
// Lower middle
if (i == arrays.length - 1 && j > 0 && j < arrays[i].length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
default:
break;
}
}
// Right center
if (j == arrays[i].length - 1 && i > 0 && i < arrays[i].length - 1) {
switch (oneToThree(3)) {
case 0:
tiHuanShuZuWeiZhi(i, j, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Under ", 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, " Upper ", arrays);
break;
case 1:
tiHuanShuZuWeiZhi(i, j, " Right ", arrays);
break;
case 2:
tiHuanShuZuWeiZhi(i, j, " Under ", arrays);
break;
case 3:
tiHuanShuZuWeiZhi(i, j, " Left ", arrays);
break;
default:
break;
}
}
}
}
}
/**
* Judge whether it is an even number
*
* @return Even number return true
*/
public static boolean ouShuOrJiShu() {
return new Random().nextInt(1000) % 2 == 0 ? true : false;
}
/**
*
* @param n Requires the value of modulus
* @return Return 0-(n-1) Value of
*/
public static int oneToThree(int n) {
return new Random().nextInt(1000) % n;
}
/**
* According to the input data, the 2 Dimensional array for data replacement
*
* @param i High position coordinates
* @param j Position coordinate
* @param direction Moving direction
* @param arrays Array of data to be exchanged
*/
public static void tiHuanShuZuWeiZhi(int i, int j, String direction, int[][] arrays) {
int tem = -1;
switch (direction) {
case " Upper ":
if (i > 0) {
tem = arrays[i][j];
arrays[i][j] = arrays[i - 1][j];
arrays[i - 1][j] = tem;
}
break;
case " Under ":
if (i < arrays.length - 1) {
tem = arrays[i][j];
arrays[i][j] = arrays[i + 1][j];
arrays[i + 1][j] = tem;
}
break;
case " Left ":
if (j > 0) {
tem = arrays[i][j];
arrays[i][j] = arrays[i][j - 1];
arrays[i][j - 1] = tem;
}
break;
case " Right ":
if (j < arrays.length - 1) {
tem = arrays[i][j];
arrays[i][j] = arrays[i][j + 1];
arrays[i][j + 1] = tem;
}
break;
default:
break;
}
}
}