Start with jigsaw Puzzle (i) _ Simple Java Simulation

Source: Internet
Author: User

       See Android Today, see an open Source Library Android-flipview, and then look at it and go around the jigsaw puzzle, and now I'm going to write a simple jigsaw puzzle game. This will be my second series of articles, today is the first day, the simple Java command line to simulate the realization of the puzzle game. Code:

Package Com.wly.algorithmproblem;

Import Java.util.Scanner;
	/** * Simple Simulation of jigsaw puzzle * @author wly * */public class Picgame {private static int COLUMN = 4;
	
	private static int[][] Elems = new Int[column][column];
		
		public static void Main (string[] args) {int elemsnum = elems.length*elems.length-1; 
			for (int i=0;i<elems.length;i++) {for (int j=0;j<elems[0].length;j++) {Elems[i][j] = i*elems.length + j; //1. Generate random data, random 22 exchange for (int i=0;i<elems.length;i++) {for (int j=0;j<elems[0].length;j++) {int R
				Andom = (int) (Math.random () * elemsnum);
			Exchangeelems (elems,i,j,random/elems.length,random%elems.length);
		
		} printmatrix (Elems);
			while (true) {System.out.print ("Enter X and y:");
			Scanner Scanner = new Scanner (system.in);
			int x = Scanner.nextint ();
			int y = Scanner.nextint ();
			Move (x, y);
		Printmatrix (Elems); The public static void move (int line,int column) {//Check argument legality if (line < 0 | | line > (elems.lengTH-1) | | Column < 0 | |
			Column > (elems.length-1)) {System.out.println ("Incorrect input data, no longer valid range!");
		Return
				} if (line > 0) {//Up if (elems[line-1][column] = = 0) {exchangeelems (Elems, line, column, line-1, column);
			Return } if (line < (elems.length-1)) {//Next if (elems[line+1][column] = = 0) {exchangeelems (Elems, line, column, L
				ine+1, column);
			Return } if (Column > 0) {//left if (elems[line][column-1] = = 0) {exchangeelems (Elems, line, column, line, column-1
				);
			Return  } if (column < (elems.length-1)) {//Right if (elems[line][column+1] = = 0) {exchangeelems (Elems, line, column,
				line, column+1);
			Return
		} System.out.println ("Cannot move current element");
	Return /** * Swap two elements adjacent to a two-dimensional array * @param array * @param x1 * @param y1 * @param x2 * @param y2/private Stati
		c void Exchangeelems (int[][] array,int x1,int y1,int x2,int y2) {int temp = array[x1][y1]; ARRAY[X1][Y1] = Array[x2][y2];
		ARRAY[X2][Y2] = temp; /** * Print two-dimensional matrix * @param matrix/private static void Printmatrix (int[][) matrix {for (int i=0;i<matrix.le
			ngth;i++) {for (int j=0;j<matrix[0].length;j++) {System.out.print (Matrix[i][j] + "");
		} System.out.println (); }
	}
}

Operation Effect:

8 5 6 1 7 0 4 3 9 2 
x and y:2 0 ( 
8 5) 6 1 7 0 (4 3) 9 2 Input x and y:1 0
8 5 0 6 1 7 a 4 3 9 2 
input x and y:0 0
0 8 5 6 1 7 ten 4 3 9 2 
enter x and y:

Things are simple, there is nothing to say, just want to do a simple opening. It is planned to spend 5 days discussing the algorithmic problem of jigsaw puzzle. Then use the Android version of the puzzle for about 5 days. Please look forward to it!

Note: The randomly generated puzzle here is just a simple analog matrix, and in fact the resulting puzzle is about%50, which will be covered in the next blog post.

o ~ ~ ~ ~

Reprint please retain the Source: http://blog.csdn.net/u011638883/article/details/17099617

Thank you!!

Related Article

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.