import random
def printBoard(board,score):
for line in board:
#print " ".join(map(str,line))
def transform(x):
if x == "0":
return "_"
else:
return x
print "%5s%5s%5s%5s" % tuple( map(transform,[str(i) for i in line]) )
print "\n\t\tscore : " , score
def initBoard():
global board
board = [[0] * 4 for i in range(4)]
a,b = random.randint(0,3) ,random.randint(0,3)
board[a][b] = 2
a,b = random.randint(0,3) ,random.randint(0,3)
while board[a][b] == 2 :
a,b = random.randint(0,3) ,random.randint(0,3)
board[a][b] = random.choice([2,4])
return board
def inBoard(m,n):
return 0 <= m and m < 4 and 0 <= n and n < 4
def neighbors(i,j):
result = []
directions = [(1,0),(-1,0),(0,1),(0,-1)]
for d in directions:
m,n = i + d[0],j + d[1]
while inBoard(m,n):
if board[m][n] != 0:
result.append(board[m][n])
break
m,n = m + d[0],n + d[1]
return result
def findSameBlock(i,j,direction,markBoard):
global score
m,n = i + direction[0],j + direction[1]
while inBoard(m,n):
if board[m][n] == 0:
m,n = m + direction[0],n+direction[1]
continue
elif board[m][n] != board[i][j]:
return None
elif board[m][n] == board[i][j]:
if markBoard[i][j]: return None
board[i][j] = 0
board[m][n] *= 2
score += board[m][n]
markBoard[m][n] = True
return (m,n)
return None
def merge(direction):
markBoard = [[False] * 4 for i in range(4)]
for i in range(4):
for j in range(4):
if board[i][j] != 0:
findSameBlock(i,j,direction,markBoard)
def detectGameOver():
global win
for i in range(4):
for j in range(4):
num = board[i][j]
if num == 2048 and not win:
print "You Win"
win = True
if num == 0:
return False
for k in neighbors(i,j):
if k == num:
return False
return True
def spawnNewBlock():
a,b = random.randint(0,3) ,random.randint(0,3)
while board[a][b] != 0:
a,b = random.randint(0,3) ,random.randint(0,3)
board[a][b] = random.choice([2,4])
def same(b1,b2):
for i in range(4):
for j in range(4):
if b1[i][j] != b2[i][j]: return False
return True
def move(d):
global board,notChanged
tempBoard = [[0] * 4 for i in range(4)]
cols = [[board[i][0] for i in range(4) if board[i][0] != 0],
[board[i][1] for i in range(4) if board[i][1] != 0],
[board[i][2] for i in range(4) if board[i][2] != 0],
[board[i][3] for i in range(4) if board[i][3] != 0]]
if d == "w":
for i in range(4):
for j in range(len(cols[i])):
tempBoard[j][i] = cols[i][j]
if d == "s":
for k in cols:
k.reverse()
for i in range(4):
for j in range(len(cols[i])):
tempBoard[3 - j][i] = cols[i][j]
rows = [[board[0][i] for i in range(4) if board[0][i] != 0],
[board[1][i] for i in range(4) if board[1][i] != 0],
[board[2][i] for i in range(4) if board[2][i] != 0],
[board[3][i] for i in range(4) if board[3][i] != 0]]
if d == "a":
for i in range(4):
for j in range(len(rows[i])):
tempBoard[i][j] = rows[i][j]
if d == "d":
for k in rows:
k.reverse()
for i in range(4):
for j in range(len(rows[i])):
tempBoard[i][3 - j] = rows[i][j]
#print board
#print tempBoard
notChanged = same(tempBoard,board)
board = tempBoard
def react(key):
handle = {
"w" : (-1, 0),
"a" : (0, -1),
"s" : (1, 0),
"d" : (0, 1)
}
if not handle.has_key(key) : return
merge(handle[key])
move(key)
def __main__():
initBoard()
global board,gameOver,score,notChanged,win
score = 0
gameOver = False
notChanged = False
win = False
while not gameOver:
printBoard(board,score)
key = raw_input().strip()
if key not in "wasd":continue
react(key.strip())
gameOver = detectGameOver()
if not gameOver and not notChanged :spawnNewBlock()
print "\n\nYou Lose!\n\n"
__main__()
闲的蛋疼,撸了一个。。、