-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
253 lines (206 loc) · 6.16 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os, random, sys, socket, time
try:
import winsound
platform = "windows"
except:
platform = "gnulinux"
def print_board():
for n in range(0, 9, 3):
print(board[n] + "|" + board[n+1] + "|" + board[n+2] + " ", n+1, "|", n+2, "|", n+3, sep="")
def place(counter, i):
if board[i] == empty:
board[i] = counter
return err_success
else:
return err_failure
def clear():
if idle:
print("\n"*80)
else:
if platform == "gnulinux":
os.system("clear")
else:
os.system("cls")
def ai_place(c):
available_spots = []
for i in range(9):
if board[i] == empty:
available_spots.append(i)
if available_spots:
i = random.choice(available_spots)
board[i] = c
return err_success
else:
return err_failure
def check_win():
b = board
if b[0] == b[1] and b[0] == b[2] and b[0] != empty:
return b[0]
elif b[0] == b[3] and b[0] == b[6] and b[0] != empty:
return b[0]
elif b[0] == b[4] and b[0] == b[8] and b[0] != empty:
return b[0]
elif b[1] == b[4] and b[1] == b[7] and b[1] != empty:
return b[1]
elif b[2] == b[5] and b[2] == b[8] and b[2] != empty:
return b[2]
elif b[3] == b[4] and b[3] == b[5] and b[3] != empty:
return b[3]
elif b[6] == b[4] and b[6] == b[2] and b[6] != empty:
return b[6]
elif b[6] == b[7] and b[6] == b[8] and b[6] != empty:
return b[6]
else:
return empty
def display_menu():
for i, o in enumerate(options):
print(i+1, ") ", o, sep = "")
print()
def menu():
display_menu()
choice = ""
while True:
try:
print("Choice: ", end="")
choice = int(input())
break
except:
clear()
display_menu()
continue
if choice == 1: # find a match
connect()
elif choice == 2: # play offline
offline()
elif choice == 3: # quit
quit()
def offline():
moves = 0
ai_moves = 0
player_counter = "O"
ai_counter = "X"
winner = empty
while True:
clear()
print_board()
try:
pos = int(input("Choose a position on the board: "))
except:
continue
if pos not in range(1, 10):
continue
if place(player_counter, pos-1) == err_failure:
continue
moves += 1
if check_win() == player_counter:
winner = player_counter
break
if ai_place(ai_counter) == err_failure:
break
ai_moves += 1
winner = check_win()
if winner != empty:
break
clear()
print_board()
if winner == player_counter:
print("You win!")
print("Moves:", moves)
elif winner != empty and winner != player_counter:
print("You lose!")
else:
print("It's a tie!")
input("Press enter to quit.")
quit()
def connect():
clear()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = ""
while not server:
server = input("Server address: ")
port = 45001
data_buf = 4096
print("Attempting to connect to server...")
try:
s.connect((server, port))
except:
print("Unable to connect to server.")
input("\nPress enter to quit.")
quit()
print("Connection established.")
print("Searching for an opponent...")
while True:
data = s.recv(data_buf)
if not data:
break
response = data.decode("utf-8")
if response == "ready":
print("Opponent found.")
print("Waiting for opponent to make the first move...")
while True:
data = s.recv(data_buf)
if not data:
break
response = data.decode("utf-8")
clear()
print_board()
print("Waiting for opponent to make a move...")
if "go" in response or "invalid_pos" in response:
c = response.split("||")[1] # counter
b = response.split("||")[2] # board raw data
bc = b.split(",") # board counters
i = 0
for nc in bc: # for counter in board counters
board[i] = nc# place them on the board
i += 1
print("It is your turn")
i = -1
clear()
print_board()
print("You are ", c)
while True:
try:
i = int(input("Choose a position: ")) - 1
except:
continue
if i not in range(9):
continue
break
s.send(str(i).encode())
place(c, i)
clear()
print_board()
print("Waiting for opponent to make a move...")
elif "over" in response:
c = response.split("||")[1] # counter
b = response.split("||")[2] # board raw data
bc = b.split(",") # board counters
i = 0
for nc in bc: # for counter in board counters
board[i] = nc # place them on the board
i += 1
clear()
print_board()
print("Game Over!")
print(c, "wins!")
input("Press enter to quit.")
quit()
s.send("blah".encode())
s.close()
input("Lost connection to server. Press enter to quit.")
quit()
if "idlelib.run" in sys.modules:
idle = True
else:
idle = False
err_failure = 1
err_success = 0
empty = " "
board = [empty, empty, empty,
empty, empty, empty,
empty, empty, empty]
options = ["Find a match",
"Play offline",
"Quit"]
# player_counter = "O"
menu()