Implementation of jigsaw puzzle on Java console

Time:2022-5-22

This example shares the specific code of Java console to realize jigsaw puzzle for your reference. The specific contents are as follows

1. Firstly, the original puzzle is moved irregularly for many times to disrupt the puzzle

2. Then play the game and judge the puzzle order while the game moves

① If the puzzle is successful, jump out of the loop and end the game;
② If the puzzle fails and falls into a dead circle, continue to move the puzzle until it succeeds.

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

public class GrameOfPingTuTest{

 private static Scanner scanner = new Scanner(System.in);

 private static int h = 2;

 private static int l = 2;

 private static int x = 0;

 private static int y = 0;

 private static Random random = new Random();


 public static void main(String[] args) {
   int[][] youxi = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  initGrame(youxi);
  System. out. Println ("game starts!");
  playGrame(youxi);
 }

 /**
  *Start playing games
  */
 public static void playGrame(int[][] a) {
  int c = 0;
  while (true) {
   printfResult(a);
   c=event();
   houQuXY(c,a);
   If (isDon (x, y)) {// first judge whether it can move and start moving the puzzle
    yiDon(x, y,a);
    if (resultOfGrame(a)) {
     System. out. Println ("the game is finished, congratulations on your victory!!");
     printfResult(a);
     break;
    }
    continue;
   } else {
    System. out. Println ("unable to move, please re-enter the moving number!");
    continue;
   }
  }
 }

 /**
  *Completion of printing jigsaw puzzle
  * @param a
  */
 public static void printfResult(int[][] a) {
  for (int[] i : a) {
   for (int j : i)
    System.out.print((j != 9 ? j : " ") + "\t");
   System.out.println();
  }
 }

 /**
  *Initialize game
  *@ param disrupt the puzzle
  */
 public static void initGrame(int[][] a) {
  int m = 1;
  while (m <= 100) {
   while (true) {
    x = runNum(3);
    y = runNum(3);
    if (isDon(x, y))
     break;
   }
   yiDon(x, y, a);
   m++;
  }
 }

 /**
  *Judge whether the game is successful
  *@ return true: game victory
  */
 public static boolean resultOfGrame(int [][] a) {
  int c = 1;
  for (int[] b : a)
   for (int i : b)
    if (i != c++)
     return false;
  return true;
 }

 /**
  *Mobile puzzle
  */
 public static void yiDon(int x, int y,int[][] a) {
  int m = a[h][l];
  a[h][l] = a[x][y];
  a[x][y] = m;
  h = x;
  l = y;
 }

 /**
  *Judge whether it can move
  */
 public static boolean isDon(int x, int y) {
  if (h == x && Math.abs(l - y) == 1)
   return true;
  if (l == y && Math.abs(h - x) == 1)
   return true;
  return false;
 }

 /**
  *Generate a random integer of [0, a)
  */
 public static int runNum(int a) {
  return random.nextInt(a);
 }

 /**
  *Gets the coordinates of the number to move
  */

 public static void houQuXY(int c,int[][] a) {
  for (int i = 0; i < a.length; i++)
   for (int j = 0; j < a[i].length; j++)
    if (c == a[i][j]) {
     x = i;
     y = j;
    }
 }

 /**
  *The keyboard receives the number to move
  * @return 1~8
  */
 public static int event() {
  System. out. Println ("please enter the number to be moved");
  String output = scanner. nextLine();//  Input failed overwrite input
  If (output. Length() = = 1) {// only one character can be entered
   char s = output. charAt(0);//  Only 1 ~ 8 can be entered
   if (s > '0' && s < '9')
    return (Integer.parseInt(output));
  }
  System. out. Println ("illegal input, please re-enter!!");
  return event();
  }
 }

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.