用 Python 写 lurd2xsb 程序 (二)

作者:杨超

本文地址:http://sokoban.ws/blog/?p=729

半年前用Python写了一个推箱子的lurd2xsb小程序。昨晚好奇为了了解Python建站技术,在https://www.pythonanywhere.com申请了一个帐号。

然后今天研究了大半天,终于成功把之前的lurd2xsb通过web.py框架,稍作修改,成功地作为一个web程序运行。本来技术上没有什么困难,但是排除缩进引起的错误花了我很长时间。程序原有的Sokoban类的代码一个字符未动,只是输入输出方式有所改变。

通过web.py框架用Python写web程序,和php很不大一样。

最后这个lurd2xsb网页程序地址是:http://sokoban.pythonanywhere.com/。在这个地址斜杆后接lurd串访问,便在返回页面中显示还原的xsb格式关卡。比如这个链接

全部代码如下:

import web

class Sokoban:
    def __init__(self):
		self.level = "####@####"
		self.w=3
		self.h=3
		self.man=4
    def __str__(self):
		temp = ""
		for i in range(0,self.h):
			temp += self.level[i*self.w : (i+1)*self.w] + "\n"
		temp += "size: " + str(self.w) + "x" + str(self.h)
		temp += "\nman: " + str(self.man)
		return temp
    def add_row(self,l):
		if l==1:
			for i in range(0,self.w):
				self.level = self.level + "#"
		elif l==0:
			for i in range(0,self.w):
				self.level = "#" + self.level
			self.man += self.w
		self.h+=1
    def add_column(self,l):
		if l==1:
			self.man += (self.man / self.w)
			for i in range(0,self.h):
				self.level = self.level[0:self.w * (self.h  -i)] + "#" + self.level[self.w * (self.h  -i):]
		elif l==0:
			self.man += ( self.man / self.w + 1 )
			for i in range(0,self.h):
				self.level = self.level[0:self.w * (self.h  - 1 - i)] + "#" + self.level[self.w * (self.h  - 1 - i):]
		self.w+=1
    def undo(self,step):
		#prepare
		if step == 'l' or step == 'L':
			d = -1
		elif step == 'r' or step == 'R':
			d = 1
		elif step == 'u' or step == 'U':
			d = -1 * self.w
		elif step == 'd' or step == 'D':
			d = self.w
		else:
			return
		#action
		if self.level[ self.man - d] == '$' or self.level[ self.man - d] == '*':
			exit("invalid input lurd string![0]")
		elif step == 'l' or step == 'u' or step == 'r' or step == 'd': #undo a move
			if self.level[ self.man] == '@':
				floor = ' '
			else:
				floor = '.'
			if self.level[ self.man - d ] == '.':
				pusher = '+'
			else:
				pusher = '@'
			self.level = self.level[0:self.man] + floor + self.level[ self.man +1:]
			self.level = self.level[0:self.man - d ] + pusher + self.level[self.man - d + 1:]
			self.man -= d
		elif step == 'L' or step == 'U' or step == 'R' or step == 'D': #undo a push
			if self.level[ self.man] == '@':
				box = '$'
			else:
				box = '*'
			if self.level[ self.man - d ] == '.':
				pusher = '+'
			else:
				pusher = '@'
			if self.level[ self.man + d ] == '*' or self.level[ self.man + d ] == '#':
				floor = '.'
			elif self.level[ self.man + d ] == '$':
				floor = ' '
			else:
				exit("invalid input lurd string![1]")
			self.level = self.level[0:self.man] + box + self.level[ self.man +1:]
			self.level = self.level[0:self.man - d ] + pusher + self.level[self.man - d + 1:]
			self.level = self.level[0:self.man + d ] + floor + self.level[self.man + d + 1:]
			self.man -= d
		#post action
		if self.man % self.w == self.w -1:
			self.add_column(1)
		elif self.man % self.w ==0:
			self.add_column(0)
		elif self.man < self.w:
			self.add_row(0)
		elif self.man >= (self.h - 1 )* self.w:
			self.add_row(1)
		#post action 2
		if '.' in self.level[0:self.w]:
			self.add_row(0)
		elif '.' in self.level[(self.h-1)*self.w : self.h*self.w]:
			self.add_row(1)
		else:
			for i in range(0,self.h):
				if self.level[i*self.w]=='.':
					self.add_column(0)
					break
				elif self.level[i*self.w + self.w -1]=='.':
					self.add_column(1)
					break

urls = (
  '/(.*)', 'index'
)

class index:
    def GET(self,lurd):
        web.header('Content-Type','text/html; charset=utf-8', unique=True)
        skb = Sokoban()
        for step in lurd[::-1]:
            try:
                skb.undo(step)
            except SystemExit, msg:
                return """
                    <html>
                    <head>
                        <title>lurd2xsb in Python</title>
                    </head>
                    <body>
                    <h1>lurd2xsb in Python</h1>
                    <pre>""" + 'Invalid lurd string!' + """  </pre><hr>
                    <i>Created on Oct. 31, 2012</i> by <a target="_blank" href="http://sokoban.ws">http://sokoban.ws</a>
                    </body></html>"""
        return """
            <html>
            <head>
                <title>lurd2xsb in Python</title>
            </head>
            <body>
            <h1>lurd2xsb in Python</h1>
            <pre>""" + skb.__str__() + """  </pre><hr>
            <i>Created on Oct. 31, 2012</i> by <a target="_blank" href="http://sokoban.ws">http://sokoban.ws</a>
            </body></html>"""

app = web.application(urls, globals())
application = app.wsgifunc()
此条目发表在 推箱子, 编程 分类目录。将固定链接加入收藏夹。

评论功能已关闭。