import random import gradio as gr import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import matplotlib.patches as patches import numpy as np import time # ---- Option A board values ---- board_values = [ 10, 19, 28, 35, 44, 53, 60, 70, 79, 87, 95, 104, 115, 125, 133, 140, 149, 160, 170, 171, 180 ] N = len(board_values) # Fixed colors for consistency colors = [ "#FFDDC1", "#FFD1DC", "#C1E1FF", "#C8FFC8", "#FFF4C1", "#E0C1FF", "#FFCCCC", "#CCE5FF", "#CCFFEC", "#FFDACC", "#D1FFC1", "#E0CCFF", "#FFC1E1", "#C1FFF4", "#FFD1C1", "#C1FFD1", "#DACCFF", "#FFECCC", "#CCE1FF", "#FFCCE5", "#D9FFC1" ] # ---- PLAYER POSITIONS ---- positions = [0, 0] # Player 1, Player 2 current_player = 0 # 0 = P1, 1 = P2 # ---- REQUIRED DICE TOTALS ---- required_totals = [ 9, 9, 7, 9, 9, 7, 10, 9, 8, 8, 9, 11, 10, 8, 7, 9, 12, 10, 11, 9 ] # len = N-1 # ----------------------------------------------------- # DRAW BOARD (WINNER TILE + P1/P2 LABELS) # ----------------------------------------------------- def draw_board(p1_pos, p2_pos, active): fig, ax = plt.subplots(figsize=(13, 3)) for i, (val, col) in enumerate(zip(board_values, colors)): # Tile background rect = patches.Rectangle((i, 0), 1, 1, edgecolor="black", facecolor=col) ax.add_patch(rect) # Player 1 label if i == p1_pos: ax.text(i + 0.5, 0.20, "P1", ha="center", va="center", fontsize=11, color="blue", fontweight="bold") # Player 2 label if i == p2_pos: ax.text(i + 0.5, 0.80, "P2", ha="center", va="center", fontsize=11, color="green", fontweight="bold") # Highlight active player's tile if (active == 0 and i == p1_pos) or (active == 1 and i == p2_pos): ax.add_patch( patches.Rectangle((i, 0), 1, 1, edgecolor="red", linewidth=3, fill=False) ) # Tile value ax.text(i + 0.5, 0.5, str(val), ha="center", va="center", fontsize=8, color="black") # ---- SPECIAL: Write "WINNER" inside tile 180 ---- if val == 180: ax.text(i + 0.5, 0.50, "WIN\nNER", ha="center", va="center", fontsize=10, color="gold", fontweight="bold") # -------- WINNER BANNER (ONLY WHEN REACHED) -------- banner = None if p1_pos == N - 1: banner = "🏆 PLAYER 1 WINS! 🏆" elif p2_pos == N - 1: banner = "🏆 PLAYER 2 WINS! 🏆" if banner: ax.text(N/2, 1.35, banner, ha="center", va="center", fontsize=24, color="gold", fontweight="bold") ax.set_xlim(0, N) ax.set_ylim(0, 1.5) ax.axis("off") fig.canvas.draw() w, h = fig.canvas.get_width_height() img = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8) img = img.reshape((h, w, 4))[:, :, :3] plt.close(fig) return img # ----------------------------------------------------- # PLAY TURN FUNCTION # ----------------------------------------------------- def play_turn(): global positions, current_player player = current_player pos = positions[player] # Already winner? if pos >= N - 1: yield f"🎉 Player {player+1} already won!", draw_board(*positions, current_player) return # ---- Dice Animation ---- for _ in range(6): d1 = random.randint(1, 6) d2 = random.randint(1, 6) anim = ( f"🎲 Rolling...\n" f"Player {player+1}\n" f"Dice: {d1}, {d2}" ) yield anim, draw_board(*positions, current_player) time.sleep(0.1) # ---- Final Roll ---- d1 = random.randint(1, 6) d2 = random.randint(1, 6) total = d1 + d2 required = required_totals[pos] if total == required: positions[player] += 1 moved = True else: moved = False # Switch turns current_player = 1 - current_player status = ( f"Player {player+1}'s turn:\n" f"🎲 Dice: {d1}, {d2} → Total = {total}\n" f"{'✅ Correct! Moved forward.' if moved else '❌ Wrong total. Stayed.'}\n\n" f"📍 P1 Pos: {positions[0]+1}/{N}\n" f"📍 P2 Pos: {positions[1]+1}/{N}\n" f"➡ Now Player {current_player+1}'s turn!" ) yield status, draw_board(*positions, current_player) # ----------------------------------------------------- # RESET GAME # ----------------------------------------------------- def reset_game(): global positions, current_player positions = [0, 0] current_player = 0 return "Game reset! Player 1 starts.", draw_board(*positions, current_player) # ----------------------------------------------------- # GRADIO UI # ----------------------------------------------------- with gr.Blocks() as demo: gr.Markdown("## 🎮 Option A Game – Two Players + Dice Animation + Winner Tile") board_image = gr.Image(label="Board", type="numpy") status_box = gr.Textbox(lines=6, label="Game Status") roll_btn = gr.Button("Roll Dice") reset_btn = gr.Button("Reset Game") roll_btn.click(play_turn, outputs=[status_box, board_image]) reset_btn.click(reset_game, outputs=[status_box, board_image]) demo.load(lambda: ("Game Ready! Player 1 starts.", draw_board(0, 0, 0)), outputs=[status_box, board_image]) demo.launch()