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") # Main layout self.layout = QVBoxLayout() # Title label 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) # Instruction label self.label_instruction = QLabel("Chọn 3 mặt xúc xắc:") self.layout.addWidget(self.label_instruction) # Dropdown menus for dice faces 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) # Roll times selector 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 buttons_layout = QHBoxLayout() # "Roll Dice" button 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) # "Reset" button 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) # Result label self.result_label = QLabel("") self.layout.addWidget(self.result_label) # Set up main interface container = QWidget() container.setLayout(self.layout) self.setCentralWidget(container) # Status bar self.status_bar = QStatusBar() self.setStatusBar(self.status_bar) # Initialize roll history self.roll_history = [] def roll_dice(self): # Get input from user faces = [combo_box.currentText() for combo_box in self.face_selectors] roll_times = int(self.roll_times_selector.currentText()) # Simulate rolling the dice for _ in range(roll_times): rolled_faces = [random.choice(self.valid_faces) for _ in range(3)] self.roll_history.extend(rolled_faces) # Count the frequency of each face face_counts = Counter(self.roll_history) # Calculate the probability of each face (%) total_rolls = len(self.roll_history) probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()} # Sort probabilities sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True) # Display results 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) # Run application if __name__ == "__main__": app = QApplication(sys.argv) window = DiceProbabilityApp() window.show() sys.exit(app.exec())