-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_2048.py
132 lines (116 loc) · 3.86 KB
/
play_2048.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# /usr/bin python
# -*- coding: utf-8 -*-
import numpy as np
import pythoncom
import pyHook
import random
import sys
# global variable
enter_key = ''
grid_core = "list_version" # "numpy_version"
grid_board = [[0,0,0,0] for j in range(4)] if grid_core == "list_version" else np.zeros((4,4))
state = 'start'
def row_Display_Format(value):
if not value:
return " "
else:
return value
def display_Grid():
global grid_board
for row in range(4):
row_grid_board = map(str,map(row_Display_Format,grid_board[row]))
row_grid_board.insert(0,' ')
row_grid_board.append(' ')
print " +-----+-----+-----+-----+"
print ' | '.join(row_grid_board)
print " +-----+-----+-----+-----+"
def grid_Rand_Fill(choice,key=None):
global grid_board
blank_block = [] # blank block which equals to 0
# record each block whose value equals to 0
for row in range(4):
for col in range(4):
if not grid_board[row][col]:
blank_block.append(row*4+col)
if len(blank_block):
# random choose a blank block
block_to_fill = random.choice(blank_block)
row_to_fill = block_to_fill / 4
col_to_fill = block_to_fill % 4
grid_board[row_to_fill][col_to_fill] = random.choice([2,4])
else:
return True
return False
def shiftRow(lst,direction):
# get non-zero element
shift_row_list = filter(lambda x: x != 0, lst)
# fill zero to len=4
while len(shift_row_list) < 4:
shift_row_list.append(0)
return shift_row_list
# merge row with enter_key=left
def mergeRow():
global grid_board
shift_row_list= []
merge_row_list = []
for row in range(4):
grid_board[row] = shiftRow(grid_board[row], 'A')
# display_Grid() # debug
print # debug
for row in range(4):
for col in range(4):
if col <= 2 and (grid_board[row][col]) and (grid_board[row][col] == grid_board[row][col + 1]):
grid_board[row][col] *= 2
# remove element in case merging with next one
grid_board[row][col + 1] = 0
merge_row_list.append(grid_board[row][col])
# merge row, then assign to grid_board row
grid_board[row] = shiftRow(merge_row_list, 'A')
merge_row_list = []
def transferGrid(lst):
gridToLine = [lst[row][col] for col in range(4) for row in range(4)]
lineToGrid = [gridToLine[4*i:4*i+4] for i in range(4)]
return lineToGrid
def merge_Grid(enter_key):
global grid_board
# shift_row_list= []
merge_row_list = []
# shift_list_compen_zero = []
if enter_key == 'W': # up
grid_board = transferGrid(grid_board)
mergeRow()
grid_board = transferGrid(grid_board)
elif enter_key == 'S': # down
grid_board = transferGrid(grid_board)
[grid_board[row].reverse() for row in range(4)]
mergeRow()
[grid_board[row].reverse() for row in range(4)]
grid_board = transferGrid(grid_board)
elif enter_key == 'A': # left
mergeRow()
elif enter_key == 'D': # right
[grid_board[row].reverse() for row in range(4)]
mergeRow()
[grid_board[row].reverse() for row in range(4)]
def main(event):
global enter_key, state
enter_key = event.Key
print "enter_key:", enter_key
if state == 'start':
isOver = grid_Rand_Fill('start')
state = 'update'
else:
merge_Grid(enter_key)
isOver = grid_Rand_Fill('update')
display_Grid()
if isOver:
print "Over!"
return True
def start_det_keyboard():
hm = pyHook.HookManager()
hm.KeyDown = main
hm.HookKeyboard()
pythoncom.PumpMessages()
if __name__ == "__main__":
target_num = 32
start_det_keyboard()