设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

猜密码游戏

[复制链接]

0

主题

0

好友

6

积分

新手上路

Rank: 1

楼主
发表于 2017-8-7 22:16:11 |显示全部楼层
当时看了先生的课,就想到了这个游戏,想着把记录成绩结合进来,感觉写的有点冗长,希望先生指点。
PS:生成随机数那边就想了好久,最后写了2个生成方法,都没舍得删
  1. from random import randint


  2. def generate_1():
  3.     a = [0, 0, 0, 0]
  4.     i = 0
  5.     while i <= 3:
  6.         a[i] = randint(0,9)
  7.         if i >=1:
  8.             for j in range(0,i):
  9.                 if a[i] == a[j]:
  10.                     i -= 1
  11.                     break
  12.         i += 1
  13.     return a

  14. def generate_2():
  15.     k = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  16.     a = [0, 0, 0, 0]
  17.     for i in range(4):
  18.         n = randint(0, 9-i)
  19.         a[i] = k[n]
  20.         del k[n]
  21.     return a

  22.    
  23. def compare(a, b):
  24.     num_a = 0
  25.     num_b = 0
  26.     for i in range(4):
  27.         for j in range(4):
  28.             if int(a[i]) == b[j]:
  29.                 if i == j:
  30.                     num_a += 1
  31.                     break
  32.                 else:
  33.                     num_b += 1
  34.                     break
  35.     return [num_a, num_b]
  36.         
  37.         

  38. print '====****====guess number game====****====\n'
  39. print 'Rules:\nThe answer is 4 different numbers\n\
  40. A means number and position are all \
  41. correct\nB means only number is right\n'

  42. scores = {}
  43. name = raw_input('Input you name:\n')
  44. try:
  45.     f = file('scores.txt')
  46.     lines = f.readlines()
  47.     for l in lines:
  48.         s = l.split()
  49.         scores[s[0]] = s[1:]
  50. except:
  51.     print 'You are the first player!'
  52.    
  53. score = scores.get(name)
  54. if score is None:
  55.     score = [0, 0]
  56. game_time = score[0]
  57. s_turn = score[1]

  58. again = 'y'
  59. while again == 'y':
  60.     game_time += 1
  61.     num = generate_2()
  62.     #print num
  63.     answer = [0, 0]
  64.     guess_turn = 0
  65.     print 'Guess the numbers:\n'
  66.     while answer[0] != 4:
  67.         guess_turn += 1
  68.         print 'turn %d:' %guess_turn
  69.         guess = raw_input()
  70.         trans = guess.split(' ')
  71.         #print trans
  72.         answer = compare(trans, num)
  73.         #print reflect
  74.         if answer[0] != 4:
  75.             print '===>>>%dA%dB<<<===' %(answer[0],answer[1])
  76.         else:
  77.             break
  78.     if s_turn == 0 or s_turn > guess_turn:
  79.         s_turn = guess_turn
  80.     print 'Congratulations! %d %d %d %d is correct!\n' %(num[0], num[1], num[2], num[3])
  81.     print 'Player: %s\tGame time: %d\tCurrent \
  82. turns: %d\tShortest turns: %d' %(name, game_time, guess_turn, s_turn)
  83.     scores[name] = [str(game_time), str(s_turn)]
  84.     result = ''
  85.     for n in scores:
  86.         str_result = n + ' ' + ' '.join(scores[n]) + '\n'
  87.         result += str_result
  88.     o = file('scores.txt','w')
  89.     o.write(result)
  90.     o.close()
  91.     print 'Do you want to play it again? (y/n)'
  92.     again = raw_input()
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-4 06:53 , Processed in 0.023945 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部