The second game-continuous view

Source: Internet
Author: User

Today, I wrote another game-continuous watching. This is a classic game,

I also want to try it out and how to implement it.

The key part is how to determine whether two selected cells can be connected.

After analysis, we can see that the connection methods are as follows:

Regardless of the above model, the following formula can be summarized:

Traverse all X in the direction of X, for any X, the connection line between the y1-y2 can be connected,

In addition, X1, Y1 ~ X, Y1 and X2, Y2, X, and Y2 can be connected.

DetailsAlgorithmThe implementation is as follows:

 1   Package  Org. Stephen. connecty. Control;  2   3   Import  Org. Stephen. connecty. model. cell;  4   Import Org. Stephen. connecty. model. game;  5   Import  Org. Stephen. connecty. model. grid;  6   Import  Org. Stephen. connecty. model. position;  7   Import  Org. Stephen. connecty. model. route;  8   9   Public   Class  Routefinder { 10   11       Public Grid grid = New  Grid ();  12   13       Public   Void  Before (){  14 Game = Game. getinstance ();  15           For ( Int X = 0; x <game. column_count; X ++){  16               For ( Int Y = 0; y <game. row_count; y ++ ){  17 Grid. cells [y] [x] = New  Cell ();  18 Grid. cells [y] [X]. type = Game. Grid. cells [y] [X]. type;  19 Grid. cells [y] [X]. Selected = Game. Grid. cells [y] [X]. selected;  20 Grid. cells [y] [X]. Destroyed = Game. Grid. cells [y] [X]. destroyed;  21 Grid. cells [y] [X]. Marked = False  ;  22   }  23   }  24   }  25   26       Public  Route find (position first, position second ){ 27   28 Route Vertical = Findvertical (first, second );  29           If (Vertical! = Null  ){  30               Return  Vertical;  31   }  32   33 Route horizontal = Findhorizontal (first, second ); 34           If (Horizontal! = Null  ){  35               Return  Horizontal;  36   }  37   38           Return   Null  ;  39   }  40  41       Private  Route findhorizontal (position first, position second ){  42           For ( Int X = 0; x <game. column_count; X ++ ){  43               Boolean Firstok = False  ;  44               Boolean Secondok = False  ; 45               Boolean Connectorok = False  ;  46   47 Firstok = Checkhorizontalempty (first. x, x, first. y );  48 Secondok = Checkhorizontalempty (second. x, x, second. y );  49 Connectorok = Checkverticalempty (first. Y, second. Y, X );  50               If (Firstok & secondok && Connectorok ){  51                   Return   New Route (first, New Position (x, first. Y ), New  Position (  52   X, second. Y), second );  53   }  54   }  55          Return   Null  ;  56   }  57   58       Private  Route findvertical (position first, position second ){  59           For ( Int Y = 0; y <game. row_count; y ++ ){  60               Boolean Firstok =False  ;  61               Boolean Secondok = False  ;  62               Boolean Connectorok = False  ;  63   64 Firstok = Checkverticalempty (first. Y, Y, first. X );  65 Secondok =Checkverticalempty (second. Y, Y, second. X );  66 Connectorok = Checkhorizontalempty (first. X, second. x, y );  67               If (Firstok & secondok && Connectorok ){  68                   Return   New Route (first, New Position (first. x, y ), New  Position (  69  Second. x, y), second );  70   }  71   }  72           Return   Null  ;  73   }  74   75       Private   Boolean Checkverticalempty ( Int Y,Int Y2, Int  X ){  76           Int Start = Math. Min (Y, Y2 );  77           Int End = Math. Max (Y, Y2 );  78   79           For ( Int I = start; I <= end; I ++ ){  80              If (! Grid. cells [I] [X]. Destroyed &&! Grid. cells [I] [X]. Selected ){  81                   Return   False  ;  82   }  83   }  84           Return   True  ;  85   } 86   87       Private   Boolean Checkhorizontalempty ( Int X, Int X2, Int  Y ){  88           Int Start = Math. Min (x, X2 );  89           Int End = Math. Max (x, X2 );  90  91           For ( Int I = start; I <= end; I ++ ){  92               If (! Grid. cells [y] [I]. Destroyed &&! Grid. cells [y] [I]. Selected ){  93                   Return   False  ;  94   }  95   } 96           Return   True  ;  97   }  98   99 }

TheCodeThe search in the description is implemented. However, when searching for the optimal path, you can list all feasible paths and find the shortest path.

In addition, this implementation code includes timing, skin, and other functions,

Extended interfaces such as modes (move left, move right, etc.) and settings (difficulty, sound, etc.) are reserved. Interested readers can do this on their own.

However, the mouselistener. onclick reaction of the Code is not very sensitive, resulting in operational problems. -- This is a known bug.

At the same time, due to Java writing, we also see performance problems. In the future, we can consider changing it to Android for writing.

 Source codeLocation: http://files.cnblogs.com/stephen-wang/Connecty.zip

 

 

 

 

 

 

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.