Python 连连看连接算法
功能:为连连看游戏提供连接算法
说明:模块中包含一个Point类,该类是游戏的基本单元“点”,该类包含属性:x,y,value。
其中x,y代表了该点的坐标,value代表该点的特征:0代表没有被填充,1-8代表被填充为游戏图案,9代表被填充为墙壁
模块中还包含一个名为points的Point列表,其中保存着整个游戏界面中的每个点
使用模块的时候应首先调用createPoints方法,初始化游戏界面中每个点,然后可通过points访问到每个点,继而初始化界面
模块中核心的方法是link,通过提供源点和终点,可尝试连接两点,如果可以连接则返回保存路径的path列表,否则返回False
#-*-coding:utf-8-*-
"""连连看连接算法
为连连看游戏提供连接算法
模块中包含一个Point类,该类是游戏的基本单元“点”,该类包含属性:x,y,value。
其中x,y代表了该点的坐标,value代表该点的特征:0代表没有被填充,1-8代表被填充为游戏图案,9代表被填充为墙壁
模块中还包含一个名为points的Point列表,其中保存着整个游戏界面中的每个点
使用模块的时候应首先调用createPoints方法,初始化游戏界面中每个点,然后可通过points访问到每个点,继而初始化界面
模块中核心的方法是link,通过提供源点和终点,可尝试连接两点,如果可以连接则返回保存路径的path列表,否则返回False
"""
import random
__author__ ="http://blog.csdn.net/anhulife"
__license__ ="python"
class Point:
"""Point类
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):
"""构造点的方向集
每个点在连接的过程中都持有一个方向集,这个方向集中保存着该点的前进方向选择的优先级
优先级:指向目标点的方向级别最高,在同等级别并且遵循x方向优先于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):
"""点的前进动作
点的前进即是依次从方向集中取出优先级高的方向,并判断该方向上的下一个点是否被填充
如果没有被填充则说明该方向可通,并返回该方向。否则试探下一个方向,如果方向集中没有方向可用了,则返回None
"""
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):
"""判断方向变化
返回在该点前进时,是否带来了方向的变化,即方向不同于原方向
"""
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):
"""构造游戏界面的点
初始化界面中的所有的点,并且规则如下:
最外一层是“墙壁”点,接下来的一层是没有被填充的点,被包裹的是填充的点
"""
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):
"""点的连接
连接方法的思想:针对源点的每个方向尝试前进,如果可以前进,则将针对该方向上的下个点的每个方向尝试前进
当一个点的可选方向都不能前进的时候,则返回到已有前进路径中的前一个点,尝试该点其他可选方向。当回源点
的每个方向都走不通或是路径的方向变化等于4的时候,连接失败返回False。否则当路径连接到目标点而且路径的方向变化小
于4的时候,连接成功返回路径
"""
if source == target:
return False
path = list()
change = 0
current = source
while True:
if current==target and change 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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
