Python connection algorithms

Source: Internet
Author: User
During this period of time, I always do something to play. Previously, the Russian Square inspired me to study game algorithms. After a week of ideas, the connected algorithms of continuous view are finally released. In a short time, we will use JavaScript to launch a webpage version of continuous playback based on this algorithm. The following is the description and code. Function: provides connection algorithms for connected video games.
Note: The module contains a Point class, which is the basic unit "Point" of the game. This class includes attributes: x, y, and value.
X and y represent the coordinates of the point. value represents the characteristics of the point. 0 represents not filled, 1-8 represents filled with game patterns, and 9 represents filled with walls.
The module also contains a Point list named points, which stores each Point in the entire game interface.
When using the module, you should first call the createPoints method to initialize each vertex on the game interface. then, you can access each vertex through points, and then initialize the interface.
The core method in the module is link. by providing the source and end points, you can try to connect two points. if you can connect, the path list of the saved path is returned; otherwise, False is returned.

The code is as follows:


#-*-Coding: UTF-8 -*-
"Connection algorithms

Provides connection algorithms for connected video games
The module contains a Point class, which is the basic unit of the game, "Point". This class contains attributes: x, y, and value.
X and y represent the coordinates of the point. value represents the characteristics of the point. 0 represents not filled, 1-8 represents filled with game patterns, and 9 represents filled with walls.
The module also contains a Point list named points, which stores each Point in the entire game interface.
When using the module, you should first call the createPoints method to initialize each vertex on the game interface. then, you can access each vertex through points, and then initialize the interface.
The core method in the module is link. by providing the source and end points, you can try to connect two points. if you can connect, the path list of the saved path is returned; otherwise, False is returned.
"""
Import random

_ Author _ = "http://blog.csdn.net/anhulife"
_ License _ = "python"

Class Point:
"Point class

Point is the basic unit in the game: "Point"
"""
Def _ init _ (self, x, y, value ):
Self. x = x
Self. y = y
Self. value = value
Self. directs = None
Self. changed = 0
Def _ createDirect (self, pre, target ):
"Constructs the direction set of a vertex.

Each vertex holds a direction set during the connection. This direction sets the priority of the forward direction of the vertex.
Priority: The highest level pointing to the target point. at the same level, the x direction prevails over y.
"""
Self. directs = list ()
Stx = target. x-self. x
Sty = target. y-self. y
If stx> = 0:
Self. directs. append ("right ")
Self. directs. append ("left ")
Else:
Self. directs. append ("left ")
Self. directs. append ("right ")
If sty> = 0:
Self. directs. insert (1, "up ")
Self. directs. append ("down ")
Else:
Self. directs. insert (1, "down ")
Self. directs. append ("up ")
If pre = None:
Return
Spx = pre. x-self. x
Spy = pre. y-self. y
If spx = 0:
If spy = 1:
Self. directs. remove ("up ")
Else:
Self. directs. remove ("down ")
Else:
If spx = 1:
Self. directs. remove ("right ")
Else:
Self. directs. remove ("left ")
Def forward (self, pre, target ):
"Point forward action

The forward of a vertex is to extract the direction with a higher priority from the direction set in sequence, and determine whether the next vertex in the direction is filled.
If it is not filled, it indicates that the direction is available and the direction is returned. Otherwise, test the next direction. If no direction is available in the direction set, None is returned.
"""
If self. directs = None:
Self. _ createDirect (pre, target)
If len (self. directs) = 0:
Return None
Direct = None
While (True ):
If len (self. directs) = 0:
Break
TmpDirect = self. directs. pop (0)
If tmpDirect = "up ":
X = self. x
Y = self. y + 1
Elif tmpDirect = "down ":
X = self. x
Y = self. y-1
Elif tmpDirect = "left ":
X = self. x-1
Y = self. y
Elif tmpDirect = "right ":
X = self. x + 1
Y = self. y
P = points [x] [y]
If p. value> 0 and p! = Target:
Continue
Else:
Direct = tmpDirect
If pre = None:
Self. changed = 1
Else:
If (pre. x-self. x) = 0 and (p. x-self. x) = 0:
Self. changed = 0
Else:
If (pre. y-self. y) = 0 and (p. y-self. y) = 0:
Self. changed = 0
Else:
Self. changed = 1
Break
Return direct
Def isChanged (self ):
"Determine the direction change

Returns whether the direction changes when the point advances, that is, the direction is different from the original direction.
"""
Return self. changed
Def _ eq _ (self, p ):
If p = None:
Return False
If self. x = p. x and self. y = p. y:
Return True
Else:
Return False
Points = list ()
Def createPoints (w, h ):
"Constructs game interface points

Initialize all vertices on the interface, and the rules are as follows:
The outermost layer is the "wall" point, the next layer is the point not filled, and the wrapped is the filled point.
"""
R = random. randint
For x in range (w ):
Temp = list ()
For y in range (h ):
If x = 0 or x = (w-1) or y = 0 or y = (h-1 ):
Temp. append (Point (x, y, 9 ))
Else:
If x = 1 or x = (w-2) or y = 1 or y = (H-2 ):
Temp. append (Point (x, y, 0 ))
Else:
Temp. append (Point (x, y, r (1, 8 )))
Points. append (temp)
Def link (source, target ):
"Point Connection

The idea of the connection method: try to move forward in every direction of the source point. if you can move forward, try to move forward in every direction of the next point in this direction.
When neither of the available directions of a vertex can be moved forward, the previous vertex in the existing forward path is returned, and other optional directions of the vertex are attempted. When the origin site
If the connection fails or the path direction changes to 4, False is returned. Otherwise, when the path is connected to the target point and the direction of the path changes little
At 4, the connection is successful and the return path is
"""
If source = target:
Return False
Path = list ()
Change = 0
Current = source
While True:
If current = target and change <4:
For p in path:
P. directs = None
Return path
If change = 4:
Current. directs = None
Current = path. pop (len (path)-1)
Change = change-current. isChanged ()
Continue
If change = 0:
Direct = current. forward (None, target)
Else:
Direct = current. forward (path [len (path)-1], target)
If direct! = None:
Change = change + current. isChanged ()
If direct = "up ":
X = current. x
Y = current. y + 1
Elif direct = "down ":
X = current. x
Y = current. y-1
Elif direct = "left ":
X = current. x-1
Y = current. y
Elif direct = "right ":
X = current. x + 1
Y = current. y
Print x, y
Path. append (current)
Current = points [x] [y]
Else:
If change = 0:
Return False
Else:
Current. directs = None
Current = path. pop (len (path)-1)
Change = change-current. isChanged ()
CreatePoints (8, 8)
P = link (points [2] [2], points [5] [2])
Print p

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.