写完这篇blog,却忘发了,呵呵
python向往很久了,国庆得闲终于决定开始学习这门语言。我的计划是以实用为目的,边用边学。首先当然先看看入门书,了解了解语法和大致结构,安装编程环境,这个花了差不多一天时间。
下载python请到官网
http://www.python.org/getit/,目前最新版为3.3.2。建议直接下载最新版学习,从python2.7到3.3.2有较大改变,很多网站的教材都是针对2.x版本的,对3.x都不再适用了。比如:print “hello!"语句在2.x版中是可以的,但3.x版中必须改为print("hello")。最好是直接用官网的教程和说明文档,更新及时,不会存在版本差异造成的问题。
第一目标:写一个搜索Fling!毛毛球碰撞游戏的答案的程序
Fling!是苹果iphone系统的一个小游戏,后来也被移植到安卓系统中。游戏玩法就是通过碰撞棋盘上的小球,使最后只剩下一个球。itune网址https://itunes.apple.com/us/app/fling!/id325815008?mt=8android网址https://play.google.com/store/apps/details?id=com.mbgames.fling超喜欢这个游戏,前面20多关难度都不算大,但是27关以后就没那么容易解决了。我先用excel的vba宏写了一个程序,可惜vba运行太慢,25个球的解答就要搜索近半个小时。现在想把它改写成python版本。excel版的毛球解决程序我已经放在这里了http://club.excelhome.net/thread-1061267-1-1.html
最后实现的代码如下,用到了class,数据存储使用了list[].
1 import copy
2 class Fling:
3 def __init__(self, parent=0, balls=[[0]*7 for x in range(8)]):
4 self.balls = copy.deepcopy(balls)
5 self.code = 0 #balls's hash code
6 self.scan = False
7 self.parent = parent
8 self.x = 0
9 self.y = 0
10 self.direct = 0
11
12 def setCode(self):
13 self.code = 0
14 for y in range(0,8):
15 for x in range(0,7):
16 if self.balls[y][x] != 0:
17 self.code = self.code * 131 + x
18 self.code = self.code % 16393001 #防止溢出
19 self.code = self.code * 131 + y
20 self.code = self.code % 16393001
21
22 def reset(self):
23 self.balls = [[0]*7 for x in range(8)]
24 self.scan = False
25 self.code = 0
26 self.parent = 0
27 self.x = 0
28 self.y = 0
29 self.direct = 0
30
31 def collision(self, x, y, direct):
32 flag = False
33 # 向上抛球
34 if direct == 0:
35 i = y - 1
36 while i >= 0:
37 if self.balls[i][x] != 0: #遇到球
38 if flag == False: #遇到第一个球
39 if i == y - 1:
40 break #exit while
41 self.balls[y][x] = 0
42 self.balls[i + 1][x] = 1 #母球移动到此
43 flag = True #碰撞成立
44 elif i != y - 1:
45 self.balls[y][x] = 0
46 self.balls[i + 1][x] = 1 #母球移动到此
47 y = i
48 i = i - 1
49
50 # 向右抛球
51 elif direct == 1:
52 i = x + 1
53 while i < 7:
54 if self.balls[y][i] != 0: #遇到球
55 if flag == False: #遇到第一个球
56 if i == x + 1:
57 break #相邻球不能碰撞
58 self.balls[y][x] = 0
59 self.balls[y][i - 1] = 1 #母球移动到此
60 flag = True #碰撞成立
61 elif i != x + 1: #非相邻球
62 self.balls[y][x] = 0
63 self.balls[y][i - 1] = 1 #母球移动到此
64 x = i #被撞球成为新的母球
65 i = i + 1
66
67 # 向下抛球
68 elif direct == 2:
69 i = y + 1
70 while i < 8:
71 if self.balls[i][x] != 0: #遇到球
72 if flag == False: #遇到第一个球
73 if i == y + 1:
74 break #exit while
75 self.balls[y][x] = 0
76 self.balls[i - 1][x] = 1 #母球移动到此
77 flag = True #碰撞成立
78 elif i != y + 1:
79 self.balls[y][x] = 0
80 self.balls[i - 1][x] = 1 #母球移动到此
81 y = i
82 i = i + 1
83
84 # 向左抛球
85 elif direct == 3:
86 i = x - 1
87 while i >= 0:
88 if self.balls[y][i] != 0: #遇到球
89 if flag == False: #遇到第一个球
90 if i == x - 1:
91 break #相邻球不能碰撞
92 self.balls[y][x] = 0
93 self.balls[y][i + 1] = 1 #母球移动到此
94 flag = True #碰撞成立
95 elif i != x - 1: #非相邻球
96 self.balls[y][x] = 0
97 self.balls[y][i + 1] = 1 #母球移动到此
98 x = i #被撞球成为新的母球
99 i = i - 1
100
101 if flag == True: #碰撞成立,最后一个球移出屏幕
102 self.balls[y][x] = 0
103 self.setCode()
104
105 return flag
106
107 # import pdb
108 # pdb.set_trace() # opens up pdb prompt
109
110 question = [[1,0,0,1,0,1,0], \
111 [0,1,0,0,0,0,0], \
112 [0,0,0,0,0,0,1], \
113 [0,0,0,1,1,0,0], \
114 [0,0,0,1,0,0,0], \
115 [0,0,1,1,1,0,0], \
116 [0,0,1,0,0,0,0], \
117 [0,0,1,0,0,0,0]]
118 myTree = []
119 myTree.append(Fling(0, question))
120 myHash = []
121 myHash.append(myTree[0].code)
122
123 i = 0
124 while i < len(myTree):
125 if myTree[i].scan != True:
126 for x in range(0,7):
127 for y in range(0,8):
128 if myTree[i].balls[y][x] == 0:
129 continue
130
131 snap = Fling(i, myTree[i].balls)
132 if snap.collision(x,y,0):
133 if snap.code not in myHash:
134 snap.x = x
135 snap.y = y
136 snap.direct = 0
137 myTree.append(snap)
138 myHash.append(snap.code)
139
140 snap = Fling(i, myTree[i].balls)
141 if snap.collision(x,y,1):
142 if snap.code not in myHash:
143 snap.x = x
144 snap.y = y
145 snap.direct = 1
146 myTree.append(snap)
147 myHash.append(snap.code)
148
149 snap = Fling(i, myTree[i].balls)
150 if snap.collision(x,y,2):
151 if snap.code not in myHash:
152 snap.x = x
153 snap.y = y
154 snap.direct = 2
155 myTree.append(snap)
156 myHash.append(snap.code)
157
158 snap = Fling(i, myTree[i].balls)
159 if snap.collision(x,y,3):
160 if snap.code not in myHash:
161 snap.x = x
162 snap.y = y
163 snap.direct = 3
164 myTree.append(snap)
165 myHash.append(snap.code)
166
167 myTree[i].scan = True
168 i = i + 1
169
170 #查找所有弹球动作
171 i = len(myTree) - 1
172 actions = []
173 while myTree[i].parent != 0:
174 actions.append([myTree[i].x, myTree[i].y, myTree[i].direct])
175 i = myTree[i].parent
176 else:
177 actions.append([myTree[i].x, myTree[i].y, myTree[i].direct])
178
179 print("Answer:\nx y direct(0:up,1:right,2:down,3:left)")
180 for element in reversed(actions):
181 print(element[0], element[1], element[2])
182
183