python写2048小游戏
#!/usr/bin/env python
# coding=utf-8
#********************************************************
# > OS : Linux 3.2.0-60-generic #91-Ubuntu
# > Author : yaolong
# > Mail : dengyaolong@yeah.net
# > Time : 2014年06月01日 星期日 13:13:39
#********************************************************
import random
import copy
def T(a):
return a if a else ' '
def display(mtr):
print "┌"+("─"*5+"┬")*3+"─"*5+"┐"
print "│%4s │%4s │%4s │%4s │"%(T(mtr[0][0]),T(mtr[0][1]),T(mtr[0][2]),T(mtr[0][3]))
print "├"+("─"*5+"┼")*3+"─"*5+"┤"
print "│%4s │%4s │%4s │%4s │"%(T(mtr[1][0]),T(mtr[1][1]),T(mtr[1][2]),T(mtr[1][3]))
print "├"+("─"*5+"┼")*3+"─"*5+"┤"
print "│%4s │%4s │%4s │%4s │"%(T(mtr[2][0]),T(mtr[2][1]),T(mtr[2][2]),T(mtr[2][3]))
print "├"+("─"*5+"┼")*3+"─"*5+"┤"
print "│%4s │%4s │%4s │%4s │"%(T(mtr[3][0]),T(mtr[3][1]),T(mtr[3][2]),T(mtr[3][3]))
print "└"+("─"*5+"┴")*3+"─"*5+"┘"
def init():
mtr = [[0 for i in range(4)] for j in range(4)] # 小小蛋疼..
ran_pos = random.sample(range(16), 2)
mtr[ran_pos[0]/4][ran_pos[0]%4] = mtr[ran_pos[1]/4][ran_pos[1]%4] = 2
return mtr
def go_on(mtr, score):
if 2048 in mtr:
print "Great!You win!Your score is ", score
raw_input("Press any key to continue...")
exit()
if 0 in mtr:
return True
for i in range(4):
for j in range(4):
if i < 3 and mtr[i][j] == mtr[i + 1][j]:
return True
if j < 3 and mtr[i][j] == mtr[i][j + 1]:
return True
print "Gameover!"
return False
def move(mtr, dirct):
score = 0
visit = []
if dirct == 0: # left
for i in range(4):
for j in range(1, 4):
for k in range(j,0,-1):
if mtr[i][k - 1] == 0 :
mtr[i][k - 1] = mtr[i][k]
mtr[i][k] = 0
elif mtr[i][k - 1] == mtr[i][k] and 4 * i + k - 1 not in visit and 4 * i + k not in visit:
mtr[i][k - 1] *= 2
mtr[i][k] = 0
score += mtr[i][k - 1]
visit.append(4 * i + k)
visit.append(4 * i + k - 1)
# for i in range(4):
# for j in range(3):
elif dirct == 1: # down
for j in range(4):
for i in range(3, 0, -1):
for k in range(0,i):
if mtr[k+1][j] == 0:
mtr[k+1][j] = mtr[k][j]
mtr[k][j]=0
elif mtr[k+1][j]==mtr[k][j] and (4 *(k+1)+j) not in visit and (4*k+j) not in visit:
mtr[k+1][j]*=2
mtr[k][j]=0
score=mtr[k+1][j]
visit.append(4*(k)+j)
visit.append(4*(k+1)+j)