-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conterllor.py
141 lines (117 loc) · 4.01 KB
/
Conterllor.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
133
134
135
136
137
138
139
140
141
import random
import copy
from projects_2048.Location import Location, Direction
class Conterllor:
def __init__(self):
self.__list_merge = []
self.__list_empty_location = []
self.count = 0
self.is_change = False
@property
def is_change(self):
return self.__is_change
@is_change.setter
def is_change(self, value):
self.__is_change = value
def __zero_to_end(self):
"""
将0元素移到最后
:param li:
:return:
"""
for i in range(len(self.__list_merge) - 1, -1, -1):
if self.__list_merge[i] == 0:
del self.__list_merge[i]
self.__list_merge.append(0)
def __merge_2048(self):
"""
合并元素2:
[2, 2, 0, 0] --> [4, 0, 0, 0],
[2, 0, 2, 0] --> [4, 0, 0, 0],
[2, 2 ,2, 0] --> [4, 2, 0, 0]
:param li:
:return:
"""
self.__zero_to_end()
for i in range(len(self.__list_merge) - 1):
if self.__list_merge[i] == self.__list_merge[i + 1]:
self.__list_merge[i] += self.__list_merge[i + 1]
self.count += self.__list_merge[i]
self.__list_merge[i + 1] = 0
self.__zero_to_end()
def __left_move(self, map):
"""
向左移动并合并
[2, 0, 0, 2], 4 0 0 0
[2, 2, 0, 0], 4 0 0 0
[2, 0, 4, 4], 2 8 0 0
[4, 0, 0, 2] 4 2 0 0
:param li:
:return:
"""
for i in range(len(map)):
self.__list_merge[:] = map[i]
self.__merge_2048()
map[i][:] = self.__list_merge
def __right_move(self, map):
"""
向右移动并整合
:param li:
:return:
"""
for i in range(len(map)):
self.__list_merge[:] = map[i][::-1] # 2 2 2 4
self.__merge_2048()
map[i][::-1] = self.__list_merge[:]
def __up_move(self, map):
for c in range(4):
self.__list_merge.clear()
for r in range(4):
self.__list_merge.append(map[r][c])
self.__merge_2048()
for i in range(4):
map[i][c] = self.__list_merge[i]
def __down_move(self, map):
"""
向下移动并合并
:param li:
:return:
"""
for c in range(4):
self.__list_merge.clear()
for r in range(3, -1, -1):
self.__list_merge.append(map[r][c])
self.__merge_2048()
for i in range(3, -1, -1):
map[i][c] = self.__list_merge[3 - i]
def move(self, dir, map):
original_map = copy.deepcopy(map)
if dir == Direction.up:
self.__up_move(map)
if dir == Direction.down:
self.__down_move(map)
if dir == Direction.left:
self.__left_move(map)
if dir == Direction.right:
self.__right_move(map)
self.is_change = original_map != map
def random_num(self, map):
self.__list_empty_location.clear()
for i in range(4):
for j in range(4):
if map[i][j] == 0:
loc = Location(i, j)
self.__list_empty_location.append(loc)
if len(self.__list_empty_location) == 0:
return
loc = random.choice(self.__list_empty_location)
map[loc.r_index][loc.c_index] = 4 if random.randint(1, 10) == 1 else 2
self.__list_empty_location.remove(loc)
def game_is_over(self, map):
if len(self.__list_empty_location) > 0:
return False
for i in range(4):
for j in range(3):
if map[i][j] == map[i][j + 1] or map[j][i] == map[j + 1][i]:
return False
return True