-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (53 loc) · 1.81 KB
/
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
import function_2048
import copy
import random
matrix = []
# Create a empty matrix
for i in range(function_2048.size):
row = []
for j in range(function_2048.size):
row.append(0)
matrix.append(row)
# Initialize the game by enter two number two into the matrix
needadd = 2
while needadd > 0:
rowNum = random.randint(0, function_2048.size - 1)
colNum = random.randint(0, function_2048.size - 1)
if matrix[rowNum][colNum] == 0:
matrix[rowNum][colNum] = function_2048.add_new()
needadd -= 1
print("Welcome to 2048! Valid input including 'w'(merge up), 'a'(merge left), 's'(merge down) or d(merge right).")
function_2048.display(matrix)
# Then we use loop to ask user to input moves
gameover = False
while not gameover:
move = input('Enter instructions.')
valid = True
tempMatrix = copy.deepcopy(matrix)
if move == 'd' or move == 'D':
matrix = function_2048.merge_right(matrix)
elif move == 'w' or move == 'W':
matrix = function_2048.merge_up(matrix)
elif move == 'a' or move == 'A':
matrix = function_2048.merge_left(matrix)
elif move == 's' or move == 'S':
matrix = function_2048.merge_down(matrix)
else:
valid = False
if not valid:
print('Please enter a valid input')
else:
# check if the move is useless
if matrix == tempMatrix:
print('Try another input.')
else:
if function_2048.if_win(matrix):
function_2048.display(matrix)
print('You won!!!')
gameover = True
else:
function_2048.add_value(matrix)
function_2048.display(matrix)
if function_2048.if_lose(matrix):
print('Sorry, you lost the game.')
gameover = True