Java making Intelligent puzzle game principle and code _java

Source: Internet
Author: User

Today's whim, want to do a smart jigsaw game to coax girlfriend.

These features need to be implemented
First image customization
The second Sudoku customization, of course, I started thinking about 3*3 4*4 5*5, not using 3*5 sudoku.
Third, to achieve the function of automatic jigsaw, I believe we know that women play games are not very powerful, so this automatic jigsaw function has.

What other pause, ranking will not write!
Now the point is out.
To realize the automatic jigsaw puzzle seems to require a little high Oh! A computer can not be as human as can be:
To investigate the nature of the first

Jigsaw puzzle is actually the arrangement question:

The arrangement has this definition: in the arrangement of a 1,2,..., N, if the front and back position of a pair of numbers is the opposite of the order of magnitude, that is, the previous number is greater than the number in the following, then they are called a reverse sequence. The total number of reverse order in an arrangement is called the reverse number of this permutation. An even-numbered arrangement is called an even arrangement, and an odd order is called a singular arrangement. As in 2431, the 21,43,41,31 is in reverse order, and the reverse number is 4, which is even arranged.
Another definition: Swap the two numbers in an arrangement, and the parity of the permutations changes.
All of the above definitions are excerpted from the higher algebra.

The puzzle arrangement must be a couple arrangement. This can be found in my reference literature.
So my only puzzle is to achieve this!

Follow-up in writing

Reference: Http://en.wikipedia.org/wiki/Fifteen_puzzle

Automatic Jigsaw:

First the automatic jigsaw should have certain rules, according to my jigsaw experience, to complete the puzzle, the different areas use the puzzle rules are different, so:
My Bongtu is divided into 4 areas (if the BONGTU is n*n lattice)
First region: X coordinate range 0 to n-2,y coordinate range 0 to n-3
Second area: X coordinate n-1,y coordinates range 0 to n-3
Third area: X coordinate range 0 to n-3, y coordinate range n-2 and n-1
Fourth area: X coordinate range n-2 to n-1, y coordinate range n-2 and n-1; that's the last four.

Each region can be completed in accordance with the rules of its respective region

Puzzle.java

Import java.io.FileNotFoundException;
Import Java.io.PrintStream;
Import java.io.UnsupportedEncodingException;
 
