游戏界面如上所示:
游戏规则:a.同色相邻(上下左右为相邻对角线不算)方块点击可消除,单个方块不可消除,消除后位置由上方方块填补;若中间出现整列空隙,由右侧平移填补至无整列缝隙;
b.每次消除得分为消除方块数的平方;即加入本次消除5个方块,则得分为25分;
c.当界面无可消除方块游戏结束;并将所有消除方块后的得分相加记为总分;
python:解决方案http://git.oschina.net/galacode/xxk-solution/tree/master python版本为3.5.2
1 __author__ = 'jishu12' 2 import sys 3 import copy 4 import threading 5 #sys.setrecursionlimit(1000000) #例如这里设置为一百万 6 #panel=[[0]*10]*10 7 #将游戏面板抽象化为矩阵 8 gamePanel = [ 9 [ 5,1,5,1,1,5,1,5,3,1 ] 10 , [ 5,1,2,1,3,5,2,5,4,1 ] 11 , [ 5,2,5,1,3,2,2,4,1,1 ] 12 , [ 5,2,3,1,5,2,2,4,4,1 ] 13 , [ 4,2,3,1,5,2,2,4,4,1 ] 14 , [ 5,2,3,1,5,2,5,5,1,4 ] 15 , [ 1,2,1,1,5,1,5,2,4,4 ] 16 , [ 5,1,1,5,5,1,5,4,1,4 ] 17 , [ 5,1,3,1,4,3,3,4,5,4 ] 18 , [ 4,1,1,1,5,4,3,3,5,4] 19 ] 20 21 def initPanel(): 22 view=[[[] for i in range(10)] for i in range(10)] 23 for j in range(0,10): 24 for i in range(0,10): 25 #print('gamePanel('+str(i)+str(j)+')'+'='+str(gamePanel[i][j])) 26 view[i][j]=gamePanel[9-j][i] 27 # print('view ('+str(i)+str(j)+')'+'='+str(view[i][j])) 28 return view 29 30 #扩展点击的方块,采用递归方法 31 def expand(dictGroup,gameView, x, y): 32 if(x>=0 and x<=9 and y>=0 and y<=9): 33 start=gameView[x][y] 34 if(start!=0): 35 if(y+1<=9): 36 up=gameView[x][y+1] 37 else: 38 up=-1 39 if(y-1>=0): 40 down=gameView[x][y-1] 41 else:down=-1 42 if(x-1>=0): 43 left=gameView[x-1][y] 44 else: 45 left=-1 46 if(x+1<=9): 47 right=gameView[x+1][y] 48 else: 49 right=-1 50 if(left!=-1 51 and dictGroup.get(str(x-1)+str(y))==None 52 and start==left): 53 #print('left'+str(x-1)+str(y)+str(left)) 54 dictGroup[str(x-1)+str(y)]=left 55 expand(dictGroup,gameView,x-1,y) 56 if(up!=-1 57 and dictGroup.get(str(x)+str(y+1))==None 58 and start==up): 59 #print('up'+str(x)+str(y+1)) 60 dictGroup[str(x)+str(y+1)]=up 61 expand(dictGroup,gameView,x,y+1) 62 if(right!=-1 63 and dictGroup.get(str(x+1)+str(y))==None 64 and start==right ): 65 #print('right'+str(x+1)+str(y)) 66 dictGroup[str(x+1)+str(y)]=right 67 expand(dictGroup,gameView,x+1,y) 68 if(down!=-1 69 and dictGroup.get(str(x)+str(y-1))==None 70 and start==down): 71 #print('down'+str(x)+str(y-1)) 72 dictGroup[str(x)+str(y-1)]=down 73 expand(dictGroup,gameView,x,y-1) 74 return dictGroup 75 76 #将选中方块值置0 77 def destoryView(tempGroup,view): 78 if(tempGroup!=None): 79 for key in tempGroup.keys(): 80 x=key[0:1] 81 y=key[1:2] 82 #print('key:'+key+'x'+x+' y'+y) 83 view[int(x)][int(y)]=0 84 85 #绘制选择完方块的游戏界面 86 def drawView(view): 87 for j in range(0,10): 88 row ='' 89 for i in range(0,10): 90 row+= str(view[i][9-j])+' ' 91 print(row) 92 93 def needCleanColumn(view ,x): 94 for y in range(0,10): 95 if(view[x][y]==0): 96 for j in range(y,10): 97 if(view[x][j]!=0): 98 return True 99 return False 100 #刷新行 101 def refreshColumn(view,x): 102 while(needCleanColumn(view,x)): 103 for y in range(0, 10): 104 if(0==view[x][y]): 105 for j in range(y,10): 106 #print(str(j)) 107 if(j==9): 108 view[x][j]=0 109 else: 110 temp=view[x][j+1] 111 view[x][j] = temp 112 113 def needCleanRow(view,x): 114 if(view[x][0]==0): 115 for i in range(x+1,10): 116 if(view[i][0]!=0): 117 #print('x-'+str(view[m][0])+' i-'+str(view[i][0])+' step-'+str(i-m)) 118 return i-x 119 return 0 120 #刷新列 121 def refreshRow(view,x): 122 step=needCleanRow(view,x) 123 while(step>0): 124 for i in range(x,10): 125 if(i>=10-step): 126 for j in range(0,10): 127 view[i][j]=0 128 else: 129 for j in range(0,10): 130 view[i][j]=view[i+step][j] 131 step=needCleanRow(view,x) 132 #print('step:'+str(step)+'x:'+str(x)) 133 134 135 def refreshView(view): 136 for x in range(0,10): 137 refreshColumn(view,x) 138 for x in range(0,10): 139 refreshRow(view,x) 140 return view 141 142 def getGroupName(group): 143 key='' 144 for y in range(0,10): 145 for x in range(0,10): 146 if(group!=None and group.get(str(x)+str(y))!=None): 147 key=key+str(x)+str(y) 148 return key 149 150 #将游戏界面划分为可点击的组 151 def getGroupList(view): 152 panelGroup={} 153 for y in range(0,10): 154 for x in range(0,10): 155 tempGroup={} 156 tempGroup=expand(tempGroup,view,x,y) 157 #print('('+str(x)+','+str(y)+')'+'=>tempGroup'+str(tempGroup)) 158 key=getGroupName(tempGroup) 159 if(panelGroup.get(key)==None and key!=''): 160 panelGroup[key]=tempGroup 161 if(tempGroup!=None):tempGroup.clear() 162 return panelGroup 163 164 class node: 165 key='' 166 score=0 167 view=None 168 childs=[] 169 groups=None 170 log=[] 171 172 processcount=0 173 record=[] 174 count=1 175 def initTree(parent): 176 global processcount 177 groups=copy.deepcopy(parent.groups) 178 #temp=copy.deepcopy(parent.view) 179 if(groups.__len__()==0): 180 global count 181 count=count+1 182 print('************************************************* score:%d'%parent.score+' count:'+str(count)) 183 if(parent.score>1050): 184 print('************************************************* score:%d'%parent.score+' count:'+str(count)+' processcount:%d'%processcount) 185 #print(str(parent.key)+' ') 186 record.append(parent.score) 187 fo=open('d:\foo.txt', 'a') 188 fo.write(str(parent.score)+' ') 189 fo.write(str(parent.key)+' ') 190 fo.close() 191 #drawView(parent.view) 192 return 193 else: 194 #print('len:%d '%groups.__len__()+'groups:'+str(groups)) 195 for item in groups.keys(): 196 #print('item:'+str(item)) 197 x = int(str(item)[0:1]) 198 y = int(str(item)[1:2]) 199 #print('>>>>>>>>>>>>>>>('+str(x)+','+str(y)+')') 200 view=copy.deepcopy(parent.view) 201 #view=initGroup(view,x,y) 202 tempGroup={} 203 tempGroup=expand(tempGroup,view,x,y) 204 destoryView(tempGroup,view) 205 view=refreshView(view) 206 #drawView(view) 207 child=node() 208 child.key=parent.key+' ->'+item 209 child.view=view 210 child.score=parent.score+len(tempGroup)*len(tempGroup) 211 child.groups=(getGroupList(view)) 212 child.log.append(view) 213 parent.childs.append(child) 214 if(processcount<10): 215 processcount=processcount+1 216 t1 = threading.Thread(target=initTree,args=(child,)) 217 t1.setDaemon(True) 218 t1.start() 219 else: 220 initTree(child) 221 threads = [] 222 def startGame(): 223 temp=initPanel() 224 groups=getGroupList(temp) 225 #print('----------'+str(groups)) 226 #for item in groups.keys(): 227 view=copy.deepcopy(temp) 228 #x = int(str(item)[0:1]) 229 #y = int(str(item)[1:2]) 230 #print('start at ('+str(x)+','+str(y)+')') 231 tempGroup={} 232 tempGroup=expand(tempGroup,view,8,0) 233 print('tempGroup'+str(tempGroup)) 234 destoryView(tempGroup,view) 235 view=refreshView(view) 236 drawView(view) 237 #view=initGroup(view,1,0) 238 root = node() 239 root.key=str(8)+str(0) 240 root.view = view 241 root.score=len(tempGroup)*len(tempGroup) 242 root.groups=getGroupList(view) 243 t1 = threading.Thread(target=initTree,args=(root,)) 244 threads.append(t1) 245 t1.setDaemon(True) 246 t1.start() 247 t1.join() 248 #for t in threads: 249 # t.setDaemon(True) 250 # t.start() 251 # t.join() 252 #initTree(root) 253 254 startGame()
遍历出的结果:
score:1638
->30
->0809
->020313040506
->5363545556
->636474849465756676
->1011
->818283
->60706162
->405041516152
->60806171818283
->0020304050607080011121316171810212327282137383147484152535162636273728
->10203011
->00102030
score:1645
->30
->020313040506
->60706162
->607071
->53545556
->8494
->90919293
->405060415161715262726373
->0304
->71627273
->222333
->1011
->0020304050607080011121316181021232728213237383142434748415253575162636
->0010203040