字符挖地雷
时间:2011-01-06
来源:互联网
本帖最后由 bluepp 于 2011-01-06 23:23 编辑
比较简陋 但基本功能都实现了
棋盘雷数定制
挖格、标记、连在一起的空白点击后自动展开
成功失败判定
暂时没发现bug,新手,写得笨。大家看看给点儿建议啊,谢谢了!!
复制代码
比较简陋 但基本功能都实现了
棋盘雷数定制
挖格、标记、连在一起的空白点击后自动展开
成功失败判定
暂时没发现bug,新手,写得笨。大家看看给点儿建议啊,谢谢了!!
- import sys, re
- from random import randint
-
- pt_relations = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
- panel_height = 9
- panel_width = 9
- bombs = 9
- martrix = []
- mask = []
-
- def draw(content):
- global bombs
- print '--%d bombs game---' % bombs
- for i in content:
- for j in i:
- print str(j).center(1),
- print
-
- def area_check(x, y):
- '''
- check if the point in panel area
- '''
- global panel_width, panel_height
- return 0 <= x <= (panel_width - 1) and 0 <= y <= (panel_height - 1)
-
- def init_panel():
- global panel_width, panel_height, bombs, martrix, mask
- martrix = [[0 for x in range(panel_width)][:] for y in range(panel_height)]
- mask = [['-' for x in range(panel_width)][:] for y in range(panel_height)]
- for i in range(bombs):
- x = randint(0, panel_width - 1)
- y = randint(0, panel_height - 1)
- martrix[y][x] = 'x'
- for r in range(len(martrix)):
- row = martrix[r]
- for c in range(len(row)):
- col = martrix[r][c]
- if col == 'x':
- for pt in pt_relations:
- y = r + pt[0]
- x = c + pt[1]
- if area_check(x, y) and martrix[y][x] != 'x':
- martrix[y][x] += 1
- draw(mask)
-
- def zvirus(seedx, seedy):
- '''
- zero virus,explan all zero cells near by cell-self
- '''
- for pt in pt_relations:
- c = seedx + pt[0]
- r = seedy + pt[1]
- if area_check(c, r) and martrix[c][r] != '*' and mask[c][r] == '-':
- mask[c][r] = martrix[c][r]
- if mask[c][r] == 0:
- zvirus(c, r)
-
- def click(loc):
- global mask, martrix
- if mask[loc[0]][loc[1]] == '#':
- print 'Marked cell is not clickable'
- else:
- mask[loc[0]][loc[1]] = martrix[loc[0]][loc[1]]
- if martrix[loc[0]][loc[1]] == 'x':
- print '\n Game Over! The bombs:'
- draw(martrix)
- print 'starting a new game'
- init_panel()
- else:
- if martrix[loc[0]][loc[1]] == 0:
- zvirus(loc[0], loc[1])
- draw(mask)
-
- def mark(loc):
- global mask, bombs
- if mask[loc[0]][loc[1]] == '-':
- mask[loc[0]][loc[1]] = '#'
- print 'marked', loc[0], loc[1]
- elif mask[loc[0]][loc[1]] == '#':
- mask[loc[0]][loc[1]] = '-'
- print 'unmarked', loc[0], loc[1]
- draw(mask)
-
- def check_win():
- global mask
- uncomplet = 0
- for i in mask:
- if '-' in i:
- uncomplet += 1
- break
- if not uncomplet:
- print 'Weldone! you win the game!'
- draw(martrix)
- print 'starting a new game'
- init_panel()
-
- def usage():
- global panel_width,panel_height
- print
- print "Use: type 'click XY' (RET) to click a certan cell\n"
- print " 'click 74' for example \n"
- print " type 'mark XY' (RET) to mark a bomb cell,\n"
- print " do this on marked cell means unmark \n"
- print " 'mark 88' for example\n"
- print " type 'stop' (RET) or C-c to quit the program\n"
- print " NOTICE THAT: X :[0,{0}) ; Y:[0,{1}).".format(panel_width,panel_height)
- print
-
- if __name__ == "__main__":
- init_panel()
- p = re.compile(r'^stop$|(^click \d\d$)|(^mark [0-8][0-8]$)')
- bRun = 1
- while(bRun):
- cmdl = sys.stdin.readline().strip()
- if p.match(cmdl):
- if cmdl == 'stop':
- bRun = 0
- else:
- cmd = cmdl[:cmdl.find(' ')]
- y = int(cmdl[cmdl.find(' ') + ((panel_width/10)+1)])
- x = int(cmdl[cmdl.find(' ') + ((panel_height/10)+((panel_width/10)+1)+1)])
- if cmd == 'click':
- click([x, y])
- elif cmd == 'mark':
- mark([x, y])
- else:
- pass
- check_win()
- else:
- usage()
作者: bluepp 发布时间: 2011-01-06
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
代码中的若干写法,虽然简单,但是很大程度上降低了代码的可读性。
个人很反感这种写法。
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
代码中的若干写法,虽然简单,但是很大程度上降低了代码的可读性。
个人很反感这种写法。
作者: tubocurarine 发布时间: 2011-01-07
QUOTE:
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
...
tubocurarine 发表于 2011-01-07 09:03
Beautiful is better than ugly.
Explicit is better than implicit.
...
tubocurarine 发表于 2011-01-07 09:03
感谢回复!
我昨天找了几个其他脚本语言的挖雷,准备参考以后再修改一下。
这段代码确实有些写法很晦涩。我试着改改。
但是其实我感觉…… 用100+ lines 代码还是有些拖沓。
作者: bluepp 发布时间: 2011-01-07
应该会有一个偶发的 bug:
设置地雷的地方,直接用 rand 来设置,可能会在已经设置过地雷的地方重复设置,导致最后设置的地雷数量不足。
还有,也可以直接用一个二维数组 (list) 来实现,这个数组中每一个元素都是一个class 实例,每个这个实例都可以表述自我(例如自己是否是个地雷,自己周围有多少地雷之类)。
这样最后的代码长度也许会更长,但是可读性应该会更好一些。
设置地雷的地方,直接用 rand 来设置,可能会在已经设置过地雷的地方重复设置,导致最后设置的地雷数量不足。
还有,也可以直接用一个二维数组 (list) 来实现,这个数组中每一个元素都是一个class 实例,每个这个实例都可以表述自我(例如自己是否是个地雷,自己周围有多少地雷之类)。
这样最后的代码长度也许会更长,但是可读性应该会更好一些。
QUOTE:
感谢回复!
我昨天找了几个其他脚本语言的挖雷,准备参考以后再修改一下。
这段代码确实有些写 ...
bluepp 发表于 2011-01-07 09:24
我昨天找了几个其他脚本语言的挖雷,准备参考以后再修改一下。
这段代码确实有些写 ...
bluepp 发表于 2011-01-07 09:24
作者: tubocurarine 发布时间: 2011-01-07
QUOTE:
应该会有一个偶发的 bug:
设置地雷的地方,直接用 rand 来设置,可能会在已经设置过地雷的地方重复设置, ...
tubocurarine 发表于 2011-01-07 09:40
设置地雷的地方,直接用 rand 来设置,可能会在已经设置过地雷的地方重复设置, ...
tubocurarine 发表于 2011-01-07 09:40
哈哈哈 是的。
我忘了加
作者: bluepp 发布时间: 2011-01-07
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28