-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
165 lines (128 loc) · 4.54 KB
/
client.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
from os import fdopen
import socket
import pickle
from tic_tac_toe import TicTacToe
HOST = '192.168.43.220'
PORT = 5015
win=0
draw=0
lose=0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print(f"\nConnected to {s.getsockname()}!")
player_o = TicTacToe("O")
rematch = True
while rematch == True:
print(f"\n\n T I C - T A C - T O E ")
player_o.draw_grid()
print(f"\nWaiting for other player...")
x_symbol_list = s.recv(1024)
x_symbol_list = pickle.loads(x_symbol_list)
player_o.update_symbol_list(x_symbol_list)
while player_o.did_win("O") == False and player_o.did_win("X") == False and player_o.is_draw() == False:
print(f"\n Your turn!")
player_o.draw_grid()
player_coord = input(f"Enter coordinate: ")
player_o.edit_square(player_coord)
player_o.draw_grid()
o_symbol_list = pickle.dumps(player_o.symbol_list)
s.send(o_symbol_list)
if player_o.did_win("O") == True or player_o.is_draw() == True:
break
print(f"\nWaiting for other player...")
x_symbol_list = s.recv(1024)
x_symbol_list = pickle.loads(x_symbol_list)
player_o.update_symbol_list(x_symbol_list)
if player_o.did_win("O") == True:
print(f"Congrats, you won!")
f = open("scores.txt", "a")
f.write("client won\n")
fin = open('scores.txt', 'r')
print("========YOUR HISTORY========")
for element in fin:
print(element)
f.close()
elif player_o.is_draw() == True:
print(f"It's a draw!")
f = open("scores.txt", "a")
f.write("draw\n")
fin = open('scores.txt', 'r')
print("=====YOUR HISTORY======")
for element in fin:
print(element)
f.close()
else:
print(f"Sorry, the host won.")
f = open("scores.txt", "a")
f.write("client lost\n")
fin = open('scores.txt', 'r')
print("====YOUR HISTORY======")
for element in fin:
print(element)
f.close()
# if player_o.did_win("O") == True:
# print(f"Congrats, you won!")
# f = open("scores.txt", "a")
# # content = f.read()
# # print(content)
# # fin = open('scores.txt', 'r')
# # for element in fin:
# # if element=='1':
# # win=win+1
# # elif element=='2':
# # lose=lose+1
# # else:
# # draw=draw+1
# # f.close()
# print(win, 'matches won')
# # print(lose, 'matches lost')
# # print(draw, 'matches drawn')
# elif player_o.is_draw() == True:
# print(f"It's a draw!")
# # f = open("scores.txt", "a")
# # f.write("3\n")
# # fin = open('scores.txt', 'r')
# # for element in fin:
# # if element=='1':
# # win=win+1
# # elif element=='2':
# # lose=lose+1
# # else:
# # draw=draw+1
# # f.close()
# print(win, 'matches won')
# # print(lose, 'matches lost')
# # print(draw, 'matches drawn')
# else:
# print(f"Sorry, the host won.")
# # f = open("scores.txt", "a")
# # f.write("2\n")
# # fin = open('scores.txt', 'r')
# # for element in fin:
# # if element=='1':
# # win=win+1
# # elif element=='2':
# # lose=lose+1
# # else:
# # draw=draw+1
# # f.close()
# print(win, 'matches won')
# # print(lose, 'matches lost')
# # print(draw, 'matches drawn')
client_response = "N"
if True:
print(f"\nWould you like a rematch??")
client_response = input("(Y/N): ")
client_response = client_response.capitalize()
temp_client_resp = client_response
client_response = pickle.dumps(client_response)
s.send(client_response)
if temp_client_resp == "Y":
player_o.restart()
else:
rematch = False
else:
print(f"\nThe host does not want a rematch.")
rematch = False
spacer = input(f"\nThank you for playing!\nPress enter to quit...\n")
s.close()