230 lines
7.5 KiB
Python
230 lines
7.5 KiB
Python
# -*- coding:utf-8 -*-
|
||
|
||
import curses
|
||
from numpy import *
|
||
from random import * # generate and place new tile
|
||
from collections import defaultdict
|
||
import os, sys
|
||
import random
|
||
import msvcrt
|
||
|
||
def move_row(row):
|
||
new_row = [i for i in row if i != 0]
|
||
new_row += [0 for i in range(len(row) - len(new_row))]
|
||
return new_row
|
||
def transpose_matrix(matrix):
|
||
new_matrix = [list(row) for row in array(matrix).T]
|
||
return new_matrix
|
||
def invert_matrix(matrix):
|
||
new_matrix = [row[::-1] for row in matrix]
|
||
return new_matrix
|
||
|
||
class Game_state(object):
|
||
"""docstring for game_state"""
|
||
game_matrix = []
|
||
win_score = 2048
|
||
score = 0
|
||
win = 0
|
||
game_over = 0
|
||
win_str = "Congratulations You Win!"
|
||
game_over_str = "Game Over!"
|
||
help_str = "press w+a+s+d to move and press q to exit r to restart!"
|
||
actions = ["up", "down", "left", "right", "restart", "exit"]
|
||
letter_codes = [ch for ch in "WSADRQwsadrq"]
|
||
actions_dict = dict(zip(letter_codes, actions * 2))
|
||
def __init__(self):
|
||
super(Game_state, self).__init__()
|
||
def reset(self):
|
||
self.game_matrix = [[0] * 4, [0] * 4, [0] * 4, [0] * 4]
|
||
for x in [0, 1]:
|
||
random_int = random.choice([2, 4])
|
||
row = random.randint(0, 3)
|
||
column = random.randint(0, 3)
|
||
while self.game_matrix[row][column] != 0:
|
||
row = random.randint(0, 3)
|
||
column = random.randint(0, 3)
|
||
self.game_matrix[row][column] = random_int
|
||
self.game_matrix = self.game_matrix
|
||
return 1
|
||
|
||
def is_win(self):
|
||
if self.score == self.win_score:
|
||
self.win = 1
|
||
return 1
|
||
return 0
|
||
|
||
# <20><><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>п<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>0
|
||
def check_null(self):
|
||
for r in self.game_matrix:
|
||
for i in r:
|
||
if i == 0:
|
||
return 1
|
||
return 0
|
||
|
||
# <20><><EFBFBD><EFBFBD>Ƿ<EFBFBD>gameover<65><72><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>0
|
||
def is_game_over(self):
|
||
# <20><><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ڿ<EFBFBD><DABF>Ժϲ<D4BA><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
||
def check_is_merge(self):
|
||
flag = 0
|
||
def is_merge(matrix):
|
||
for row in matrix:
|
||
row = move_row(row)
|
||
i = len(row) - 1
|
||
while i > 0:
|
||
if row[i] == row[i - 1] and row[i] != 0:
|
||
return 1
|
||
i = i - 1
|
||
return 0
|
||
flag = is_merge(self.game_matrix)
|
||
if flag == 1:return flag
|
||
flag = is_merge(transpose_matrix(self.game_matrix))
|
||
if flag == 1:return flag
|
||
flag = is_merge(invert_matrix(self.game_matrix))
|
||
if flag == 1:return flag
|
||
flag = is_merge(invert_matrix(transpose_matrix(self.game_matrix)))
|
||
return flag
|
||
flag = self.check_null() + check_is_merge(self)
|
||
if flag != 0:
|
||
return 0
|
||
self.game_over = 1
|
||
return 1
|
||
# <20><><EFBFBD>Ŀǰ<C4BF>ĵ÷<C4B5>
|
||
def get_score(self, matrix):
|
||
# print matrix
|
||
max = 0
|
||
for row in matrix:
|
||
for i in row:
|
||
if i > max:
|
||
max = i
|
||
self.score = max
|
||
return self.score
|
||
|
||
# <20><><EFBFBD><EFBFBD>һ<EFBFBD>θ<EFBFBD><CEB8>£<EFBFBD><C2A3><EFBFBD>game<6D><65><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֵ <20><><EFBFBD><EFBFBD><EFBFBD>³ɹ<C2B3><C9B9><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
||
def update_matrix(self, matrix):
|
||
if self.check_null() == 0:
|
||
return 0
|
||
random_int = random.choice([2, 4])
|
||
row = random.randint(0, 3)
|
||
column = random.randint(0, 3)
|
||
# print matrix
|
||
while matrix[row][column] != 0:
|
||
row = random.randint(0, 3)
|
||
column = random.randint(0, 3)
|
||
self.game_matrix[row][column] = random_int
|
||
return 1
|
||
|
||
# һ<>θ<EFBFBD><CEB8>£<EFBFBD><C2A3><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ
|
||
|
||
def one_step(self, direction):
|
||
# print self.game_matrix
|
||
def left_matrix(matrix):
|
||
flag = 0
|
||
def merge_row(row):
|
||
i = len(row) - 1
|
||
while i > 0:
|
||
if row[i] == row[i - 1] and row[i] != 0:
|
||
flag = 1
|
||
row[i - 1] *= 2
|
||
row[i] = 0
|
||
row = move_row(row)
|
||
i = len(row) - 1
|
||
continue
|
||
i = i - 1
|
||
return row
|
||
|
||
new_matrix = []
|
||
for row in matrix:
|
||
new_matrix.append(merge_row(move_row(row)))
|
||
self.game_matrix = new_matrix
|
||
return flag
|
||
|
||
if self.actions_dict[direction] == "left":
|
||
left_matrix(self.game_matrix)
|
||
# print self.game_matrix
|
||
elif self.actions_dict[direction] == "right":
|
||
self.game_matrix = invert_matrix(self.game_matrix)
|
||
left_matrix(self.game_matrix)
|
||
self.game_matrix = invert_matrix(self.game_matrix)
|
||
elif self.actions_dict[direction] == "up":
|
||
self.game_matrix = transpose_matrix(self.game_matrix)
|
||
left_matrix(self.game_matrix)
|
||
self.game_matrix = transpose_matrix(self.game_matrix)
|
||
elif self.actions_dict[direction] == "down":
|
||
self.game_matrix = transpose_matrix(self.game_matrix)
|
||
self.game_matrix = invert_matrix(self.game_matrix)
|
||
left_matrix(self.game_matrix)
|
||
self.game_matrix = invert_matrix(self.game_matrix)
|
||
self.game_matrix = transpose_matrix(self.game_matrix)
|
||
self.get_score(self.game_matrix)
|
||
self.is_win()
|
||
self.is_game_over()
|
||
self.update_matrix(self.game_matrix)
|
||
return 1
|
||
|
||
def which_step(self, direction):
|
||
if self.actions_dict[direction] == "exit":
|
||
sys.exit(0)
|
||
elif self.actions_dict[direction] == "restart":
|
||
self.reset()
|
||
else :
|
||
self.one_step(direction)
|
||
return 1
|
||
|
||
def draw_game(self):
|
||
def clear():
|
||
if os.name == 'posix':
|
||
os.system('clear')
|
||
elif os.name == 'nt':
|
||
os.system('cls')
|
||
else:
|
||
import curses
|
||
curses.setupterm()
|
||
lines = curses.tigetnum('lines')
|
||
for x in xrange(lines):
|
||
print
|
||
print '\x1b[H\x1b2J'
|
||
# os.system('cls')
|
||
clear()
|
||
print "\n"
|
||
print self.help_str
|
||
print "\n"
|
||
print "score:%s" % (self.score)
|
||
|
||
for r in self.game_matrix:
|
||
print "+-------+-------+-------+-------+"
|
||
for i in r:
|
||
if i==0:print "| \t" ,
|
||
else:print "|%d\t" % (i),
|
||
print "|"
|
||
print "+-------+-------+-------+-------+"
|
||
if self.win == 1:
|
||
print self.win_str
|
||
if self.game_over == 1:
|
||
print self.game_over_str
|
||
|
||
return 1
|
||
|
||
# 监控键盘输入
|
||
def kbfunc():
|
||
x = msvcrt.kbhit()
|
||
if x:
|
||
ret = msvcrt.getch()
|
||
else:
|
||
ret = 0
|
||
return ret
|
||
|
||
# 开始状态循环
|
||
def start_state():
|
||
one_game = Game_state()
|
||
one_game.reset()
|
||
one_game.draw_game()
|
||
one_game.which_step('a')
|
||
while 1:
|
||
x = kbfunc()
|
||
if x in one_game.letter_codes:
|
||
one_game.which_step(x)
|
||
one_game.draw_game()
|
||
return 1
|
||
if __name__ == '__main__':
|
||
start_state()
|