寫完這篇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