-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048sql.py
371 lines (329 loc) · 11.4 KB
/
2048sql.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#2048 the game in python with sql leaderboards
import random
tilesize = (7, 4)
gridsize = (4, 4) # y,x
pad = 40
gamescene = ""
color = "Ñ%@#W$?|!;:+=-,._ "[::-1]
startchoice = [[2, 2], [2, 2], [4], [4, 2]]
choicies = [[2],[2],[2],[2],[4]]
score = 0
def createtile(value,tilesize=tilesize):
value = str(value)
tile = ''
n = 0
p = 1
try:
while True:
if p >= int(value):
break
n += 1
p += p
except:
n = 0
spacestr = color[n]
spaces = (tilesize[0] - 1 - len(value))//2
valueformated = spacestr*spaces + value
addspaces = tilesize[0] - 1 - len(valueformated)
valueformated = valueformated + spacestr * addspaces
for j in range(tilesize[1]):
for i in range(tilesize[0]):
if i in (0,) and j in (0,):
tile += '+'
elif i in range(0, tilesize[0]+1) and j in (0,):
tile += '-'
elif i in (0,) and j in range(0, tilesize[1]+1):
tile += '|'
elif j == tilesize[1]//2:
tile += valueformated[i-j+1]
else:
tile += spacestr
tile += '\n'
return tile
def combinetiles(grouptiles,gridsize,tilesize):
finalgrid = ''
for j in range(len(grouptiles)):
for i in range(len(grouptiles[j])):
grouptiles[j][i] = grouptiles[j][i].split('\n')
for g in range(len(grouptiles)):
tiles = grouptiles[g]
for i in range(len(tiles[0])):
for j in range(len(tiles)):
finalgrid += tiles[j][i]
if i == 0:
finalgrid += '+'
else:
finalgrid += '|'
finalgrid += '\n'
finalgrid = finalgrid.rstrip('|\n')
finalgrid += '|\n'
finalgrid += '+'
for i in range(gridsize[1]):
for j in range(tilesize[0]-1):
finalgrid += '-'
finalgrid += '+'
return finalgrid
def addrandomvalue(grid):
curchoice = random.choice(choicies)
for k in curchoice:
popablepoints = []
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 0:
popablepoints.append((i, j))
point = random.choice(popablepoints)
grid[point[0]][point[1]] = k
return grid
def addscore(value):
global score
score += value
def displaygrid(pad,grid,gridsize=gridsize,tilesize=tilesize):
global gridtiles
clearscreen()
for i in range(len(grid)):
gridtiles.append([])
for j in range(len(grid[i])):
gridtiles[i].append(createtile(grid[i][j],tilesize))
print(combinetiles(gridtiles,gridsize,tilesize))
gridtiles = []
def compresgrid(move):
def compupdown(jtupe):
for i in range(0, len(grid[0]),1):
for j in range(jtupe[0],jtupe[1],jtupe[2]):
if grid[j][i] != 0 and j != jtupe[3]:
k = j - jtupe[4]
while grid[k][i] == 0:
if k == jtupe[3]:
break
k -= jtupe[4]
else:
k += jtupe[4]
if grid[k][i] == grid[j][i]:
continue
else:
grid[k][i] = grid[j][i]
grid[j][i] = 0
if move == "w":
compupdown((0, len(grid),1,0,1))
elif move == "s":
compupdown((len(grid)-1,-1,-1,len(grid)-1,-1))
def compleftright(itupe):
for j in range(0, len(grid),1):
for i in range(itupe[0],itupe[1],itupe[2]):
if grid[j][i] != 0 and i != itupe[3]:
k = i - itupe[4]
while grid[j][k] == 0:
if k == itupe[3]:
break
k -= itupe[4]
else:
k += itupe[4]
if grid[j][k] == grid[j][i]:
continue
else:
grid[j][k] = grid[j][i]
grid[j][i] = 0
if move == "a":
compleftright((0, len(grid[0]),1,0,1))
elif move == "d":
compleftright((len(grid[0])-1,-1,-1,len(grid[0])-1,-1))
def mergegrid(move):
def mergeupdown(jtupe):
justmerged = []
for i in range(0, len(grid[0]),1):
for j in range(jtupe[0],jtupe[1],jtupe[2]):
if grid[j][i] != 0 and j != jtupe[3]:
k = j - jtupe[4]
while grid[k][i] == 0 and k != jtupe[3]:
k -= jtupe[4]
if grid[k][i] == grid[j][i] and (k,i) not in justmerged:
grid[k][i] += grid[j][i]
addscore(grid[k][i])
grid[j][i] = 0
justmerged.append((k,i))
if move == "w":
mergeupdown((0, len(grid),1,0,1))
elif move == "s":
mergeupdown((len(grid)-1,-1,-1,len(grid)-1,-1))
def mergeleftright(itupe):
justmerged = []
for j in range(0, len(grid),1):
for i in range(itupe[0],itupe[1],itupe[2]):
if grid[j][i] != 0 and i != itupe[3]:
k = i - itupe[4]
while grid[j][k] == 0 and k != itupe[3]:
k -= itupe[4]
if grid[j][k] == grid[j][i] and (j,k) not in justmerged:
grid[j][k] += grid[j][i]
addscore(grid[j][k])
grid[j][i] = 0
justmerged.append((j,k))
if move == "a":
mergeleftright((0, len(grid[0]),1,0,1))
elif move == "d":
mergeleftright((len(grid[0])-1,-1,-1,len(grid[0])-1,-1))
compresgrid(move)
gridtiles = []
grid = []
for i in range(gridsize[0]*gridsize[1]):
grid.append(0)
curchoice = random.choice(startchoice)
popablepoints = list(range(len(grid)))
for i in curchoice:
point = random.choice(popablepoints)
popablepoints.remove(point)
grid[point] = i
tempgrid = list(grid)
grid = []
c = 0
for i in range(0, len(tempgrid), gridsize[1]):
i += gridsize[1]
grid.append(tempgrid[c:i])
c = i
def clearscreen(n=40):
print("\n"*40)
def startgame():
global grid
while True:
tempgrid = []
for i in range(len(grid)):
tempgrid.append([])
for j in grid[i]:
tempgrid[i].append(j)
displaygrid(pad,grid)
print("Score:",score)
move = input(':').lower()
if move == "exit":
print("\nGame over","\nYour Score: ",score,sep='')
return score
if move not in "wasd":
continue
mergegrid(move)
if tempgrid == grid:
try:
addrandomvalue(tempgrid)
except:
print("\nGame over","\nYour Score: ",score,sep='')
return score
continue
grid = addrandomvalue(grid)
def showoptions(opt):
s = ""
choices = []
for i in range(len(opt)):
s += f"({i}): {opt[i]} \n"
choices.append(str(i))
while True:
print(s)
out = input("Choose: ")
if out in choices:
return int(out)
else:
print("Invalid Choice\n")
options = ["New game","High Scores","Help","Exit"]
import mysql.connector as sql
while True:
user = input("Sql Username (Default is root): ")
if user == "": user = "root"
try:
password = input("Sql Password (Default is root): ")
if password == "": password = "root"
con = sql.connect(host="localhost",password = password,user=user)
except sql.errors.ProgrammingError:
print("Wrong password")
except Exception as e:
print("Could not connect to mysql")
print("Error Details: " + str(e))
else:
break
cur = con.cursor()
try:
cur.execute("use 2048database")
except:
cur.execute("create database 2048database")
cur.execute("use 2048database")
try:
cur.execute("insert into data values('test','test',0)")
cur.execute("delete from data where username = 'test' and password = 'test' and highscore = 0")
except:
cur.execute("create table data(username varchar(20) primary key,password varchar(20),highscore int)")
loginoptions = ["New User","Login"]
choice = showoptions(loginoptions)
if choice == 0:
while True:
newuser = input("User Name with no spaces or special characters of 10 characters: ")
if len(newuser) > 10:
print("Username too long")
continue
try:
cur.execute(f"select * from data where username = '{newuser}'")
if len(cur.fetchall()) > 0:
print("User name already exists")
continue
else:
while True:
password = input("Password with no spaces: ")
if password == input("Retype password: "):
break
else:
print("Passwords dont match")
continue
cur.execute(f"insert into data values('{newuser}','{password}',0)")
con.commit()
user = newuser
break
except Exception as e:
print("Wrong username format")
print("More error details: " + str(e))
elif choice == 1:
while True:
try:
username = input("Username: ")
password = input("Password: ")
cur.execute(f"select * from data where username='{username}' and password='{password}'")
if len(cur.fetchall()) == 1:
user = username
break
else: print("Wrong password or username \n")
except Exception as e:
print("Error Details: " + str(e))
pass
while True:
clearscreen()
choice = showoptions(options)
if choice == 3:
print("Exiting")
exit()
if choice == 2:
print("Use w,a,s,d to move by inserting the key and pressing enter\nType exit to end game")
input("Press enter to go back to main menu")
if choice == 0:
score = startgame()
try:
cur.execute(f"select * from data where username = '{user}'")
data = list(cur.fetchall())
highscore = data[0][-1]
if score > highscore: # type: ignore
print("New Highscore")
cur.execute(f"update data set highscore = {score} where username = '{user}'")
con.commit()
input("Press enter to go back to main menu")
except Exception as e:
print("Error details: " + str(e))
input("Press enter to go back to main menu")
if choice == 1:
clearscreen()
try:
cur.execute(f"select username,highscore from data order by highscore desc")
highscores = cur.fetchall()
maxlen = 0
highscores.insert(0,("Names","High Scores"))
for i in highscores:
if len(i[0]) > maxlen: # type: ignore
maxlen = len(i[0]) # type: ignore
if len(str(i[1])) > maxlen:
maxlen = len(str(i[1]))
displaygrid(pad,highscores,gridsize=(0,2),tilesize=(maxlen + 3 , 4))
input("Press enter to go back to main menu")
except Exception as e:
print("Error Details: ",e)