Implement the 9-square-lattice puzzle by Using J2EE

Source: Internet
Author: User
The experiment requires that the jiugongge jigsaw puzzle game be moved up and down the left and right keys using textures. Program ideas

To divide an image into three rows and three columns for texture, you must first disrupt the order, but ensure that the bottom right corner is blank. Then, you can move the picture up, down, and left, and exchange the blank space with the surrounding area. If you finally reach the complete picture, the game will end and win.

The key is whether a piece of puzzle can be restored if the order is disordered at will. This is not necessarily true. The online materials show that the order of texture labels is arranged into a column, and the last position is 0. If the parity of the two sequences is the same, one graph can get the other. The expected game result is a sequence of {1, 2, 3, 4, 5, 6, 7, 8, 0}. If the reverse order number is an even number, you can determine whether the reverse order number of the randomly generated puzzle is an even number.

I used a random number to disrupt the puzzle. I used an array to save this sequence. Apart from the last digit, that is, the number of subscript 8, I randomly obtained numbers ranging from 0 to 7 and switched positions, achieve disruption. If the puzzle cannot be resolved, it will be disrupted again, five times at a time.

In gamecanvas, a thread cyclically checks whether there are top, bottom, left, right, and so on, and re-maps the image. Each time, it also determines whether it wins. If it wins, it switches to the end interface. Advisory link: puzzle ~ 8. The input is random and requires that only 0 and one number on the edge be exchanged at a time to arrange the required numbers.
For example:
The random Real Number Matrix is:
1 3 5
0 2 6
4 7 8 0, can be exchanged with 1, 2, 4
Eventually becomes the target matrix
1 2 3
8 0 4
7 6 5
The exchange between 0 and other numbers is unlimited.
Q:
If a digital matrix is given randomly, can it prove that, after an infinite Exchange, the target matrix can be reached or cannot be achieved through an infinite Exchange?

This is a 3*3 digital puzzle game;

Consider a more general problem: for the Number Matrix of two random M * n arrays (the two matrices contain the same elements, both of which are 0 -- M-1 but with different order) how can a and B determine whether a can go through a series of legal moves (only 0 and one number on the edge can be exchanged at a time) and be converted to B;

This is a classic problem. It is also early to determine the existence of solutions for digital puzzle games by the parity of reverse order numbers;

I did not find a detailed proof, so I tried to give a general proof. please correct me!

Definition:

Sequence: The M * n elements in the M * n numeric matrix are arranged in the following ways: (M * n )! After a sort rule is specified, the M * n numeric matrix can get a corresponding sequence (the sequence contains element 0 );

The sequence mentioned in the following discussion assumes that the same sorting rule is followed, but not limited to any specific sorting rule;

1: Any two elements of the exchange sequence, the parity of the reverse Number of the sequence must change;

This is the theorem in the textbook and will not be repeated;

From Theorem 1, the exchange changes the parity once, and then the exchange changes the parity again, so the two consecutive exchanges, the parity of the number of reverse orders of the sequence must remain unchanged; so there are

Inference 1: The sequence is exchanged for an even number of times, and the parity of the reverse Number of the sequence remains unchanged;

Inference 2: "For any M * n numeric matrix, after any K legal moving, if 0 returns to the original position, the parity of the Reverse Sequence pair remains unchanged"
1) because each legal move is performed with 0, the total number of moves = 0 is the total number of moves;
2) For the number 0, because its position remains unchanged, it must be moved between the left and right of an even number, and up and down of an even number of times;
From inference 1, so inference 2 is proved;

Inference 3: "For any M * n numeric matrix, assume that after multiple legal moves, the positions of two non-zero elements are exchanged, and the positions of other elements remain unchanged, it must be exchanged between the left and right sides of the zero even number of times, and between the upper and lower Switches of the even number of times ";
Since the positions of 0 before and after the exchange remain unchanged, we obtain the result from inference 2 and inference 3;

However, from the Theorem 1: Any two elements of the exchange sequence, the parity of the number of reverse orders of the sequence must change;

This is in conflict with inference 3, so the assumption of inference 3 does not exist;

Inference 4: "For any M * n numeric matrix, it cannot be legally moved any time, so that two non-zero elements are exchanged, and the positions of other elements remain unchanged "; that is, if A and B have only two non-0 elements, the positions of other (including 0) elements are the same, then a cannot be converted to B through legal movement;

