-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
193 lines (161 loc) · 5.38 KB
/
Copy pathdemo.py
File metadata and controls
193 lines (161 loc) · 5.38 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
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
import time
import streamlit as st
import numpy as np
import pandas as pd
import torch
from torch.optim import Adam
from ai2048.game import Game
from ai2048.model import Policy, Value
@st.cache_resource
def get_policy():
policy = Policy()
checkpoint = torch.load("./ckpt-2025-03-17T23:06:44.826040-2600.pt")
policy.load_state_dict(checkpoint['policy'])
return policy
def play_game():
game = Game()
states = [game.clone().state]
while game.alive():
valid_actions = torch.tensor([float(game.valid(i)) for i in range(4)])
policy_output = get_policy()(torch.tensor(game.state), valid_actions)
action = torch.multinomial(policy_output, 1).item()
game.move(action)
states.append(game.clone().state)
return states
def display_2048_board(board):
"""
Display a 2048 game board in Streamlit with proper styling.
Parameters:
board (numpy.ndarray or list): A 4x4 array representing the 2048 game board,
where 0 represents an empty cell.
"""
# Convert to numpy array if it's a list
if isinstance(board, list):
board = np.array(board)
# Ensure the board is 4x4
if board.shape != (4, 4):
st.error("Board must be 4x4. Current shape: " + str(board.shape))
return
# Define colors for each tile value (using 2048 game colors)
color_map = {
0: "#CDC1B4", # Empty tile
2: "#EEE4DA",
4: "#EDE0C8",
8: "#F2B179",
16: "#F59563",
32: "#F67C5F",
64: "#F65E3B",
128: "#EDCF72",
256: "#EDCC61",
512: "#EDC850",
1024: "#EDC53F",
2048: "#EDC22E",
}
# For values not in our color map, use the 2048+ color
default_color = "#3C3A32"
# Convert board to DataFrame for easier styling
df = pd.DataFrame(board)
# Create a custom CSS for the game board
st.markdown("""
<style>
.tile-container {
background-color: #BBADA0;
border-radius: 6px;
padding: 10px;
width: fit-content;
}
.tile {
width: 80px;
height: 80px;
border-radius: 3px;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Arial', sans-serif;
font-weight: bold;
font-size: 24px;
text-align: center;
color: #776E65;
}
.value-0 {
color: transparent;
}
.value-high {
color: #F9F6F2;
}
</style>
""", unsafe_allow_html=True)
# Start the tile container
s = """<div class="tile-container">"""
# Create the board
for i in range(4):
# Start a row
s += '<div style="display: flex;">'
for j in range(4):
value = board[i][j]
# Determine tile color
bg_color = color_map.get(value, default_color)
# Determine text color class (dark for low values, light for high values)
text_class = "value-0" if value == 0 else "value-high" if value >= 8 else ""
# Display text as empty for 0, and actual value for others
display_text = "" if value == 0 else str(value)
# Create the tile
s += f'<div class="tile {text_class}" style="background-color: {bg_color};">{display_text}</div>'
s += '</div>'
s += "</div>"
st.markdown(s, unsafe_allow_html=True)
# Display score information
total_score = np.sum(board)
max_tile = np.max(board)
st.markdown("---")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Moves made", st.session_state.index)
with col2:
st.metric("Highest Tile", max_tile)
with col3:
st.metric("Game Length", len(states) - 1)
@st.cache_data
def winner_game():
game = play_game()
while max(np.array(game[-1]).flatten()) < 2048:
game = play_game()
return game
if __name__ == "__main__":
# Streamlit UI
st.title("2048 Game Viewer")
if st.button("Regenerate") or "game" not in st.session_state:
# game = play_game()
# while max(np.array(game[-1]).flatten()) < 2048:
# game = play_game()
st.session_state.game = winner_game()
st.session_state.index = len(st.session_state.game) - 1
states = st.session_state.game
# Initialize session state
if 'index' not in st.session_state:
st.session_state.index = 0
cols = st.columns(4)
with cols[0]:
if st.button("Start"):
st.session_state.index = 0
with cols[1]:
if st.button("Previous"):
st.session_state.index = max(0, st.session_state.index - 1)
with cols[2]:
if st.button("Next"):
st.session_state.index = min(len(states) - 1, st.session_state.index + 1)
with cols[3]:
if st.button("End"):
st.session_state.index = len(states) - 1
if st.button("Auto play"):
st.session_state.auto_play = True
# Display the current state
current_state = states[st.session_state.index]
display_2048_board(current_state)
if "auto_play" in st.session_state and st.session_state.auto_play:
if st.session_state.index == len(states) - 1:
st.session_state.auto_play = False
else:
st.session_state.index += 1
time.sleep(0.01)
st.rerun()