|
import random |
|
from collections import Counter |
|
import gradio as gr |
|
|
|
class DiceProbabilityApp: |
|
def __init__(self): |
|
self.valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"] |
|
self.roll_history = [] |
|
|
|
def roll_dice(self, face1, face2, face3, roll_times): |
|
faces = [face1, face2, face3] |
|
roll_times = int(roll_times) |
|
|
|
|
|
for _ in range(roll_times): |
|
rolled_faces = [random.choice(self.valid_faces) for _ in range(3)] |
|
self.roll_history.extend(rolled_faces) |
|
|
|
|
|
face_counts = Counter(self.roll_history) |
|
|
|
|
|
total_rolls = len(self.roll_history) |
|
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()} |
|
|
|
|
|
sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True) |
|
|
|
|
|
result_text = f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):\n" |
|
for face, count in face_counts.items(): |
|
result_text += f"{face}: {count} lần\n" |
|
|
|
result_text += "\nXác suất tổng hợp:\n" |
|
for rank, (face, prob) in enumerate(sorted_probabilities, 1): |
|
result_text += f"{rank}. {face}: {prob:.2f}%\n" |
|
|
|
return result_text |
|
|
|
def reset_selections(self): |
|
self.roll_history = [] |
|
return "Đã reset kết quả." |
|
|
|
app = DiceProbabilityApp() |
|
|
|
iface = gr.Interface( |
|
fn=app.roll_dice, |
|
inputs=[ |
|
gr.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 1"), |
|
gr.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 2"), |
|
gr.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 3"), |
|
gr.Dropdown(choices=["1", "3", "5", "10"], label="Số lần lắc") |
|
], |
|
outputs="text", |
|
live=True, |
|
title="Bầu Cua Tôm Cá - Xác Suất Xúc Xắc", |
|
description="Chọn 3 mặt xúc xắc và số lần lắc để xem kết quả và xác suất." |
|
) |
|
|
|
iface.add_button("Quay Xúc Xắc", app.roll_dice) |
|
iface.add_button("Reset", app.reset_selections) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|