Flash games-See the algorithm big reveal

Source: Internet
Author: User
Tags define expression integer polyline
Algorithm

The topic is a bit exaggerated, because the game is a relatively simple game.

However, the technical issue of this simple game is the basis of a class of games such as Tetris, card games and so on. Through the analysis of this game algorithm, especially to check the connection of the discussion, we can basically master this kind of game algorithm basic thinking method.

The rules of the game are very simple, is the point of two matching and can pass no more than two fold points of the line together after the box, the two squares can be eliminated. Therefore, the following three kinds of cases can be eliminated. In the author's game, the pairing rule is two number plus equals 100.

Paired check is relatively simple, as long as the use of an if statement, conditions a+b==100 can be tested. And whether the two blocks can realize the polyline connection, it is a relatively complicated problem. The author here to the whole idea to tell you. The square looks like a chessboard, From one lattice to another it is easy for the author to associate with the square problem which is often encountered in the primary school Mathematics Olympiad. Although the primary school problem, but the difficulty for the author is still quite large, now, those questions to answer the idea, I probably have forgotten. However, as a computer, you can try to get the program to list all the ways out, And then directly into the condition to test. This algorithm is roughly as follows: Let a point go in all directions, and go to the obstacle. Then, the point that passes through, and then goes on in another direction. This uses an array to identify the relative position of each block.

Here, the author thought has been prepared, only owed to achieve. And, the advantage of this is that portability is strong. It can be in other programs related to the point-and-click Connection. In this game, you can have an algorithm to achieve a different number of discount line inspection, and can be added with the minus. So, I began to write a pen.

After it was written, the test found that the algorithm was not implemented in Flash, the reason is that recursion is more than 256, and the Phalanx is only 5*5 (the Edge is 7*7). The author draws a 5*5 square in the draft book, tries to let the lower left point go to the top right point, and the fold number of the polyline does not exceed 2

The figure can be found indeed good horror, the road to go more than N, and some roads will intersect, at the intersection of the time, we have to check which line of the fold less. And there are a lot of ways it's impossible to make it. So, it leads to high recursion (and a lot of times if you use loops).

This can be a rigorous mathematical method to exclude most of the road, but the author of the math is bad, and in fact, there is no such need. In the Flash game production, technology is to achieve the rules of service, we should not pass the rules to show the technical content of the game works.
So, here, with a very intuitive and well understood algorithm, to realize the connection test. This algorithm, I would like to call it a stepwise algorithm. Just see if you can connect straight, if not, turn a corner again, no, no, go on. This also matches the way some players judge the connection when they play the game.

