-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
186 lines (157 loc) · 5.35 KB
/
ui.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import sys
from PySide2.QtWidgets import QWidget, QGridLayout, QPushButton, QLabel
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import Qt
import socket, threading
class SocketChat:
def __init__(self):
self.nickname = "Hossam"
# Server Ip and Port
self.IP = "127.0.0.1"
self.PORT = 55555
self.client_socket = socket.socket(
family=socket.AF_INET, type=socket.SOCK_STREAM
)
def receive(self):
message = self.client_socket.recv(1024).decode("utf-8")
return message
def write(self, msg: str):
message = msg
self.client_socket.send(message.encode("utf-8"))
if message.startswith("/"):
self.handleCommand(message[1:])
def handleCommand(self, command: str):
if command == "exit":
return 404 # status code for exit
class Example(QWidget):
winning_states = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)],
]
def __init__(self):
super().__init__()
self.player = ""
self.turn = "X"
self.initUI()
self.x_score = 0
self.y_score = 0
self.chat_object = SocketChat()
self.chat_object.client_socket.connect(
(self.chat_object.IP, self.chat_object.PORT)
)
self.player = self.chat_object.receive()
print(f"player {self.player}")
self.player_label.setText("{}\nPlayer".format(self.player))
if self.player != self.turn:
self.otherPalyerTurn()
def initUI(self):
self.game_size = 3
self.buttons = [
[],
[],
[],
]
grid = QGridLayout()
self.setLayout(grid)
# buttons
for i in range(self.game_size):
for j in range(self.game_size):
button = QPushButton()
button.setFixedSize(200, 200)
button.clicked.connect(self.takeTurn(button, i, j))
font = button.font()
font.setPointSize(60)
button.setFont(font)
grid.addWidget(button, i, j)
self.buttons[i].append(button)
# turn label
self.turn_label = QLabel("{}\nTurn".format(self.turn))
self.player_label = QLabel("{}\nPlayer".format(self.player))
font = self.turn_label.font()
font.setPointSize(20)
self.turn_label.setFont(font)
grid.addWidget(self.turn_label, self.game_size + 1, 0)
grid.addWidget(self.player_label, self.game_size + 2, 0)
self.turn_label.setAlignment(Qt.AlignCenter)
# newgame button
button = QPushButton("New Game / Reset")
font = button.font()
font.setPointSize(15)
button.setFont(font)
button.clicked.connect(self.newGame)
grid.addWidget(button, self.game_size + 1, 1)
# who wins label
self.player_won_label = QLabel()
font = self.player_won_label.font()
font.setPointSize(15)
self.player_won_label.setFont(font)
grid.addWidget(self.player_won_label, self.game_size + 1, 2)
self.player_won_label.setAlignment(Qt.AlignCenter)
def newGame(self):
for row in self.buttons:
for btn in row:
btn.setText("")
def checkGame(self):
win = ""
for win_state in Example.winning_states:
i, j = win_state[0]
state = self.buttons[i][j].text()
if state == "":
continue
for i, j in win_state:
if state != self.buttons[i][j].text():
break
else:
win = state
print(f"'{win}' wins")
self.player_won_label.setText("{} has won".format(win))
self.newGame()
if win == "":
empty = False
for row in self.buttons:
for btn in row:
if btn.text() == "":
empty = True
if not empty:
print("draw")
self.newGame()
def _otherPalyerTurn(self):
message = self.chat_object.receive()
i, j = map(lambda x: int(x), message.split(" "))
self.buttons[i][j].setText(self.turn)
self.toggle_turn()
self.checkGame()
def otherPalyerTurn(self):
threading.Thread(target=self._otherPalyerTurn).start()
def toggle_turn(self):
if self.turn == "X":
self.turn = "O"
else:
self.turn = "X"
self.turn_label.setText("{}\nTurn".format(self.turn))
def endTurn(self):
self.toggle_turn()
self.checkGame()
self.otherPalyerTurn()
def takeTurn(self, button: QPushButton, i, j):
def action():
if self.player != self.turn:
return
if button.text() == "" and button.text() != self.player:
button.setText(self.player)
self.chat_object.write(f"{i} {j}")
self.endTurn()
return action
def main():
app = QApplication([])
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()