  @usuyus22/

# 2048 ## No description

Files
• main.py
main.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
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
```
```#    2048 attempt #1     #
# Made by usuyus22 (me!) #
#        17/04/18        #

# Some necessary imports for random numbers and clearing the console
import random

######### Functions for the game to work ########

# Clears the console
def clear():
try:
from replit import clear
clear()
except ModuleNotFoundError: print("\n"*100)

# Returns an empty grid
def new_grid():
return [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

# Prints the grid onto the console
def print_grid(grid):
string = "\n"
for y in range(len(grid)):
for x in range(len(grid)):
string += str(grid[y][x]) + "   "
string += "\n" * 2
print(string)

# Rotates the grid (for pile() to work)
def rotate(grid):
alternate_grid = new_grid()
for y in range(4):
for x in range(4):
alternate_grid[y][x] = grid[x][-y + 3]
return alternate_grid

# Reverses an array symmetrically
def reverse(row):
new_row =  * len(row)
for i in range(len(row)):
new_row[i] = row[len(row) - (i + 1)]
return new_row

# Piles everything on the array to the left
def pile(row):
new_row = []
for i in range(len(row)):
if row[i] != 0: new_row.append(row[i])
while len(new_row) < len(row):
new_row.append(0)
reverse(row)
return new_row

# Adds the consecutive and the same numbers together
for i in range(len(row) - 1):
row = pile(row)
if row[i] == row[i + 1]:
row[i] = row[i] + row[i + 1]
row[i + 1] = 0
return row

# Adds a random '2' or '4' to a place which isn't occupied by another number
for i in range(number):
random_number = 0
if random.random() > 0.9: random_number = 4
else: random_number = 2
x = random.randint(0, 3)
y = random.randint(0, 3)
while grid[y][x] != 0:
x = random.randint(0, 3)
y = random.randint(0, 3)
grid[y][x] = random_number
return grid

#Incomplete - will do later
def gameover(grid):
for y in range(len(grid)):
for x in range(len(grid)):
if grid[y][x] == 0: return False

left = pile_all(grid)
right = rotate(rotate(pile_all(rotate(rotate(grid)))))
down = rotate(pile_all(rotate(rotate(rotate(grid)))))
up = rotate(rotate(rotate(pile_all(rotate(grid)))))

if grid == left and grid == right and grid == up and grid == down: return True
else: return False

# Piles every single column on the grid
def pile_all(grid):
for y in range(len(grid)):
return grid

# The intro-tutorial thingy
def init():
intro_speech = [
"Welcome to my attempt of the game 2048!",
"If you don't know the game already, let me explain.",
"2048 is played on a 4×4 grid, with numbered tiles that slide smoothly when a player moves them using the four arrow keys...",
"...or in this attemt (I don't know how to use the arrow keys), the keys are up(w), down(s), right(d) and left(a).",
"Every turn, a new tile will randomly appear in an empty spot on the board with a value of either 2 or 4",
"Tiles slide as far as possible in the chosen direction until they are stopped by either another tile or the edge of the grid.",
"If two tiles of the same number collide while moving, they will merge into a tile with the total value of the two tiles that collided.",
"Your aim is to go as far as possible until there are no possible moves left.",
"I didn't put scores, but I plan to later. Sorry for that.",
"Good luck :)"
]

for sentence in intro_speech:
print(sentence, end="\n\n--------------------------------\n\n")
if input("(Type 'exit' to quit the tutorial, or just press enter to continue. )") == "exit": break
clear()

#################################################

def main():
# Initialize
clear()
init()
game = new_grid()

# Loop for each turn of the player until game is over
while not gameover(game):
clear()
print_grid(game)
key = input("\n\n==>").lower()
if key == "w":
game = rotate(game)
pile_all(game)
game = rotate(rotate(rotate(game)))
elif key == "a":
pile_all(game)
elif key == "s":
game = rotate(rotate(rotate(game)))
pile_all(game)
game = rotate(game)
elif key == "d":
game = rotate(rotate(game))
pile_all(game)
game = rotate(rotate(game))