-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.py
94 lines (83 loc) · 3.01 KB
/
2048.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
import random
class Game2048:
def __init__(self):
self.board = [[0] * 4 for _ in range(4)]
self.add_random_tile()
self.add_random_tile()
def initialize_board(self):
self.board = [[0] * 4 for _ in range(4)]
self.add_random_tile()
self.add_random_tile()
def add_random_tile(self):
empty_cells = [(i, j) for i in range(4) for j in range(4) if self.board[i][j] == 0]
if empty_cells:
i, j = random.choice(empty_cells)
self.board[i][j] = 2 if random.random() < 0.9 else 4
def print_board(self):
for row in self.board:
print('\t'.join(str(cell) if cell != 0 else '-' for cell in row))
print()
def slide_left(self, row):
merged = [False] * 4
new_row = []
for i in range(4):
if row[i] != 0:
if new_row and new_row[-1] == row[i] and not merged[i - 1]:
new_row[-1] *= 2
merged[i - 1] = True
else:
new_row.append(row[i])
new_row.extend([0] * (4 - len(new_row)))
return new_row
def move_left(self):
new_board = []
for row in self.board:
new_row = self.slide_left(row)
new_board.append(new_row)
self.board = new_board
def rotate_board(self):
self.board = [list(row) for row in zip(*self.board[::-1])]
def perform_move(self, direction):
if direction == 'up':
self.rotate_board()
self.move_left()
self.rotate_board()
elif direction == 'down':
self.rotate_board()
self.rotate_board()
self.move_left()
self.rotate_board()
self.rotate_board()
elif direction == 'left':
self.move_left()
elif direction == 'right':
self.rotate_board()
self.rotate_board()
self.rotate_board()
self.move_left()
self.rotate_board()
def is_game_over(self):
for i in range(4):
for j in range(4):
if self.board[i][j] == 0:
return False
if i > 0 and self.board[i][j] == self.board[i - 1][j]:
return False
if j > 0 and self.board[i][j] == self.board[i][j - 1]:
return False
return True
def play_game(self):
self.initialize_board()
self.print_board()
while not self.is_game_over():
direction = input("Enter move direction (up/down/left/right): ").strip().lower()
if direction in ['up', 'down', 'left', 'right']:
self.perform_move(direction)
self.add_random_tile()
self.print_board()
else:
print("Invalid direction. Please enter 'up', 'down', 'left', or 'right'.")
print("Game over!")
if __name__ == '__main__':
game = Game2048()
game.play_game()