Upload 2 files
Browse files- app.py +126 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import random
|
| 3 |
+
from collections import Counter
|
| 4 |
+
from PySide6.QtWidgets import (
|
| 5 |
+
QApplication, QMainWindow, QVBoxLayout, QLabel, QComboBox, QPushButton, QWidget, QMessageBox, QHBoxLayout, QStatusBar, QListWidget, QListWidgetItem, QLineEdit
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
class DiceProbabilityApp(QMainWindow):
|
| 9 |
+
def __init__(self):
|
| 10 |
+
super().__init__()
|
| 11 |
+
self.setWindowTitle("Bầu Cua Tôm Cá - Xác Suất Xúc Xắc")
|
| 12 |
+
|
| 13 |
+
# Main layout
|
| 14 |
+
self.layout = QVBoxLayout()
|
| 15 |
+
|
| 16 |
+
# Title label
|
| 17 |
+
self.label_title = QLabel("Bầu Cua Tôm Cá")
|
| 18 |
+
self.label_title.setStyleSheet("font-size: 24px; font-weight: bold;")
|
| 19 |
+
self.layout.addWidget(self.label_title)
|
| 20 |
+
|
| 21 |
+
# Instruction label
|
| 22 |
+
self.label_instruction = QLabel("Chọn 3 mặt xúc xắc:")
|
| 23 |
+
self.layout.addWidget(self.label_instruction)
|
| 24 |
+
|
| 25 |
+
# Dropdown menus for dice faces
|
| 26 |
+
self.face_selectors = []
|
| 27 |
+
self.valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
| 28 |
+
self.opposite_faces = {
|
| 29 |
+
"Gà": "Nai",
|
| 30 |
+
"Nai": "Gà",
|
| 31 |
+
"Cua": "Tôm",
|
| 32 |
+
"Tôm": "Cua",
|
| 33 |
+
"Bầu": "Cá",
|
| 34 |
+
"Cá": "Bầu"
|
| 35 |
+
}
|
| 36 |
+
for _ in range(3):
|
| 37 |
+
combo_box = QComboBox()
|
| 38 |
+
combo_box.addItems(self.valid_faces)
|
| 39 |
+
combo_box.setToolTip("Chọn một mặt xúc xắc")
|
| 40 |
+
self.face_selectors.append(combo_box)
|
| 41 |
+
self.layout.addWidget(combo_box)
|
| 42 |
+
|
| 43 |
+
# Roll times selector
|
| 44 |
+
self.label_roll_times = QLabel("Số lần lắc:")
|
| 45 |
+
self.layout.addWidget(self.label_roll_times)
|
| 46 |
+
|
| 47 |
+
self.roll_times_selector = QComboBox()
|
| 48 |
+
self.roll_times_selector.addItems(["1", "3", "5", "10"])
|
| 49 |
+
self.layout.addWidget(self.roll_times_selector)
|
| 50 |
+
|
| 51 |
+
# Buttons layout
|
| 52 |
+
buttons_layout = QHBoxLayout()
|
| 53 |
+
|
| 54 |
+
# "Roll Dice" button
|
| 55 |
+
self.button_calculate = QPushButton("Lắc Xúc Xắc")
|
| 56 |
+
self.button_calculate.setToolTip("Click để lắc xúc xắc")
|
| 57 |
+
self.button_calculate.clicked.connect(self.roll_dice)
|
| 58 |
+
buttons_layout.addWidget(self.button_calculate)
|
| 59 |
+
|
| 60 |
+
# "Reset" button
|
| 61 |
+
self.button_reset = QPushButton("Reset")
|
| 62 |
+
self.button_reset.setToolTip("Click để reset các lựa chọn")
|
| 63 |
+
self.button_reset.clicked.connect(self.reset_selections)
|
| 64 |
+
buttons_layout.addWidget(self.button_reset)
|
| 65 |
+
|
| 66 |
+
self.layout.addLayout(buttons_layout)
|
| 67 |
+
|
| 68 |
+
# Result label
|
| 69 |
+
self.result_label = QLabel("")
|
| 70 |
+
self.layout.addWidget(self.result_label)
|
| 71 |
+
|
| 72 |
+
# Set up main interface
|
| 73 |
+
container = QWidget()
|
| 74 |
+
container.setLayout(self.layout)
|
| 75 |
+
self.setCentralWidget(container)
|
| 76 |
+
|
| 77 |
+
# Status bar
|
| 78 |
+
self.status_bar = QStatusBar()
|
| 79 |
+
self.setStatusBar(self.status_bar)
|
| 80 |
+
|
| 81 |
+
# Initialize roll history
|
| 82 |
+
self.roll_history = []
|
| 83 |
+
|
| 84 |
+
def roll_dice(self):
|
| 85 |
+
# Get input from user
|
| 86 |
+
faces = [combo_box.currentText() for combo_box in self.face_selectors]
|
| 87 |
+
roll_times = int(self.roll_times_selector.currentText())
|
| 88 |
+
|
| 89 |
+
# Simulate rolling the dice
|
| 90 |
+
for _ in range(roll_times):
|
| 91 |
+
rolled_faces = [random.choice(self.valid_faces) for _ in range(3)]
|
| 92 |
+
self.roll_history.extend(rolled_faces)
|
| 93 |
+
|
| 94 |
+
# Count the frequency of each face
|
| 95 |
+
face_counts = Counter(self.roll_history)
|
| 96 |
+
|
| 97 |
+
# Calculate the probability of each face (%)
|
| 98 |
+
total_rolls = len(self.roll_history)
|
| 99 |
+
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()}
|
| 100 |
+
|
| 101 |
+
# Sort probabilities
|
| 102 |
+
sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)
|
| 103 |
+
|
| 104 |
+
# Display results
|
| 105 |
+
result_text = f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):\n"
|
| 106 |
+
for face, count in face_counts.items():
|
| 107 |
+
result_text += f"{face}: {count} lần\n"
|
| 108 |
+
|
| 109 |
+
result_text += "\nXác suất tổng hợp:\n"
|
| 110 |
+
for rank, (face, prob) in enumerate(sorted_probabilities, 1):
|
| 111 |
+
result_text += f"{rank}. {face}: {prob:.2f}%\n"
|
| 112 |
+
|
| 113 |
+
self.result_label.setText(result_text)
|
| 114 |
+
self.status_bar.showMessage("Lắc xúc xắc thành công!", 5000)
|
| 115 |
+
|
| 116 |
+
def reset_selections(self):
|
| 117 |
+
self.result_label.setText("")
|
| 118 |
+
self.roll_history = []
|
| 119 |
+
self.status_bar.showMessage("Đã reset kết quả.", 5000)
|
| 120 |
+
|
| 121 |
+
# Run application
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
app = QApplication(sys.argv)
|
| 124 |
+
window = DiceProbabilityApp()
|
| 125 |
+
window.show()
|
| 126 |
+
sys.exit(app.exec())
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio
|