|
import sys
|
|
import random
|
|
from collections import Counter
|
|
from PySide6.QtWidgets import (
|
|
QApplication, QMainWindow, QVBoxLayout, QLabel, QComboBox, QPushButton, QWidget, QMessageBox, QHBoxLayout, QStatusBar, QListWidget, QListWidgetItem, QLineEdit
|
|
)
|
|
|
|
class DiceProbabilityApp(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle("Bầu Cua Tôm Cá - Xác Suất Xúc Xắc")
|
|
|
|
|
|
self.layout = QVBoxLayout()
|
|
|
|
|
|
self.label_title = QLabel("Bầu Cua Tôm Cá")
|
|
self.label_title.setStyleSheet("font-size: 24px; font-weight: bold;")
|
|
self.layout.addWidget(self.label_title)
|
|
|
|
|
|
self.label_instruction = QLabel("Chọn 3 mặt xúc xắc:")
|
|
self.layout.addWidget(self.label_instruction)
|
|
|
|
|
|
self.face_selectors = []
|
|
self.valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
|
self.opposite_faces = {
|
|
"Gà": "Nai",
|
|
"Nai": "Gà",
|
|
"Cua": "Tôm",
|
|
"Tôm": "Cua",
|
|
"Bầu": "Cá",
|
|
"Cá": "Bầu"
|
|
}
|
|
for _ in range(3):
|
|
combo_box = QComboBox()
|
|
combo_box.addItems(self.valid_faces)
|
|
combo_box.setToolTip("Chọn một mặt xúc xắc")
|
|
self.face_selectors.append(combo_box)
|
|
self.layout.addWidget(combo_box)
|
|
|
|
|
|
self.label_roll_times = QLabel("Số lần lắc:")
|
|
self.layout.addWidget(self.label_roll_times)
|
|
|
|
self.roll_times_selector = QComboBox()
|
|
self.roll_times_selector.addItems(["1", "3", "5", "10"])
|
|
self.layout.addWidget(self.roll_times_selector)
|
|
|
|
|
|
buttons_layout = QHBoxLayout()
|
|
|
|
|
|
self.button_calculate = QPushButton("Lắc Xúc Xắc")
|
|
self.button_calculate.setToolTip("Click để lắc xúc xắc")
|
|
self.button_calculate.clicked.connect(self.roll_dice)
|
|
buttons_layout.addWidget(self.button_calculate)
|
|
|
|
|
|
self.button_reset = QPushButton("Reset")
|
|
self.button_reset.setToolTip("Click để reset các lựa chọn")
|
|
self.button_reset.clicked.connect(self.reset_selections)
|
|
buttons_layout.addWidget(self.button_reset)
|
|
|
|
self.layout.addLayout(buttons_layout)
|
|
|
|
|
|
self.result_label = QLabel("")
|
|
self.layout.addWidget(self.result_label)
|
|
|
|
|
|
container = QWidget()
|
|
container.setLayout(self.layout)
|
|
self.setCentralWidget(container)
|
|
|
|
|
|
self.status_bar = QStatusBar()
|
|
self.setStatusBar(self.status_bar)
|
|
|
|
|
|
self.roll_history = []
|
|
|
|
def roll_dice(self):
|
|
|
|
faces = [combo_box.currentText() for combo_box in self.face_selectors]
|
|
roll_times = int(self.roll_times_selector.currentText())
|
|
|
|
|
|
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"
|
|
|
|
self.result_label.setText(result_text)
|
|
self.status_bar.showMessage("Lắc xúc xắc thành công!", 5000)
|
|
|
|
def reset_selections(self):
|
|
self.result_label.setText("")
|
|
self.roll_history = []
|
|
self.status_bar.showMessage("Đã reset kết quả.", 5000)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
window = DiceProbabilityApp()
|
|
window.show()
|
|
sys.exit(app.exec())
|
|
|