[Python]
マインスイーパを書いてる。
なんか修正していくとだんだんダメっぽいプログラムになってきた。
…一回真面目にリファクタリングした方がいいかも。
#!/usr/bin/env python import Tkinter import random class MineSweeper: def initial(self): self.clear() self.map = [] self.openMap = [] for ii in range(self.width): self.map.append([]) self.openMap.append([]) self.panel.append([]) for jj in range(self.height): if random.randint(0,3)==3: self.map[ii].append(1) else: self.map[ii].append(0) self.openMap[ii].append(0) scale = self.scale self.panel[ii].append(self.canvas.create_rectangle(ii*scale, jj*scale, ii*scale+scale, jj*scale+scale, outline="black", fill="gray")) self.drawMap() def clear(self): for item in self.panel[:]: self.canvas.delete(item) self.panel = [] for item in self.texts[:]: self.canvas.delete(item) self.texts = [] def countAround(self,x,y): map = self.map scale = self.scale w,h = self.width,self.height arndnum = 0 # if 0 <= x-1 < w and 0 <= y-1 < h and map[x-1][y-1]: arndnum = arndnum +1 # if 0 <= x < w and 0 <= y-1 < h and map[x ][y-1]: arndnum = arndnum +1 # if 0 <= x+1 < w and 0 <= y-1 < h and map[x+1][y-1]: arndnum = arndnum +1 # if 0 <= x-1 < w and 0 <= y < h and map[x-1][y ]: arndnum = arndnum +1 # # if 0 <= x < w and 0 <= y < h and map[x ][y ]: arndnum = arndnum +1 # if 0 <= x+1 < w and 0 <= y < h and map[x+1][y ]: arndnum = arndnum +1 # if 0 <= x-1 < w and 0 <= y+1 < h and map[x-1][y+1]: arndnum = arndnum +1 # if 0 <= x < w and 0 <= y+1 < h and map[x ][y+1]: arndnum = arndnum +1 # if 0 <= x+1 < w and 0 <= y+1 < h and map[x+1][y+1]: arndnum = arndnum +1 for ii in range(x-1,x+2): for jj in range(y-1,y+2): if 0 <= ii < w and 0 <= jj < h and map[ii][jj]: arndnum = arndnum + 1 return arndnum def drawMap(self): for ii in range(self.width): for jj in range(self.height): if self.openMap[ii][jj]==1: # 開いているマップの表示 self.canvas.delete(self.panel[ii][jj]) map = self.map scale = self.scale arndnum = 0 w, h = self.width, self.height if map[ii][jj]: self.texts.append(self.canvas.create_text(ii*scale+scale/2, jj*scale+scale/2, fill="gray", text="B")) else: arndnum = self.countAround(ii,jj) self.texts.append(self.canvas.create_text(ii*scale+scale/2, jj*scale+scale/2, text="%d" % arndnum)) else: # 未開のマップ表示 pass def pressCanvas(self,event): x = event.x / self.scale y = event.y / self.scale #self.openMap[x][y] = 1 self.recursiveCheck(x,y) self.drawMap() def recursiveCheck(self,x,y): self.openMap[x][y] = 1 w,h = self.width,self.height map = self.map openMap = self.openMap if self.countAround(x,y)==0: for ii in range(x-1,x+2): for jj in range(y-1,y+2): if 0 <= ii < w and 0 <= jj < h and map[ii][jj]==0 and openMap[ii][jj]==0: self.recursiveCheck(ii,jj) def __init__(self): window = Tkinter.Tk() self.panel = [] self.texts = [] self.width = 16 self.height = 16 self.scale = 20 self.canvas = Tkinter.Canvas(window, bg="white", width=self.width*self.scale, height=self.height*self.scale) self.canvas.bind("<ButtonPress-1>",self.pressCanvas) self.canvas.pack() self.initButton = Tkinter.Button(window, text="initial", command=self.initial) self.initButton.pack() self.initial() window.mainloop() MineSweeper()
慣れていないからなのか、すごいダメなコード。
…脳みそ寝た後に書いたりしているからか。