免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2392 | 回复: 5
打印 上一主题 下一主题

前2天完成hangman game附上source code [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-24 11:59 |只看该作者 |倒序浏览
本帖最后由 dreamlearn 于 2012-10-24 12:02 编辑

代碼複製在這裡有有些醜陋sorry
猜linux命令
因為有其他程序在寫所以沒有時間完善他, 所以有一些功能不完整
有興趣的請完善他謝謝!!
我的設計上每通過10關上一級
words 寫入 shelve 是想方便用戶添加words
原本猜對了會有中文解釋命令但還沒有寫


  1. import os
  2. import random
  3. import shelve

  4. losed = 0

  5. if not os.path.exists('/home/jack/project/protector_lib'):
  6.     db = shelve.open('/home/jack/project/protector_lib')
  7.     db['round']  = 0
  8.     db['level']  = 0
  9.     db['words_0'] = ['ls', 'cd', 'cat', 'clear', 'cat',
  10.                      'cal', 'cp', 'df', 'find', 'file']
  11.     db['words_1'] = ['login', 'loginame', 'logout', 'adduser','useradd',
  12.                      'userdel', 'uname', 'hostname', 'whoami', 'finger']
  13. else:
  14.     db = shelve.open('/home/jack/project/protector_lib.py')

  15. interface = (
  16. '''
  17. Wellcome to play the [Protector game]
  18. 1 Play game.
  19. 2 Add new words.
  20. 3 Exit.
  21. ''')  

  22. def Main( ):
  23.     print(game_name)
  24.     print(interface)  
  25.     opt = input('Please input option: ')
  26.     opt = int(opt)
  27.     if opt == 1:
  28.         Game()
  29.     if opt == 2:
  30.         Add_Words()
  31.     if opt == 3:
  32.         exit( )

  33. def get_words( ):
  34.     words = 'words_%s' % db['level']
  35.     return db[words]

  36. def Game_OPT(opt):
  37.     if opt == 'menu':
  38.         opt = input('Your want quit game to Menu?[Y/N]').upper( )
  39.         if opt != 'Y': print('Cancel!')
  40.         else: return True
  41.     elif opt == 'exit':
  42.         opt = input('Your want exit the Game?[Y/N]').upper( )
  43.         if opt != 'Y': print('Cancel')
  44.         else: return True
  45.         
  46. def Game( ):
  47.     global losed
  48.     MAX_WRONG = len(pattern_1) - 1
  49.     words = random.choice(get_words( ))
  50.     words.upper( )
  51.     print(words)
  52.     so_far= '-' * len(words)
  53.     used  = [ ]
  54.     wrong = 0
  55.     losed += 1
  56.     if losed >= 4: print('Game Over!')  
  57.     if db['round'] >= 10:
  58.         db['round']  = 0
  59.         db['level'] += 1
  60.     while True:
  61.         print("You've used the following letters\n%s:" % used)
  62.         print(pattern_1[wrong])
  63.         print('So far, the word is:\n%s' % so_far)   
  64.         guess = input('Enter your guess: ')
  65.         guess.upper( )
  66.         while guess in used:
  67.             print("You've already guess the letter %s" % guess)
  68.             guess =  input('Enter your guess: ')
  69.             guess.upper( )
  70.         used.append(guess)
  71.         if guess == 'menu':
  72.             if Game_OPT('menu'): Main( )
  73.         elif guess == 'exit':
  74.             if Game_OPT('exit'): exit( )
  75.         if guess not in words:
  76.             print('Sorry, %s is not in the word.' % guess)
  77.             wrong += 1
  78.             if MAX_WRONG < wrong:
  79.                 print('aaasas')
  80.                 Game( )
  81.                     
  82.         elif guess in words:
  83.             new = ''
  84.             for i in range(len(words)):
  85.                 if guess == words[i]:
  86.                     new += guess
  87.                 else:
  88.                     new += so_far[i]
  89.             so_far = new
  90.             if so_far == words:
  91.                 db['round']  += 1
  92.                 Game( )
  93.             if db['round'] == 10:
  94.                 Game( )


  95. game_name = (
  96. r'''
  97.              _______   _______     ____   ________   ________     ______   ________   ____     _______
  98.             |  ___  \ |  ___  \   / __ \ |__    __| |   _____|   / _____| |__    __| / __ \   |  __   \
  99.             | |___| | | |___| |  / /  \ \   |  |    |  |_____   / /          |  |   / /  \ \  | |___| |
  100.             |  _____/ |  ___  / | |    | |  |  |    |   _____| |  |          |  |  | |    | | |  ___  /
  101.             | |       | |   \ \  \ \__/ /   |  |    |  |_____   \  \____     |  |   \ \__/ /  | |   \ \
  102.             |_|       |_|    \_\  \____/    |__|    |________|   \______|    |__|    \____/   |_|    \_\
  103. ''')

  104. pattern_1 = (
  105. r'''

  106.                          _____________________________
  107.                         |               |             |
  108.                         |               |             |
  109.                         |                             |
  110.                         |                             |
  111.                         |                             |
  112.                         |                             |
  113.                         |                             |
  114.                         |                             |
  115.                         |                             |
  116.                         |                             |
  117.                         |                             |
  118.                         |_____________________________|
  119. ''',
  120. r'''
  121.                          _____________________________
  122.                         |               |             |
  123.                         |               |             |
  124.                         |              ---            |
  125.                         |             /* *\           |
  126.                         |             \ o /           |
  127.                         |              ---            |
  128.                         |                             |
  129.                         |                             |
  130.                         |                             |
  131.                         |                             |
  132.                         |                             |
  133.                         |                             |
  134.                         |_____________________________|
  135. ''',
  136. r'''
  137.                          _____________________________
  138.                         |               |             |
  139.                         |               |             |
  140.                         |              ---            |
  141.                         |             /* *\           |
  142.                         |             \ * /           |
  143.                         |              ---            |
  144.                         |              ===            |
  145.                         |                             |
  146.                         |                             |
  147.                         |                             |
  148.                         |                             |
  149.                         |                             |
  150.                         |_____________________________|
  151. ''',
  152. r'''                    
  153.                          _____________________________
  154.                         |               |             |
  155.                         |               |             |
  156.                         |              ---            |
  157.                         |             /* *\           |
  158.                         |             \ ^ /           |
  159.                         |              ---            |
  160.                         |              ===            |
  161.                         |              ===            |
  162.                         |              ===            |
  163.                         |                             |
  164.                         |                             |
  165.                         |                             |
  166.                         |_____________________________|
  167. ''',
  168. r'''               
  169.                          _____________________________
  170.                         |               |             |
  171.                         |               |             |
  172.                         |              ---            |
  173.                         |             /* *\           |
  174.                         |             \ ~ /           |
  175.                         |              ---            |
  176.                         |            //===            |
  177.                         |            //===            |
  178.                         |            * ===            |
  179.                         |                             |
  180.                         |                             |
  181.                         |                             |
  182.                         |_____________________________|

  183. ''',
  184. r'''
  185.                          _____________________________
  186.                         |               |             |
  187.                         |               |             |
  188.                         |              ---            |
  189.                         |             /* *\           |
  190.                         |             \ + /           |
  191.                         |              ---            |
  192.                         |            //===\\          |
  193.                         |           // === \\         |
  194.                         |           *  ===  *         |
  195.                         |                             |
  196.                         |                             |
  197.                         |                             |
  198.                         |_____________________________|
  199. ''',
  200. r'''                     
  201.                          _____________________________
  202.                         |               |             |
  203.                         |               |             |
  204.                         |              ---            |
  205.                         |             /* *\           |
  206.                         |             \ ~ /           |
  207.                         |              ---            |
  208.                         |            //===\\          |
  209.                         |           // === \\         |
  210.                         |           *  ===  *         |
  211.                         |             //              |
  212.                         |            //               |
  213.                         |           ``                |
  214.                         |_____________________________|
  215. ''',
  216.                     
  217. r'''               
  218.                          _____________________________
  219.                         |               |             |
  220.                         |               |             |
  221.                         |              ---            |
  222.                         |             /* *\           |
  223.                         |             \ ~ /           |
  224.                         |              ---            |
  225.                         |           // ===\\          |
  226.                         |           // === \\         |
  227.                         |           *  ===  *         |
  228.                         |             // \\           |
  229.                         |            //   \\          |
  230.                         |           ``     ``         |
  231.                         |_____________________________|
  232. ''')

  233. pattern_2 = (
  234. '''








  235.    
  236.   O                                                          .------.     
  237. |||                                                        /  [] [] \
  238.   |                                                     ----          \
  239. | |                                                   /__(O)_____(O)__.
  240. ___________________________________________________________________________
  241. ''',
  242. '''








  243.    
  244.   O                                                    .------.     
  245. |||                                                  /  [] [] \
  246.   |                                               ----          \
  247. | |                                             /__(O)_____(O)__.
  248. ___________________________________________________________________________
  249. ''',
  250. '''








  251.    
  252.   O                                              .------.     
  253. |||                                            /  [] [] \
  254.   |                                         ----          \
  255. | |                                       /__(O)_____(O)__.
  256. ___________________________________________________________________________
  257. ''',
  258. '''








  259.    
  260.   O                                       .------.     
  261. /|\                                     /  [] [] \
  262.   |                                  ----          \
  263. | |                                /__(O)_____(O)__.
  264. ___________________________________________________________________________
  265. ''',
  266. '''








  267.    
  268.   O                               .------.   
  269. \|/                             /  [] [] \
  270.   |                          ----          \
  271. | |                        /__(O)_____(O)__.
  272. ___________________________________________________________________________
  273. ''',
  274. '''








  275.    
  276.   O                       .------.     
  277. \|/                     /  [] [] \
  278.   |                  ----          \
  279. / \                /__(O)_____(O)__.
  280. ___________________________________________________________________________
  281. ''',
  282. '''








  283.    
  284.   O              .------.     
  285. \|/            /  [] [] \
  286.   |         ----          \
  287. / \       /__(O)_____(O)__.
  288. ___________________________________________________________________________
  289. ''',
  290. '''








  291.    
  292.               .------.     
  293.    \    /    /  [] [] \
  294. O-----   ---          \
  295.   /    \/__(O)_____(O)__.
  296. ___________________________________________________________________________
  297. ''')

  298. Main( )
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-10-24 12:04 |只看该作者
运行了一下,显示

Traceback (most recent call last):
  File "hangman.py", line 8, in <module>
    db = shelve.open('/home/jack/project/protector_lib')
  File "/usr/lib/python2.6/shelve.py", line 239, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/lib/python2.6/shelve.py", line 223, in __init__
    Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
  File "/usr/lib/python2.6/anydbm.py", line 83, in open
    return mod.open(file, flag, mode)
  File "/usr/lib/python2.6/dbhash.py", line 19, in open
    return bsddb.hashopen(file, flag, mode)
  File "/usr/lib/python2.6/bsddb/__init__.py", line 361, in hashopen
    d.open(file, db.DB_HASH, flags, mode)
bsddb.db.DBNoSuchFileError: (2, 'No such file or directory')

论坛徽章:
0
3 [报告]
发表于 2012-10-24 12:12 |只看该作者
db = shelve.open('/home/jack/project/protector_lib')這是我自己的路徑
請修改你自己的路徑!!

论坛徽章:
0
4 [报告]
发表于 2012-10-24 12:22 |只看该作者
回复 3# dreamlearn

程序的鲁邦性,有待提高。不能打几个键就报错退出呀!
   

论坛徽章:
0
5 [报告]
发表于 2012-10-24 12:25 |只看该作者
回复 4# Hadron74
用一天完成,沒有測試,所以......sorry

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
6 [报告]
发表于 2012-10-24 16:03 |只看该作者
额, 为什么代码量那么小。。。啥游戏。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP