50000のモグラは投下され、うまく空地に着陸できたら、ランダムに6方向に掘る。
掘れなかったらそこでおしまい。モグラの寿命は最大で10。(回掘る)
そんなプログラムです。

Python3.6.7の予約語を全部使ってみる。
紆余曲折の末、残っているのは break class continue del global is or return while
割とメジャーなものばかり。(isは ほぼ使ったことないけど)
classがあるのでクラスを使った何か、ということで冒頭のモグラの話になります。
クラスの中でglobalを初めて使ってみました。(使えると思っていなかった)
isとcontinueは無理して使ってます。
掘れなかったらそこでおしまい。モグラの寿命は最大で10。(回掘る)
そんなプログラムです。

Python3.6.7の予約語を全部使ってみる。
紆余曲折の末、残っているのは break class continue del global is or return while
割とメジャーなものばかり。(isは ほぼ使ったことないけど)
classがあるのでクラスを使った何か、ということで冒頭のモグラの話になります。
クラスの中でglobalを初めて使ってみました。(使えると思っていなかった)
isとcontinueは無理して使ってます。
import tkinter import random tk = tkinter.Tk() tk.title("hex maze") tk.geometry("800x600") class Mogura: x=0 y=0 live=1 age=0 def drop(self): global w,h,mp self.x = random.randint(0,int((w-1)/2))*2 self.y = random.randint(0,int((h-1)/2))*2 if self.y%4==2:self.x+=1 if mp[self.y][self.x]==1:self.live=0 def dig(self): global mp,dx1,dy1,dx2,dy2,w,h di=random.randint(0,5) tx=self.x+dx2[di] ty=self.y+dy2[di] if tx<0 or ty<0 or tx>w-1 or ty>h-1: self.live=0 return if mp[ty][tx]==0: self.live=0 return mp[ty][tx]=0 mp[self.y+dy1[di]][self.x+dx1[di]]=0 self.x=tx self.y=ty self.age+=1 w=60 #width h=40 #height size=10 #hex size hsize=int(size/2) #map init mp=[[1 for x in range(w)] for y in range(h)] mp[0][0]=0 toukei=[0 for x in range(11)] isTest=mp #direction data dx1=[-1,-1, 0, 1, 0,-1] dy1=[ 0,-1,-1, 0, 1, 1] dx2=[-2,-1, 1, 2, 1,-1] dy2=[ 0,-2,-2, 0, 2, 2] #mogura 50000 mo=[Mogura() for i in range(50000)] #main while len(mo)>0: mo[0].drop() for i in range(10): if mo[0].live==0:break mo[0].dig() toukei[mo[0].age]+=1 del mo[0] #is test print("mp is isTest",mp is isTest) print(toukei) #map write cv = tkinter.Canvas(tk, width = 800, height = 600) cv.create_rectangle(0, 0, 800, 600, fill = 'gray') for y in range(h): if y==h-1:continue for x in range(w): sx=x*size+50+(y%2)*hsize sy=y*size+50 if mp[y][x]==1: cv.create_rectangle(sx, sy, sx+size, sy+size, fill = 'red') else: cv.create_rectangle(sx, sy, sx+size, sy+size, fill = 'black') cv.place(x=0, y=0) tk.mainloop()