Update app.py
Browse files
app.py
CHANGED
@@ -1,108 +1,357 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
|
|
|
|
|
|
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 |
-
# Gradio interface
|
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 |
# Launch the app
|
108 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
from PIL import Image, ImageDraw, ImageFont
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
|
8 |
+
class LudoGame:
|
9 |
+
def __init__(self):
|
10 |
+
# Player colors
|
11 |
+
self.colors = ["red", "green", "yellow", "blue"]
|
12 |
+
self.color_codes = {
|
13 |
+
"red": "#FF5555",
|
14 |
+
"green": "#55FF55",
|
15 |
+
"yellow": "#FFFF55",
|
16 |
+
"blue": "#5555FF",
|
17 |
+
"white": "#FFFFFF",
|
18 |
+
"black": "#000000"
|
19 |
+
}
|
20 |
+
|
21 |
+
# Game state
|
22 |
+
self.reset_game()
|
23 |
+
|
24 |
+
def reset_game(self):
|
25 |
+
# Initialize game state
|
26 |
+
self.current_player = 0
|
27 |
+
self.dice_value = 1
|
28 |
+
self.dice_rolled = False
|
29 |
+
self.winner = None
|
30 |
+
|
31 |
+
# Initialize tokens (4 tokens per player)
|
32 |
+
self.tokens = {}
|
33 |
+
for i, color in enumerate(self.colors):
|
34 |
+
self.tokens[color] = [-1, -1, -1, -1] # -1 means in home
|
35 |
+
|
36 |
+
# Track if player can play after rolling
|
37 |
+
self.can_play = False
|
38 |
+
|
39 |
+
# Game messages
|
40 |
+
self.message = f"Game started! {self.colors[self.current_player].capitalize()}'s turn to roll."
|
41 |
+
|
42 |
+
def roll_dice(self):
|
43 |
+
"""Roll the dice and return the value"""
|
44 |
+
if self.winner:
|
45 |
+
return self.render_board()
|
46 |
+
|
47 |
+
if self.dice_rolled:
|
48 |
+
self.message = f"You already rolled a {self.dice_value}. Please move a token or pass."
|
49 |
+
return self.render_board()
|
50 |
+
|
51 |
+
self.dice_value = random.randint(1, 6)
|
52 |
+
self.dice_rolled = True
|
53 |
+
|
54 |
+
# Check if player can move any token
|
55 |
+
self.can_play = self._can_play()
|
56 |
+
|
57 |
+
if not self.can_play:
|
58 |
+
# If player rolled 6, give them another turn
|
59 |
+
if self.dice_value == 6:
|
60 |
+
self.message = f"{self.colors[self.current_player].capitalize()} rolled a 6 but can't move. Roll again!"
|
61 |
+
self.dice_rolled = False
|
62 |
+
else:
|
63 |
+
self.message = f"{self.colors[self.current_player].capitalize()} rolled {self.dice_value} but can't move. Next player's turn."
|
64 |
+
self._next_player()
|
65 |
+
else:
|
66 |
+
self.message = f"{self.colors[self.current_player].capitalize()} rolled {self.dice_value}. Choose a token to move."
|
67 |
+
|
68 |
+
return self.render_board()
|
69 |
+
|
70 |
+
def _can_play(self):
|
71 |
+
"""Check if current player can move any token"""
|
72 |
+
current_color = self.colors[self.current_player]
|
73 |
+
tokens = self.tokens[current_color]
|
74 |
+
|
75 |
+
for i, position in enumerate(tokens):
|
76 |
+
if position == -1 and self.dice_value == 6:
|
77 |
+
# Can move out of home
|
78 |
+
return True
|
79 |
+
elif position >= 0:
|
80 |
+
# Token is already on the board
|
81 |
+
return True
|
82 |
+
|
83 |
+
return False
|
84 |
+
|
85 |
+
def move_token(self, token_idx):
|
86 |
+
"""Move the selected token for the current player"""
|
87 |
+
if self.winner:
|
88 |
+
return self.render_board()
|
89 |
+
|
90 |
+
if not self.dice_rolled:
|
91 |
+
self.message = "Please roll the dice first."
|
92 |
+
return self.render_board()
|
93 |
+
|
94 |
+
if not self.can_play:
|
95 |
+
self.message = "You can't move any token. Please pass your turn."
|
96 |
+
return self.render_board()
|
97 |
+
|
98 |
+
current_color = self.colors[self.current_player]
|
99 |
+
current_pos = self.tokens[current_color][token_idx]
|
100 |
+
|
101 |
+
# Token still in home and not rolled a 6
|
102 |
+
if current_pos == -1 and self.dice_value != 6:
|
103 |
+
self.message = "Need to roll a 6 to move a token out of home."
|
104 |
+
return self.render_board()
|
105 |
+
|
106 |
+
# Token in home and rolled a 6
|
107 |
+
if current_pos == -1 and self.dice_value == 6:
|
108 |
+
# Put token on the board at starting position (different for each player)
|
109 |
+
self.tokens[current_color][token_idx] = self.current_player * 13
|
110 |
+
self.message = f"{current_color.capitalize()} token {token_idx+1} is now on the board."
|
111 |
+
self.dice_rolled = False
|
112 |
+
return self.render_board()
|
113 |
+
|
114 |
+
# Token already on board
|
115 |
+
new_pos = (current_pos + self.dice_value) % 52
|
116 |
+
|
117 |
+
# Check for token captures (simplistic implementation)
|
118 |
+
for color in self.colors:
|
119 |
+
if color != current_color:
|
120 |
+
for i, pos in enumerate(self.tokens[color]):
|
121 |
+
if pos == new_pos:
|
122 |
+
# Capture token
|
123 |
+
self.tokens[color][i] = -1
|
124 |
+
self.message = f"{current_color.capitalize()} captured {color}'s token!"
|
125 |
+
|
126 |
+
# Move token
|
127 |
+
self.tokens[current_color][token_idx] = new_pos
|
128 |
+
|
129 |
+
# Check if the player has won (simplistic - all tokens completed a full circle)
|
130 |
+
if self._check_winner():
|
131 |
+
self.winner = self.current_player
|
132 |
+
self.message = f"{current_color.capitalize()} wins the game!"
|
133 |
+
else:
|
134 |
+
self.message = f"{current_color.capitalize()} moved token {token_idx+1} to position {new_pos}."
|
135 |
+
|
136 |
+
# If player rolled 6, give them another turn
|
137 |
+
if self.dice_value == 6:
|
138 |
+
self.message += " Roll again!"
|
139 |
+
else:
|
140 |
+
self._next_player()
|
141 |
+
|
142 |
+
self.dice_rolled = False
|
143 |
+
return self.render_board()
|
144 |
+
|
145 |
+
def _check_winner(self):
|
146 |
+
"""Very simple win check - if all tokens made a complete circuit"""
|
147 |
+
current_color = self.colors[self.current_player]
|
148 |
+
starting_pos = self.current_player * 13
|
149 |
+
|
150 |
+
for pos in self.tokens[current_color]:
|
151 |
+
if pos < starting_pos: # Simplified win condition
|
152 |
+
return False
|
153 |
+
|
154 |
+
return True
|
155 |
+
|
156 |
+
def _next_player(self):
|
157 |
+
"""Move to the next player's turn"""
|
158 |
+
self.current_player = (self.current_player + 1) % 4
|
159 |
+
self.dice_rolled = False
|
160 |
+
self.message += f" {self.colors[self.current_player].capitalize()}'s turn to roll."
|
161 |
+
|
162 |
+
def pass_turn(self):
|
163 |
+
"""Pass the current player's turn"""
|
164 |
+
if self.winner:
|
165 |
+
return self.render_board()
|
166 |
+
|
167 |
+
if not self.dice_rolled:
|
168 |
+
self.message = "Please roll the dice first."
|
169 |
+
return self.render_board()
|
170 |
+
|
171 |
+
if self.can_play:
|
172 |
+
self.message = "You have valid moves available. Please move a token."
|
173 |
+
return self.render_board()
|
174 |
+
|
175 |
+
self._next_player()
|
176 |
+
return self.render_board()
|
177 |
+
|
178 |
+
def render_board(self):
|
179 |
+
"""Render the Ludo board as an image"""
|
180 |
+
# Create a new image with white background
|
181 |
+
width, height = 600, 600
|
182 |
+
board = Image.new('RGB', (width, height), color='white')
|
183 |
+
draw = ImageDraw.Draw(board)
|
184 |
+
|
185 |
+
# Draw the game board (simplified version)
|
186 |
+
# Draw the outer square
|
187 |
+
draw.rectangle([(50, 50), (550, 550)], outline='black', width=2)
|
188 |
+
|
189 |
+
# Draw the home squares for each player
|
190 |
+
home_squares = [
|
191 |
+
(50, 50, 250, 250), # Red (top-left)
|
192 |
+
(350, 50, 550, 250), # Green (top-right)
|
193 |
+
(50, 350, 250, 550), # Yellow (bottom-left)
|
194 |
+
(350, 350, 550, 550) # Blue (bottom-right)
|
195 |
+
]
|
196 |
+
|
197 |
+
for i, color in enumerate(self.colors):
|
198 |
+
draw.rectangle(home_squares[i], fill=self.color_codes[color], outline='black', width=2)
|
199 |
+
|
200 |
+
# Draw the center square
|
201 |
+
draw.rectangle([(250, 250), (350, 350)], fill='white', outline='black', width=2)
|
202 |
+
|
203 |
+
# Draw the path (simplified)
|
204 |
+
# Horizontal paths
|
205 |
+
draw.rectangle([(250, 50), (350, 250)], fill='white', outline='black', width=1) # Top
|
206 |
+
draw.rectangle([(250, 350), (350, 550)], fill='white', outline='black', width=1) # Bottom
|
207 |
+
draw.rectangle([(50, 250), (250, 350)], fill='white', outline='black', width=1) # Left
|
208 |
+
draw.rectangle([(350, 250), (550, 350)], fill='white', outline='black', width=1) # Right
|
209 |
+
|
210 |
+
# Draw the tokens
|
211 |
+
for color_idx, color in enumerate(self.colors):
|
212 |
+
for token_idx, position in enumerate(self.tokens[color]):
|
213 |
+
if position == -1:
|
214 |
+
# Token in home - draw in home area
|
215 |
+
home_x = home_squares[color_idx][0] + 50 + (token_idx % 2) * 100
|
216 |
+
home_y = home_squares[color_idx][1] + 50 + (token_idx // 2) * 100
|
217 |
+
draw.ellipse([(home_x-20, home_y-20), (home_x+20, home_y+20)],
|
218 |
+
fill=self.color_codes[color], outline='black', width=2)
|
219 |
+
else:
|
220 |
+
# Token on board - simplified position calculation
|
221 |
+
# This is a very basic mapping for illustration
|
222 |
+
board_positions = [
|
223 |
+
# Top row (left to right)
|
224 |
+
(100, 300), (150, 300), (200, 300), (250, 300), (300, 300), (350, 300), (400, 300), (450, 300), (500, 300),
|
225 |
+
# Right column (top to bottom)
|
226 |
+
(500, 350), (500, 400), (500, 450), (500, 500),
|
227 |
+
# Bottom row (right to left)
|
228 |
+
(450, 500), (400, 500), (350, 500), (300, 500), (250, 500), (200, 500), (150, 500), (100, 500),
|
229 |
+
# Left column (bottom to top)
|
230 |
+
(100, 450), (100, 400), (100, 350), (100, 300),
|
231 |
+
# Inner track (simplified approximation)
|
232 |
+
# Repeat the pattern for simplicity
|
233 |
+
(100, 300), (150, 300), (200, 300), (250, 300), (300, 300), (350, 300), (400, 300), (450, 300), (500, 300),
|
234 |
+
(500, 350), (500, 400), (500, 450), (500, 500),
|
235 |
+
(450, 500), (400, 500), (350, 500), (300, 500), (250, 500), (200, 500), (150, 500), (100, 500),
|
236 |
+
(100, 450), (100, 400), (100, 350), (100, 300),
|
237 |
+
]
|
238 |
+
|
239 |
+
if position < len(board_positions):
|
240 |
+
token_x, token_y = board_positions[position]
|
241 |
+
draw.ellipse([(token_x-15, token_y-15), (token_x+15, token_y+15)],
|
242 |
+
fill=self.color_codes[color], outline='black', width=2)
|
243 |
+
# Draw token number
|
244 |
+
draw.text((token_x-5, token_y-5), str(token_idx+1), fill='black')
|
245 |
+
|
246 |
+
# Draw the dice
|
247 |
+
dice_x, dice_y = 300, 300
|
248 |
+
draw.rectangle([(dice_x-25, dice_y-25), (dice_x+25, dice_y+25)], fill='white', outline='black', width=2)
|
249 |
+
|
250 |
+
# Draw dice pips based on value
|
251 |
+
if self.dice_value == 1:
|
252 |
+
draw.ellipse([(dice_x-5, dice_y-5), (dice_x+5, dice_y+5)], fill='black')
|
253 |
+
elif self.dice_value == 2:
|
254 |
+
draw.ellipse([(dice_x-15, dice_y-15), (dice_x-5, dice_y-5)], fill='black')
|
255 |
+
draw.ellipse([(dice_x+5, dice_y+5), (dice_x+15, dice_y+15)], fill='black')
|
256 |
+
elif self.dice_value == 3:
|
257 |
+
draw.ellipse([(dice_x-15, dice_y-15), (dice_x-5, dice_y-5)], fill='black')
|
258 |
+
draw.ellipse([(dice_x-5, dice_y-5), (dice_x+5, dice_y+5)], fill='black')
|
259 |
+
draw.ellipse([(dice_x+5, dice_y+5), (dice_x+15, dice_y+15)], fill='black')
|
260 |
+
elif self.dice_value == 4:
|
261 |
+
draw.ellipse([(dice_x-15, dice_y-15), (dice_x-5, dice_y-5)], fill='black')
|
262 |
+
draw.ellipse([(dice_x+5, dice_y-15), (dice_x+15, dice_y-5)], fill='black')
|
263 |
+
draw.ellipse([(dice_x-15, dice_y+5), (dice_x-5, dice_y+15)], fill='black')
|
264 |
+
draw.ellipse([(dice_x+5, dice_y+5), (dice_x+15, dice_y+15)], fill='black')
|
265 |
+
elif self.dice_value == 5:
|
266 |
+
draw.ellipse([(dice_x-15, dice_y-15), (dice_x-5, dice_y-5)], fill='black')
|
267 |
+
draw.ellipse([(dice_x+5, dice_y-15), (dice_x+15, dice_y-5)], fill='black')
|
268 |
+
draw.ellipse([(dice_x-5, dice_y-5), (dice_x+5, dice_y+5)], fill='black')
|
269 |
+
draw.ellipse([(dice_x-15, dice_y+5), (dice_x-5, dice_y+15)], fill='black')
|
270 |
+
draw.ellipse([(dice_x+5, dice_y+5), (dice_x+15, dice_y+15)], fill='black')
|
271 |
+
elif self.dice_value == 6:
|
272 |
+
draw.ellipse([(dice_x-15, dice_y-15), (dice_x-5, dice_y-5)], fill='black')
|
273 |
+
draw.ellipse([(dice_x+5, dice_y-15), (dice_x+15, dice_y-5)], fill='black')
|
274 |
+
draw.ellipse([(dice_x-15, dice_y-5), (dice_x-5, dice_y+5)], fill='black')
|
275 |
+
draw.ellipse([(dice_x+5, dice_y-5), (dice_x+15, dice_y+5)], fill='black')
|
276 |
+
draw.ellipse([(dice_x-15, dice_y+5), (dice_x-5, dice_y+15)], fill='black')
|
277 |
+
draw.ellipse([(dice_x+5, dice_y+5), (dice_x+15, dice_y+15)], fill='black')
|
278 |
+
|
279 |
+
# Draw the current player indicator
|
280 |
+
current_color = self.colors[self.current_player]
|
281 |
+
draw.rectangle([(20, 20), (40, 40)], fill=self.color_codes[current_color], outline='black', width=2)
|
282 |
+
|
283 |
+
# Draw game message
|
284 |
+
draw.text((50, 20), self.message, fill='black')
|
285 |
+
|
286 |
+
# Convert to bytes for Gradio
|
287 |
+
img_byte_arr = io.BytesIO()
|
288 |
+
board.save(img_byte_arr, format='PNG')
|
289 |
+
img_byte_arr.seek(0)
|
290 |
+
|
291 |
+
return img_byte_arr
|
292 |
|
293 |
+
# Create the Gradio interface
|
294 |
+
def create_ludo_game():
|
295 |
+
game = LudoGame()
|
296 |
+
|
297 |
+
def roll():
|
298 |
+
return game.roll_dice()
|
299 |
+
|
300 |
+
def move_token_0():
|
301 |
+
return game.move_token(0)
|
302 |
+
|
303 |
+
def move_token_1():
|
304 |
+
return game.move_token(1)
|
305 |
+
|
306 |
+
def move_token_2():
|
307 |
+
return game.move_token(2)
|
308 |
+
|
309 |
+
def move_token_3():
|
310 |
+
return game.move_token(3)
|
311 |
+
|
312 |
+
def pass_turn():
|
313 |
+
return game.pass_turn()
|
314 |
+
|
315 |
+
def reset():
|
316 |
+
game.reset_game()
|
317 |
+
return game.render_board()
|
318 |
+
|
319 |
+
with gr.Blocks() as ludo_app:
|
320 |
+
gr.Markdown("# Ludo Game")
|
321 |
+
gr.Markdown("### A classic 4-player board game")
|
322 |
+
|
323 |
+
with gr.Row():
|
324 |
+
with gr.Column():
|
325 |
+
image_output = gr.Image(type="pil", label="Ludo Board")
|
326 |
+
|
327 |
+
with gr.Row():
|
328 |
+
roll_button = gr.Button("Roll Dice")
|
329 |
+
|
330 |
+
with gr.Row():
|
331 |
+
token1_button = gr.Button("Move Token 1")
|
332 |
+
token2_button = gr.Button("Move Token 2")
|
333 |
+
token3_button = gr.Button("Move Token 3")
|
334 |
+
token4_button = gr.Button("Move Token 4")
|
335 |
+
|
336 |
+
with gr.Row():
|
337 |
+
pass_button = gr.Button("Pass Turn")
|
338 |
+
reset_button = gr.Button("Reset Game")
|
339 |
+
|
340 |
+
# Set up button click events
|
341 |
+
roll_button.click(roll, inputs=[], outputs=[image_output])
|
342 |
+
token1_button.click(move_token_0, inputs=[], outputs=[image_output])
|
343 |
+
token2_button.click(move_token_1, inputs=[], outputs=[image_output])
|
344 |
+
token3_button.click(move_token_2, inputs=[], outputs=[image_output])
|
345 |
+
token4_button.click(move_token_3, inputs=[], outputs=[image_output])
|
346 |
+
pass_button.click(pass_turn, inputs=[], outputs=[image_output])
|
347 |
+
reset_button.click(reset, inputs=[], outputs=[image_output])
|
348 |
+
|
349 |
+
# Initialize the board on load
|
350 |
+
ludo_app.load(fn=game.render_board, inputs=None, outputs=image_output)
|
351 |
+
|
352 |
+
return ludo_app
|
353 |
|
354 |
# Launch the app
|
355 |
+
if __name__ == "__main__":
|
356 |
+
app = create_ludo_game()
|
357 |
+
app.launch()
|