Here, the author defines whether the connection was successfully defined as a function checknobarrier (x1,y1,x2,y2), True indicates a successful connection, and False indicates that the connection failed. Therefore, it is generally faster to return true than to return false. Because as long as a polyline is connected, You can exit the function. False, however, will fail all attempts to return (of course, if you first examine some special circumstances, such as a block tightly surrounded by the obstacles, the return of false is faster, but these cases are also included in the general test later, so the code will appear scattered, However, if the pursuit of operational efficiency, the use of the law is also not possible.

First-level connection test, the so-called first-level connection test, is to check whether the two blocks can be straight line connection, one of the necessary conditions is two blocks of the horizontal axis or ordinate equal, otherwise, immediately leave first-level inspection, into the two-level test.

The following is an example of the horizontal axis, which shows the algorithm of the first-order connection test. If the horizontal axis is equal, it is necessary to examine whether there are any obstructions in these points between the Y1 and the Y2, which can be examined by cyclic method. If there are no obstructions, the Checknobarrier returns true immediately, and the function exits immediately. Otherwise, you need to go to level two test.

The ordinate test method is similar.

One of the things that can easily be overlooked here is that two squares are adjacent. At this time, there is no point between Y1 and Y2. If you do not test, you will mistakenly jump to the level two test. In order to avoid this situation, you can define a temporary variable temp=true before the loop, and then the Loop statement, Meet the obstacles and let the temp=false, and jump out of the loop. At this point, the temp is absolutely true for adjacent situations because the number of loops is 0.

To sum up, Temp=true,checknobarrier returns True, otherwise to enter two level test.

Second-level connection test

First-level inspection, found that two blocks can not be connected through a straight line, then try a folding point of the line can be two blocks connected together, this is the level two test.

The second-order test, after excluding the condition of the first level test, has no requirement for the coordinate relationship between the two squares. So, if the horizontal axis or ordinate is equal, then directly into the three-level test.

In Figure 10, if you want the two yellow squares in the diagram to successfully achieve a level two connection, there must be no obstructions in the place where the red or blue line passes.

In Figure 10, the red line is made up of two segments, so this test is equivalent to the "and" operation of two first-level tests. It can be seen that the first-level test method could be used here. Therefore, it is more convenient to define a class I test as a function in two-level test.

The first level test function is defined as Checky1 (x,y1,y2) when the horizontal axis is equal, and the first level test function of the ordinate equality is defined as CHECKX1 (x1,x2,y)

The result of checking the red line is then determined by the following expression:

(Checky1 (x1,y1,y2+1) && checkx1 (x1-1,x2,y2))

The expression returns True, then Checknobarrier returns True, otherwise the blue line can be checked for connections:

(Checky1 (X2,y1-1,y2) && checkx1 (x1,x2+1,y1))

The reader is expected to notice the addition and subtraction. This problem is easily overlooked. As for why, please carefully observe the distribution characteristics of the points to be tested in the first-grade test.

The expression returns True, then Checknobarrier returns True. Otherwise, the two test result is false; To enter level three test
Therefore, the following expression of the true and false decision does not need to enter the level three test.

(Checky1 (x1,y1,y2+1) && checkx1 (x1-1,x2,y2)) | | (Checky1 (X2,y1-1,y2) && checkx1 (x1,x2+1,y1))

The two green blocks in Figure 10 are analyzed in similar ways.

Three-level connection test

With two squares, a polyline with a fold number of 2 has many bars, if the two-level test as a direct decomposition into a first-level test, the number of cycles is still more, and need to read a point multiple times (such as the red line in Figure 11 and the blue lines will repeat three points, that is, the yellow part of the mark). So, The first level test from the square is replaced by other methods.

It can be found that all polylines have a feature. Two fold points are equal or ordinate equal to the horizontal axis of the two squares respectively.

Therefore, we only need the middle of the fold line for a first-level test. As for the position of the folding point, let the two squares go in four directions, and go to the edge of the obstacle or the square. Thus, the points that pass are likely to be the folding points of the polyline, and in these points, see which point pairs (the combination of points) can be examined at first level.

In Figure 13, the following point pairs can be checked first, and returns True if one returns True,checknobarrier.

(0,7), (0,9)
(1,7), (1,9)
(2,10), (3,10)

If none of them returns true, or cannot find a point pair that can be examined at all (especially when a block is tightly surrounded by obstructions), then the three test result is false, at which point the fold number of the polyline has reached 2, according to the rules of the game, The Checknobarrier returns false at this time.

If you think that the three-level test is a two-step problem, I would like to use the first level test directly, also can.

Just check all of the following points (see Figure 13).

(n,7), (n,9)
(2,m), (3,m)

where n is a 0~12 integer and M is a 0~10 integer.

This code can be simplified, but because the first-level test itself is a loop, if this method, it is necessary to nest on a larger cycle, operating efficiency is not high, so here or do not recommend this method. At this point, the wiring algorithm has been introduced. Hope that through this process, we can realize the general idea of the block game algorithm. As for the other details of the game, here is a little bit of mention.

1 Order of inspection

To check whether two blocks can be eliminated, we want two squares to meet two conditions at the same time, which is paired and connected successfully. In which, the pairing only needs to use a a+b==100, so the operation is much faster than the test connection. So, you should check the pairing first, if the condition is not true, No more wiring checks, so you can avoid a lot of unnecessary complex operations. Of course, if you are doing this game, the pairing rules are not as simple as the author, then see which is the trouble to put it in the back.

2 Location of barrier properties

Obstacle properties as far as possible not to use the box is visible and other appearance factors to determine, the appearance of factors should be determined by internal variables. Because it is possible that one day you want to change to the elimination box just need to make the box gray, then your internal inspection code to modify, more trouble. Try to use an array to store these properties. Then the appearance is another set of code, which is easy to maintain, but also easy to place animation effects.



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.