智一面的面试题提供python的测试题
使用地址:http://www.gtalent.cn/exam/interview?token=52cf92de494f4a8b6165d817a7279966

一百行代码实现的《2048》小游戏
python2.7可用,若要在3.0以上运行,需要把Tkinter的“T”改成小写
 

  1. from Tkinter import *
  2. import random
  3. import tkMessageBox   
  4.  
  5. root = Tk(className = "2048")
  6. root.geometry("320x360")
  7.  
  8. str_data=[]
  9. B_list = []
  10. frame2 = Frame(root,borderwidth = 10)
  11. dic_color = {0:'GhostWhite', 2:'AliceBlue', 4:'LightCyan', 8:'Khaki',16:'SandyBrown', 32:'Goldenrod',64:'Orange',128:'Maroon',256:'Tomato',512:'OrangeRed',1024:'FireBrick',2048:'Red'}
  12. list_data=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  13. left=0
  14. up=1
  15. right=2
  16. down=3
  17. derection_index=[   [[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]],
  18.                     [[0,4,8,12],[1,5,9,13],[2,6,10,14],[3,7,11,15]],
  19.                     [[3,2,1,0],[7,6,5,4],[11,10,9,8],[15,14,13,12]],
  20.                     [[12,8,4,0],[13,9,5,1],[14,10,6,2],[15,11,7,3]]   ]
  21.  
  22. for i in range(16):
  23.     str_data.append(StringVar())
  24.     B_list.append(Button(frame2, width=4, height=2, textvariable = str_data[i]  ,font = ("Courier 20 bold roman"),bg=dic_color[0]))
  25.     B_list[i].grid(row=i/4,column=i%4)
  26.  
  27. def CanMove(derection, index):
  28.     for i in range(1, 4):
  29.         if list_data[derection_index[derection][index][i]] == list_data[derection_index[derection][index][i-1]]:
  30.             return True
  31.         if list_data[derection_index[derection][index][i]] != 0 and list_data[derection_index[derection][index][i-1]] == 0:
  32.             return True
  33.     return False
  34.  
  35. def DealSpace(derection, index):
  36.     for i in [3,2,1]:
  37.         if list_data[derection_index[derection][index][i]] == 0:
  38.             continue
  39.         if list_data[derection_index[derection][index][i-1]] == 0:
  40.             j = i
  41.             while (j < 4):
  42.                 list_data[derection_index[derection][index][j-1]] = list_data[derection_index[derection][index][j]]
  43.                 list_data[derection_index[derection][index][j]] = 0
  44.                 j=j+1
  45.  
  46. def DealEqualNum(derection, index):
  47.     for i in [0,1,2]:
  48.         if list_data[derection_index[derection][index][i]] == list_data[derection_index[derection][index][i+1]]:
  49.             list_data[derection_index[derection][index][i]] = list_data[derection_index[derection][index][i]] * 2
  50.             list_data[derection_index[derection][index][i+1]] = 0
  51.  
  52. def Move(derection, index):
  53.     DealSpace(derection, index)
  54.     DealEqualNum(derection, index)
  55.     DealSpace(derection, index)
  56.  
  57. def Refresh():
  58.     for i in range(16):
  59.         B_list[i].configure(bg=dic_color[list_data[i]])
  60.         if list_data[i] == 0:
  61.             str_data[i].set("")
  62.             continue
  63.         str_data[i].set(str(list_data[i]))
  64.          
  65. def key_event(event):
  66.     canmovelist = []
  67.     derection_code = 9
  68.     global list_data
  69.  
  70.     if event.keycode == 37:
  71.         derection_code = left
  72.     if event.keycode == 39:
  73.         derection_code = right
  74.     if event.keycode == 38:
  75.         derection_code = up
  76.     if event.keycode == 40:
  77.         derection_code = down
  78.  
  79.     for i in range(4):
  80.         if CanMove(derection_code, i):
  81.             Move(derection_code, i)
  82.             canmovelist.append(i)
  83.  
  84.     if len(canmovelist) == 0:
  85.         return
  86.     list_data[derection_index[derection_code][canmovelist[random.randint(0,len(canmovelist)-1)]][3]] = 2
  87.     Refresh()
  88.     for i_derection in range(4):
  89.         for i_index in range(4):
  90.             if CanMove(i_derection, i_index):
  91.                 return
  92.     tkMessageBox.showinfo('Game Over','Your score is %u.\n' % sum(list_data))
  93.     list_data=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  94.     Refresh()
  95.  
  96. frame2.bind('<KeyRelease>', key_event)
  97. frame2.pack()
  98. frame2.focus_set()
  99. root.mainloop()