Repository navigation
Expand file tree
/
Copy pathgame.py
More file actions
113 lines (89 loc) · 3.44 KB
/
Copy pathgame.py
File metadata and controls
113 lines (89 loc) · 3.44 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
import random
import c2048
class Game2048:
SIZE = 4
def __init__(self, board=0):
# The board is just a single 64-bit integer where each 4 bits represent the power of 2 for that tile.
self.board = board
# Only spawn starting tiles if it's a brand new game
if self.board == 0:
self.add_tile()
self.add_tile()
def add_tile(self):
# Find all empty 4-bit slots
empty_slots = []
for i in range(16):
# Shift the bits down and mask with 1111 (0xF) to read the 4-bit power
if (self.board >> (i * 4)) & 0xF == 0:
empty_slots.append(i)
if empty_slots:
slot = random.choice(empty_slots)
# 90% chance of 2 (power 1), 10% chance of 4 (power 2)
power = 1 if random.random() < 0.9 else 2
# OR the new tile into the correct position
self.board |= (power << (slot * 4))
def move(self, direction, add_random_tile=True):
old_board = self.board
if direction == 0:
self.board = c2048.move_up(self.board)
elif direction == 1:
self.board = c2048.move_down(self.board)
elif direction == 2:
self.board = c2048.move_left(self.board)
elif direction == 3:
self.board = c2048.move_right(self.board)
else:
raise ValueError("Direction must be 0,1,2,3")
moved = (self.board != old_board)
if moved and add_random_tile:
self.add_tile()
# We drop the 'merges' dictionary tracking here because it slows down
# the engine and your Expectimax doesn't actually use it.
return moved, []
def get_board(self):
# Returns the 64-bit integer. No more list slicing!
return self.board
def is_game_over(self):
# If no move changes the board, you are dead.
if self.board != c2048.move_up(self.board): return False
if self.board != c2048.move_down(self.board): return False
if self.board != c2048.move_left(self.board): return False
if self.board != c2048.move_right(self.board): return False
return True
def print_board(self):
for r in range(4):
row_strs = []
for c in range(4):
shift = (r * 4 + c) * 4
power = (self.board >> shift) & 0xF
if power == 0:
row_strs.append(".")
else:
# Convert power back to actual tile value (e.g., power 3 -> 1 << 3 = 8)
row_strs.append(str(1 << power))
print('\t'.join(row_strs))
print()
def clone(self):
# Cloning is now instantaneous since it's just passing an integer
return Game2048(self.board)
def play_game_human():
# Make sure to initialize the C++ tables!
c2048.init_tables()
game = Game2048()
while not game.is_game_over():
game.print_board()
try:
move = int(input("Enter 0-Up, 1-Down, 2-Left, 3-Right: "))
except ValueError:
print("Invalid input! Must be an integer 0-3.")
continue
if move not in [0, 1, 2, 3]:
print("Invalid input! Must be 0,1,2,3.")
continue
moved, _ = game.move(move)
if not moved:
print("Move didn't change the board!")
game.print_board()
print("Game Over!")
if __name__ == "__main__":
play_game_human()