-
Notifications
You must be signed in to change notification settings - Fork 2
/
maze_excel_ole.py
84 lines (75 loc) · 2.61 KB
/
maze_excel_ole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
'''maze_excel_ole
http://nyaos.org/d/index.cgi?p=[Ruby]+win32ole
http://jp.rubyist.net/magazine/?0004-Win32OLE
1: K 2: W 3: R 4: G 5: B 6: Y 7: M 8: C
※ 100 x 100 のときは maximum recursion depth exceeded に達するので limit 変更
入口(左上)から掘ると迷路が簡単になる傾向があるので出口(右下)から掘るように修正
罫線描画のタイミングを変更することで無駄を減らし高速化
mat(r, c) を毎回実行するより m = mat(r, c) とした方が速そうだが Excel 側で
メモリ不足になる? (毎回 del m すれば解決するかも知れないが再帰の都合で無理?)
'''
import sys, os, random
import pywintypes, win32com.client
sys.setrecursionlimit(sys.getrecursionlimit() * 20) # ※
VISIBLE = True
TITLE = u'迷路'
HEIGHT, WIDTH, OFFSET_ROW, OFFSET_COL = 100, 100, 2, 2
MAX_ROW, MAX_COL = OFFSET_ROW + HEIGHT - 1, OFFSET_COL + WIDTH - 1
sheet = None
def mat(r, c):
return sheet.Cells(OFFSET_ROW + r, OFFSET_COL + c)
def isPassed(r, c):
try:
return mat(r, c).Interior.ColorIndex != 6 # Y
except pywintypes.com_error, e:
return True
def isDeadend(r, c):
for d in xrange(4):
dr = 1 if d == 3 else -1 if d == 2 else 0
dc = 1 if d == 1 else -1 if d == 0 else 0
if not isPassed(r + dr, c + dc): return False
else:
return True
def drawWall(r, c, dlist):
e = mat(r, c)
for d in xrange(4):
if dlist[d] == 0: e.Borders(1 + d).Weight = 2
def dig(r, c, direc, count):
dlist = [0, 0, 0, 0]
if direc >= 0: dlist[[1, 0, 3, 2][direc]] = 1
mat(r, c).Interior.ColorIndex = 4 # G
count -= 1
if count == 0: return drawWall(r, c, dlist)
while True:
if isDeadend(r, c): return drawWall(r, c, dlist)
d = random.randint(0, 3)
dr = 1 if d == 3 else -1 if d == 2 else 0
dc = 1 if d == 1 else -1 if d == 0 else 0
if not isPassed(r + dr, c + dc):
dlist[d] = 1
dig(r + dr, c + dc, d, count)
def maze_excel_ole(filename):
global sheet
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = VISIBLE
try:
book = xl.Workbooks.Add()
sheet = book.Worksheets(1)
sheet.Name = TITLE
sheet.Cells(1, 2).Value = TITLE
rg = sheet.Range(
sheet.Cells(OFFSET_ROW, OFFSET_COL), sheet.Cells(MAX_ROW, MAX_COL))
rg.RowHeight = 5.18
rg.ColumnWidth = 0.58
rg.Interior.ColorIndex = 6 # Y
random.seed()
dig(HEIGHT - 1, WIDTH - 1, -1, WIDTH * HEIGHT)
book.SaveAs(filename)
finally:
xl.ScreenUpdating = True
xl.Workbooks.Close()
xl.Quit()
if __name__ == '__main__':
maze_excel_ole(os.path.abspath(u'./%s.xls' % TITLE))