-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048_New.py
More file actions
168 lines (135 loc) · 5.22 KB
/
2048_New.py
File metadata and controls
168 lines (135 loc) · 5.22 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
Clone of 2048 game.
"""
import poc_2048_gui
import random
# Directions, DO NOT MODIFY
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
# Offsets for computing tile indices in each direction.
# DO NOT MODIFY this dictionary.
OFFSETS = {UP: (1, 0),
DOWN: (-1, 0),
LEFT: (0, 1),
RIGHT: (0, -1)}
def merge(line):
"""
Helper function that merges a single row or column in 2048
"""
# replace with your code
merge_line = [0]*len(line)
last_number = 0
merge_flag = False
for idx in range(len(line)):
if (line[idx] != 0):
if ( line[idx] == last_number) and merge_flag == False:
index = merge_line.index(0)
if (index == 0):
merge_line[index-1] = 2*last_number
else:
merge_line[index-1] = 2*last_number
last_number = line[idx]
merge_flag = True
else:
index = merge_line.index(0)
merge_line[index] = line[idx]
last_number = line[idx]
merge_flag = False
return merge_line
class TwentyFortyEight:
"""
Class to run the game logic.
"""
def __init__(self, grid_height, grid_width):
self.grid_height = grid_height
self.grid_width = grid_width
self.grid = self.reset()
self.initial_tile_indices_dic = {UP: list(tuple([0, index]) for index in range(self.grid_width)),
DOWN: list(tuple([self.grid_height-1, index]) for index in range(self.grid_width)),
LEFT: list(tuple([index, 0]) for index in range(self.grid_height)),
RIGHT: list(tuple([index, self.grid_width-1]) for index in range(self.grid_height)) }
# print self.indices
def reset(self):
"""
Reset the game so the grid is empty.
"""
self.grid = [[0]*self.grid_width for _ in range(self.grid_height)]
return self.grid
def __str__(self):
"""
Return a string representation of the grid for debugging.
"""
grid_index = []
for row in range(self.grid_height):
for col in range(self.grid_width):
grid_index.append(str(row) + str(col))
return str(grid_index)
def get_grid_height(self):
"""
Get the height of the board.
"""
return self.grid_height
def get_grid_width(self):
"""
Get the width of the board.
"""
return self.grid_width
def move(self, direction):
"""
Move all tiles in the given direction and add
a new tile if any tiles moved.
"""
initial_tile_indices = self.initial_tile_indices_dic[direction]
offset = OFFSETS[direction]
temp_list = []
tiles_moved = False
for indices in initial_tile_indices:
row = indices[0]
col = indices[1]
temp_list = []
temp_indices = []
while (0 <= row < self.grid_height and 0 <= col < self.grid_width):
#print "row", row, "Col", col
temp_indices.append([row,col])
temp_list.append(self.get_tile(row, col))
row = row + offset[0]
col = col + offset[1]
merge_list = merge(temp_list)
for idx in range(len(merge_list)):
if (self.get_tile(temp_indices[idx][0],temp_indices[idx][1]) != merge_list[idx]):
tiles_moved = tiles_moved or True
self.set_tile(temp_indices[idx][0], temp_indices[idx][1], merge_list[idx])
if (tiles_moved == True):
self.new_tile()
def new_tile(self):
"""
Create a new tile in a randomly selected empty
square. The tile should be 2 90% of the time and
4 10% of the time.
"""
choice = [4, 2, 2, 2, 2, 2, 2, 2, 2, 2]
index_choice = []
for row in range(self.grid_height):
for col in range(self.grid_width):
if self.get_tile(row, col) == 0:
index_choice.append([row, col])
random_index_choice = random.choice(index_choice)
self.set_tile(random_index_choice[0], random_index_choice[1], random.choice(choice))
def set_tile(self, row, col, value):
"""
Set the tile at position row, col to have the given value.
"""
self.grid[row][col] = value
def get_tile(self, row, col):
"""
Return the value of the tile at position row, col.
"""
return self.grid[row][col]
#my_class = TwentyFortyEight(4, 4)
#print my_class
#my_class.set_tile(3,1,100)
#temp = my_class.get_tile(3, 1)
#print temp
poc_2048_gui.run_gui(TwentyFortyEight(4, 4))