kooldark commited on
Commit
de9cbdf
·
verified ·
1 Parent(s): 67af9d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -126
app.py CHANGED
@@ -1,126 +1,61 @@
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 - 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())
 
1
+ import random
2
+ from collections import Counter
3
+ import gradio as gr
4
+
5
+ class DiceProbabilityApp:
6
+ def __init__(self):
7
+ self.valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
8
+ self.roll_history = []
9
+
10
+ def roll_dice(self, face1, face2, face3, roll_times):
11
+ faces = [face1, face2, face3]
12
+ roll_times = int(roll_times)
13
+
14
+ # Simulate rolling the dice
15
+ for _ in range(roll_times):
16
+ rolled_faces = [random.choice(self.valid_faces) for _ in range(3)]
17
+ self.roll_history.extend(rolled_faces)
18
+
19
+ # Count the frequency of each face
20
+ face_counts = Counter(self.roll_history)
21
+
22
+ # Calculate the probability of each face (%)
23
+ total_rolls = len(self.roll_history)
24
+ probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()}
25
+
26
+ # Sort probabilities
27
+ sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)
28
+
29
+ # Display results
30
+ result_text = f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):\n"
31
+ for face, count in face_counts.items():
32
+ result_text += f"{face}: {count} lần\n"
33
+
34
+ result_text += "\nXác suất tổng hợp:\n"
35
+ for rank, (face, prob) in enumerate(sorted_probabilities, 1):
36
+ result_text += f"{rank}. {face}: {prob:.2f}%\n"
37
+
38
+ return result_text
39
+
40
+ def reset_selections(self):
41
+ self.roll_history = []
42
+ return "Đã reset kết quả."
43
+
44
+ app = DiceProbabilityApp()
45
+
46
+ iface = gr.Interface(
47
+ fn=app.roll_dice,
48
+ inputs=[
49
+ gr.inputs.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 1"),
50
+ gr.inputs.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 2"),
51
+ gr.inputs.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 3"),
52
+ gr.inputs.Dropdown(choices=["1", "3", "5", "10"], label="Số lần lắc")
53
+ ],
54
+ outputs="text",
55
+ live=True,
56
+ title="Bầu Cua Tôm - Xác Suất Xúc Xắc",
57
+ 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."
58
+ )
59
+
60
+ if __name__ == "__main__":
61
+ iface.launch()