- 帖子
- 8
- 精华
- 0
- 积分
- 33
- 阅读权限
- 10
- 注册时间
- 2016-7-9
- 最后登录
- 2016-9-9
|
写了一个2048的简单框架
一直是错误的结果,求debug- # -*- coding: utf-8 -*-
- import random
- list1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- print '-------2048--------'
- def newway(n):
- new_list = [2,2,4]
- list_weizhi = []
- new = random.choice(new_list)
- a = 0
- for i in n:
- a = a + 1
- if i == 0:
- list_weizhi.append(a)
- weizhi = random.choice(list_weizhi)
- n[weizhi - 1] = new
- return n
- #向列表中空项添加2或4
-
- def yidong(x,xulie):#x为原list,xulie为对应方法的序列list
- list_final = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- for m in xulie:#m为一待排序列的位置
- list_read = []
- for n in m:
- list_read.append(x[n-1])
- list_out = [0,0,0,0]
- if list_read[0]==list_read[1]==list_read[2] and list_read[3]==0:
- list_read[1],list_read[2] = 0,list_read[2]*2
- elif list_read[2]==list_read[1]==list_read[3] and list_read[0]==0:
- list_read[2],list_read[3] = 0,list_read[2]*2
- list_out = list_read
- elif list_read[2]==list_read[1]==list_read[3]==list_read[0]:
- list_read[0],list_read[1],list_read[2],list_read[3]=0,list_read[1]*2,0,list_read[3]*2
- list_out = list_read
- else:
- for weizhi in range(1,4):
- if list_read[weizhi - 1] == list_read[weizhi]:
- list_out[weizhi] = list_read[weizhi] * 2
- #实行加法
-
- save = 1
- while save == 1:
- save = 0
- for i in range(1,4):
- if list_out[i] == 0 and list_out[i - 1] != 0:
- list_out[i] = list_out[i-1]
- save = 1
- #移动过0(即空位)
- #此时获得一个储存该方向列的操作后版本的列表
-
- xgl = zip(m,list_out)
- for j,k in xgl:
- list_final[j-1] = k
- #向最终输出的list_final进行赋值
- return list_final
-
- list1 = newway(list1)
- while True:
- print list1[0:4],'\n',list1[4:8],'\n',list1[8:12],'\n',list1[12:16]
- print '输入w或a或s或d,来操控数字向上或左或下或右移动。'
- player = raw_input()
- if player == 'w':
- list1 = yidong(list1,[[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]])
- if player == 'a':
- list1 = yidong(list1,[[4,3,2,1],[8,7,6,5],[12,11,10,9],[16,15,14,13]])
- if player == 's':
- list1 = yidong(list1,[[1,5,9,13],[2,6,10,14],[3,7,11,15],[4,8,12,16]])
- if player == 'd':
- list1 = yidong(list1,[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
- list1 = newway(list1)
复制代码 |
|