File size: 4,850 Bytes
67af9d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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())