Next we will discuss a specific mobile policy;
For any M * n matrix, assume that the Row M> = column N;
Assume that the source State Matrix is a and the target State Matrix is B;

1) always move 0 of the target State B to the upper left corner;
Record the new target status as B '. Because every step is reversible, you can move from B to B'. Then, you can certainly move from B 'to B;
Therefore, whether a can move to B is equivalent to whether a can move to B ';
2) If M> N, you can always move a so that the elements and positions of the last row of a are identical to those of B;
Assume that the elements in the last line of B 'are from left to right, which is B' [1. N];
A) For B '[1] In a, you can always move this element to the top of the target position, and then move it to 0 in sequence; then, exchange B '[1] and 0 to move B' [1] In a to the target location;
B) assuming that the first I element of the last row of a has been placed, the same method can be used for the I + 1 <n elements as, move B '[I + 1] In a to the target location;
C) For B '[N] In a, first move it to the reciprocal 2nd column of the last 2nd rows of A, and then move 0 to the top of the B' [1] In, then bind B '[1 .. n-1] Move one position clockwise. Move B '[N] down, And then B' [1 .. n] Each moves a position counterclockwise;
This completes "moving a makes the elements and positions of the last row of a exactly the same as those of B"
The child matrix composed of M-1 row of A is A', M-1 row of B 'is the new B'

Then execute it cyclically 2) until the number of rows in a' is the same as the number of columns;

3) if the number of rows of a' is the same as the number of columns, then, you can always move a' so that the last row of a' and the last row of the last column and the last row of B are exactly the same element and position;
(2 );

Then execute it cyclically 3) until the number of rows and columns of A' are both 2;

4) for the matrix a' and B 'of 2*2, if the element at the diagonal position of 0 in B' is C, it can always follow the forward (or inverse) the hour hand moves A', C moves to the correct position, and then 0 to the correct position;
Then we can get two different states;
A) if the elements in the other two positions of A' are exactly the same as those in the corresponding positions of B ', that is, A' = B ', note that a' is B1 (b1 = A' = B ');
The source State Matrix A can be moved to the target State matrix B according to the reversible movement;
B) if the elements in the other two positions of A' are different from those in the corresponding positions of B ', the elements in the two positions are reversed, and A' in this case is B2;

Compared with B2, B1 has the same position as other elements except the two-position element interchange;
From inference 4, we can see that B1 and B2 cannot be converted to each other;

So there are
Conclusion 5: For any specific target State B, all source State A can be divided into two different types, one can be converted to B1 (that is, a can be converted to B ), one type can be converted to B2 (that is, a cannot be converted to B );
Move 0 in source status A to the upper left corner to get a new A'. The positions of 0 in A and B are the same;
For A', either it can be converted to B1 or B2. However, the positions of A', B1, and B2 0 are the same, and inference 2: the parity of the reverse order pairs of their sequences remains unchanged; the parity of the reverse order pairs of B1 and B2 is different;

So there is a final conclusion:

Normalize source status A and target status B so that the element 0 of the two matrices is at the same position. It is recorded as the new source status a' and target status B ';
If the parity of the reverse order of a' and B 'is the same (that is, the parity of the reverse order of a' and B1 is the same), then a' may be converted to B ', that is, a can be converted to B;
If the parity of the reverse order of a' and B 'is different (that is, the parity of the reverse order of a' and B2 is the same), then A' must not be converted to B ', that is, a cannot be converted to B.

1. In statement T1 =NewTiledlayer (9, IMG, IMG. getwidth ()/3, IMG. in getheight ()/3);, if the last two parameters are not divisible, an error occurs in Java. lang. illegalargumentexception. Therefore, the size of the image must be modified by 210.

2. In jdk1.3, the integer type is not encapsulated, and the element of the vector is of the object type. Therefore, the int type cannot be put in. In this case, a random value is obtained and deleted, values are randomly obtained in sequence and ordered textures are then implemented using the array exchange.

3. t1.paint (g) was used for re-painting, so that even if we used to fill the screen with a rectangle, we could not achieve the goal of cleaning the screen. Later, we used the repainting method of the mygamecanvas object for reference, place the Paster label value in the paint function to clear and update the image.

Source Code address

Http://download.csdn.net/detail/felicitia/5317955

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.