-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.py
More file actions
162 lines (139 loc) · 4.67 KB
/
Copy path2048.py
File metadata and controls
162 lines (139 loc) · 4.67 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
"""
Clone of 2048 game.
"""
#import poc_2048_gui
import numeric
import random
import math
# 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
"""
result = line
for idx in range(len(result)):
found = True
# Find the first zero
if result[idx] == 0:
# check if there is any non zero after this zero
found = False # Variable to indicate if a non zero is found after a zero
dist = 0 # The distance of the non zero from the first zero
for jdx in range(idx + 1, len(result)):
if result[jdx] != 0:
found = True
dist = jdx - idx
break
if found:
# Move all element tho the left of the zero found
for kdx in range(idx, len(result) - dist):
result[kdx] = result[kdx + dist]
result[kdx + dist] = 0
# Stop if all elements after result[idx] in the list are 0
# this block is here for performance reasons - result is the same without this
if found == False:
break
# Add any same neighbor values and slide the rest
for ldx in range(len(result) - 1):
if result[ldx] == result[ldx + 1]:
for mdx in range(ldx + 1, len(result) - 1):
result[mdx] = result[mdx + 1]
result[ldx] *= 2
result[len(result)- 1] = 0
return result
class TwentyFortyEight:
"""
Class to run the game logic.
"""
def __init__(self, grid_height, grid_width):
self._height = grid_height
self._width = grid_width
self.reset()
def reset(self):
"""
Reset the game so the grid is empty.
"""
self._grid = [[0 for dummy_row in range(self._width)] for dummy_col in range(self._height)]
def __str__(self):
"""
Return a string representation of the grid for debugging.
"""
result = ""
for row in self._grid:
result += "\n" + str(row)
return result
def get_grid_height(self):
"""
Get the height of the board.
"""
return self._height
def get_grid_width(self):
"""
Get the width of the board.
"""
return self._width
def move(self, direction):
"""
Move all tiles in the given direction and add
a new tile if any tiles moved.
"""
new_d = {1: 3, 2: 1, 3: 0, 4: 2}
for dummy_kdx in range(new_d.get(direction)):
self.transpose()
for idx in range(self._height):
merge(self._grid[idx])
for dummy_jdx in range(4 - new_d.get(direction)):
self.transpose()
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.
"""
zero_found = False
row_shuf = range(self._height)
random.shuffle(row_shuf)
col_shuf = range(self._width)
random.shuffle(col_shuf)
for row in row_shuf:
for col in col_shuf:
if self.get_tile(row, col) == 0:
self.set_tile(row, col, random.choice([2, 2, 2, 2, 2, 2, 2, 2, 2, 4]))
zero_found = True
break
if zero_found == True:
break
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]
def transpose(self):
"""
Transposes the matrix in order to aply merge.
"""
temp =0
result = [[0 for dummy_row in range(self._height)] for dummy_col in range(self._width)]
for row in range(self._height):
for col in range(self._width):
result[col][self._height - row - 1] = self._grid[row][col]
self._grid = result
temp = self._height
self._height = self._width
self._width = temp
#poc_2048_gui.run_gui(TwentyFortyEight(4, 4))