Import Java.util.Random;
 public class Puzzle {Private Long step = 0;
 private int n = 6;//Sudoku base private int[][] puzzle;
 private int resetblock = 0;////blank block position private int whiteblockx;
  
 private int whiteblocky;
 The coordinates of the current block to be moved are the reset block private int resetblockx;
  
 private int resetblocky;
  
 Private Boolean isprint=false;
 Public puzzle () {init ();
  public puzzle (int n) {THIS.N = n;
 Init ();
  private void Init () {puzzle = new int[n][n];
   for (int y = 0; y < n; y++) {A for (int x = 0; x < n, x + +) {Puzzle[y][x] = x + y * n;
  } Whiteblockx = n-1;
  Whiteblocky = n-1;
  int times = 100;//number of disruptions, must be even Random Random = new Random ();
   while (Times > 0) {int x0 = Random.nextint (n);
   int y0 = Random.nextint (n);
   int x1 = random.nextint (n);
   int y1 = Random.nextint (n); if (x0!= x1 && y0!=y1) {//guaranteed to be even-sorted if (x0==n-1&&y0==n-1) | | (x1==n-1&&y1==n-1))
    {//The last one does not exchange continue;
    } times--;
    int t = puzzle[x0][y0];
    PUZZLE[X0][Y0] = puzzle[x1][y1];
   PUZZLE[X1][Y1] = t; }//int[][] p = {{22,9, 1, 5, 0, 25},{//33,23,20,26,18,21},{//6, 16,17,10,34,31},{//19,28,32,7, 3, 2
},{//11,4, 12,14,27,24},{//15,29,30,8, 13,35}};
 Puzzle = P; The public void sort () {for (int y = 0; y < n; y++) {for int x = 0; x < n x + +) {if (x = = N-1 && Amp
    y = = n-1) {//The last one is blank,} else {reset (x, y); Block reset move target location private void reset (int targetx, int targety) {//Find the current position of the reset block Initresetblock (TARGETX, Targ
  Ety);
   * * Reset order is from left to right, moving from top to bottom * Move first, then left to move * The current reset block, it will be reset position can be divided into four kinds of situation * 1, not on the rightmost line is not the bottom two lines * 2, the rightmost line x=n-1, but not the following two lines;
   * 3, the bottom two lines y=n-2, but not the rightmost line; * 4, even the rightmost line is the bottom two lines */if (Targetx < n-1 && Targety < n-2) {if (Resetblockx==targetx&&resetblo Cky==targety) {//position correct do not moveDynamic return;//exits recursive} resetblocktotarget (Targetx, targety); }else if (targetx==n-1 && targety < n-2) {//second case if (resetblockx==targetx&&resetblocky==targety) {//
  Position correct without moving return;//exit recursive} reset2 (Targetx, targety);
   }else if (Targetx < n-2 && Targety = = n-2) {//isprint=true;
   Reset3 (TARGETX);
  Return
   }else{Initresetblock (n-2, n-2);
   Resetblocktotarget (N-2, n-2);
   if (whiteblockx<n-1) {whiteblockright ();
   } if (whiteblocky<n-1) {whiteblockdown ();
   } if (whiteblockx==n-1&&whiteblocky==n-1) {return; } Reset (Targetx, targety);//recursive} private void Initresetblock (int targetx,int targety) {resetblock = Targetx + ta
  Rgety * N;
     for (int y = 0; y < n; y++) {for (int x = 0; x < n x + +) {if (puzzle[y][x] = Resetblock) {//X,y is the position of the reset block
     Resetblockx = x;
     Resetblocky = y;
    Break private void Reset3 (int targetx) {//if (targetx>=2) {//} initResetblock (Targetx, n-1);
   
  Resetblocktotarget (Targetx, n-2);
  Initresetblock (Targetx, n-2);
  Resetblocktotarget (targetx+1, n-2); L:while (!) (
    Whiteblockx==targetx && whiteblocky==n-1)) {if (whiteblocky<n-1) {whiteblockdown ();
   Continue l;
    } if (Whiteblockx>targetx) {whiteblockleft ();
   Continue l;
  } break;
  } whiteblockup ();
   
  Swapwhiteblockandcurrentblock (); if (puzzle[n-2][targetx]!=resetblock| |
   Puzzle[n-1][targetx]!= (Resetblock+n)) {//No Reset success//isprint=true;
   Swapwhiteblockandcurrentblock ();
   Reset3_0 ();
  Reset3 (TARGETX);
   }} private void Reset3_0 () {if (resetblockx<n-1) {whiteblockdown ();
   Whiteblockright ();
   Whiteblockright ();
   Whiteblockup ();
   Swapwhiteblockandcurrentblock ();
   Reset3_0 ();
  Return
 } return; The private void Reset2_3 () {if (Whiteblockx==resetblockx && whiteblocky==resetblocky+1) {return;//satisfies the condition, fallback The recursive}//white block may be in the reset block: Left, bottom, bottom if (whiteblocky==resetblocky) {Left-Hand Whiteblockdown ();
  }else if (Whiteblockx < Resetblockx) {//lower left whiteblockright ();
  }else {whiteblockup (); Reset2_3 ();//recursive} private void reset2_2 (int targetx, int targety) {if (resetblockx==targetx&&resetblocky ==targety) {//2, move the reset block to the target position directly below return;//exit recursive}//reset block possible position, target position left, directly below, lower left-hand if (RESETBLOCKX==TARGETX) {//directly below to move re
  Setblockup (Targetx, targety);
  }else{//to the left or lower left-hand side, first move to the right and then up Resetblockright (Targetx, targety); } reset2_2 (Targetx, targety);//recursive} private void Reset2 (int targetx, int targety) {if (resetblockx==targetx&&am P;resetblocky==targety) {//position correct do not move return;//exit recursion}/* 1, if the white block just occupied the target position: If the reset block just below, exchange and complete the reset, if the lower is not a reset block, the white block to remove the target position * 2,
   Move the reset block to the target position directly below * 3, remove the white block under * 4, move the reset block under the specified steps, and///First step if (whiteblockx==targetx&& whiteblocky==targety) {
    if (whiteblockx==resetblockx&&whiteblocky==resetblocky+1) {//reset block below Swapwhiteblockandcurrentblock ();
   Return
   }else{Whiteblockdown (); }
  }
  //The second step is to move the reset block directly below the target position reset2_2 (TARGETX, targety+1);
  The third step is to move the white block below the reset2_3 ();
  The fourth step is to reset Swapwhiteblockandcurrentblock () according to the prescribed steps;
  Whiteblockleft ();
  Whiteblockup ();
  Whiteblockright ();
  Whiteblockdown ();
  Whiteblockleft ();
  Whiteblockup ();
  Whiteblockright ();
  Whiteblockdown ();
  Swapwhiteblockandcurrentblock ();
  Whiteblockleft ();
  Whiteblockup ();
  Whiteblockup ();
  Whiteblockright ();
 Swapwhiteblockandcurrentblock (); } private void Resetblocktotarget (int targetx, int targety) {if (resetblockx==targetx&&resetblocky==targety) {
  Correct position without moving return;//exit recursive} if (resetblocky==targety) {//Positive left resetblockleft (Targetx, targety); }else{//bottom left, down, right down if (RESETBLOCKX&GT;=TARGETX) {//lower right | |
    (resetblockx==n-1) {//reset block to the right, first left, convenient to move up when the unified use of white block counterclockwise way Resetblockleft (Targetx, targety);
    }else{resetblockup (Targetx, targety);
   }else{//left and right resetblockright (Targetx, targety); } resetblocktotarget (Targetx, targety);//recursive} private void resetblockright (int targetx, int targety) {if (resetblockx==targetx&&resetblocky==targety) {//position correct without moving R
  eturn;//exit Recursive} if (resetblockx==n-1) {//reset block on rightmost, can't move right, exit return directly;
  }//System.out.println ("Resetblockright");
   if (whiteblocky<resetblocky) {//Top if (whiteblocky<resetblocky-1) {//Top multiline whiteblockdown ();
    }else{//Upper Line if (whiteblockx<resetblockx+1) {//upper left and Upper Whiteblockright ();
    }else{//right Upper Whiteblockdown (); }}else if (whiteblocky==resetblocky) {//the same line if (Whiteblockx<resetblockx) {//left if (whiteblocky==n-1) {//In the end, can only be
    Upward whiteblockup ();
    }else{Whiteblockdown ();
     }else{//Right if (whiteblockx==resetblockx+1) {swapwhiteblockandcurrentblock ();
    return;//exit recursive}else{whiteblockleft ();
   }}}else{//below if (Whiteblockx <= Resetblockx) {//lower left, Lower whiteblockright ();
   }else{//lower right whiteblockup (); } resetblockright (Targetx, targety);//recursive} private void ResetblOckleft (int targetx, int targety) {if (resetblockx==targetx&&resetblocky==targety) {//position correct do not move return;//exit recursion
  } if (resetblockx==0) {//On the left edge of the reset block cannot move left, direct exit recursive return;
  }//System.out.println ("Resetblockleft");
   if (whiteblocky<resetblocky) {//Top if (whiteblocky<resetblocky-1) {//Top multiline whiteblockdown ();
     }else{//Upper Line if (Whiteblockx==resetblockx) {//Top if (whiteblockx==n-1) {//rightmost, white block can not move to the right, can only move left whiteblockleft ();
      }else{if (resetblocky==n-1) {//reset block in the low-end, white block can not move clockwise whiteblockleft ();
      }else{whiteblockright (); }}else if (Whiteblockx>resetblockx) {//upper right if (resetblocky==n-1) {//reset block in the low-end, white block can not move clockwise Whiteblockleft
     ();
     }else{Whiteblockdown ();
    }}else{//Upper left Whiteblockdown (); }}else if (whiteblocky==resetblocky) {//left, right if (Whiteblockx<resetblockx) {//left if (Whiteblockx==resetblockx
    1) {//left one swapwhiteblockandcurrentblock ();//exit recursive return; }else{WHITeblockright (); }else{//Right if (whiteblocky==n-1) {//In the end, cannot move down.
    Can only move up whiteblockup ();
    }else{Whiteblockdown ();
    }else{//lower left, lower, lower right if (Whiteblockx<resetblockx) {//left lower if (whiteblockx==resetblockx-1) {whiteblockup ();
    }else{whiteblockright ();
   }else{//below, lower right whiteblockleft (); } resetblockleft (Targetx, targety);//recursive} private void Resetblockup (int targetx, int targety) {if (Resetblockx
  ==targetx&&resetblocky==targety) {//position correct do not move return;//exit recursive} if (resetblocky==0) {//reset block to top, cannot move return;
  }//System.out.println ("Resetblockup");
   if (Whiteblocky < Resetblocky) {//Top if (Whiteblocky < resetBlockY-1) {//Top multiline whiteblockdown ();
     }else{//Upper Line if (Whiteblockx = = Resetblockx) {//Bai and reset block in the same column (vertical column) white block and reset block Direct Exchange position swapwhiteblockandcurrentblock ();/exit recursion
    Return 
     }else{if (Whiteblockx<resetblockx) {//Bai to the left of the reset block, the white block to the right whiteblockright (); }else{//white block on the right side of the reset block, white block left shift WHiteblockleft (); else if (Whiteblocky = = Resetblocky) {//Bai and reset block the same row; the white block moves the IF (Whiteblockx<resetblockx) {//Positive left if (whit
    EBLOCKX&LT;RESETBLOCKX-1) {//right-left multi lattice whiteblockright ();
     }else{//the left one lattice if (whiteblocky==n-1) {//Whiteblockup ();
      }else {if (resetblockx==n-1) {//reset block on the far right, not counterclockwise, only move the white block whiteblockup ();
      }else{Whiteblockdown ();
   }}}else{//right Whiteblockup (); }else{//White block under the reset block, white pieces need to be spared the reset block up, white block counterclockwise around to the white block above///three cases: lower left, lower, right lower if (Whiteblockx<=resetblockx) {//lower left, lower; white block right shift
     blockx==n-1) {//reset block on the rightmost, not counterclockwise, only the CIS-pointer move white block if (Whiteblockx==resetblockx) {//directly below Whiteblockleft ();
     }else{//the lower left Whiteblockup ();
    }}else{whiteblockright ();
   }else{//lower right; white block up whiteblockup ();
  } resetblockup (Targetx, targety);//recursive}//Bai and reset block interchange location private void Swapwhiteblockandcurrentblock () {step++;
  int tempx = Whiteblockx,tempy = Whiteblocky; int temp = Puzzle[wHiteblocky][whiteblockx];
  Puzzle[whiteblocky][whiteblockx] = Puzzle[resetblocky][resetblockx];
  Puzzle[resetblocky][resetblockx] = temp;
  Whiteblockx = Resetblockx;
  Whiteblocky = Resetblocky;
  Resetblockx = tempx;
  Resetblocky = Tempy;
 println ("swap");
  private void Whiteblockdown () {step++;
  int temp = Puzzle[whiteblocky][whiteblockx];
  Puzzle[whiteblocky][whiteblockx] = Puzzle[whiteblocky+1][whiteblockx];
  Puzzle[whiteblocky+1][whiteblockx] = temp;
  whiteblocky++;
 println ("↓");
  private void Whiteblockup () {step++;
  int temp = Puzzle[whiteblocky][whiteblockx];
  Puzzle[whiteblocky][whiteblockx] = Puzzle[whiteblocky-1][whiteblockx];
  Puzzle[whiteblocky-1][whiteblockx] = temp;
  whiteblocky--;
 println ("↑");
  private void Whiteblockleft () {step++;
  int temp = Puzzle[whiteblocky][whiteblockx];
  Puzzle[whiteblocky][whiteblockx] = puzzle[whiteblocky][whiteblockx-1];
  PUZZLE[WHITEBLOCKY][WHITEBLOCKX-1] = temp;
  whiteblockx--;
 println ("←");
 }private void Whiteblockright () {step++;
  int temp = Puzzle[whiteblocky][whiteblockx];
  Puzzle[whiteblocky][whiteblockx] = puzzle[whiteblocky][whiteblockx+1];
  PUZZLE[WHITEBLOCKY][WHITEBLOCKX+1] = temp;
  whiteblockx++;
 println ("→");
  @Override public String toString () {StringBuilder sb = new StringBuilder ();
  Sb.append ("resetblock=" ("+resetblock+", "+resetblockx+", "+resetblocky+") \ n ");
   if (puzzle!=null) {int len = string.valueof (n*2-1). Length ();
     for (int y = 0; y < n; y++) {for (int x = 0; x < n. x +) {if (x>0) {sb.append (",");
    } sb.append (_str (string.valueof (puzzle[y][x), Len));
   } sb.append ("\ n");
  } sb.append ("---------------------------------------");
  }else{sb.append ("Puzzle is null");
 return sb.tostring ();
  private string _str (string Str,int len) {str=str==null? ": str;
  if (Str.length () <len) {return _str (str+ "", Len);
 return str; } private void println (String str) {if (IsPrint) {System.out.println (str);
  System.out.println (this); } public static void Main (string[] args) throws FileNotFoundException, unsupportedencodingexception {//System.set
  Out (New PrintStream ("E:/puzzle.txt", "UTF-8"));
  Puzzle p = new Puzzle ();
  SYSTEM.OUT.PRINTLN (P);
  try {p.sort ();
   catch (Exception e) {e.printstacktrace ();
  System.out.println ("Exception:");
  }finally{System.out.println (P); }
 }
}

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.