-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathttt_client.py
335 lines (296 loc) · 10.6 KB
/
ttt_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#! /usr/bin/python3
# Import the socket module
import socket
# Import command line arguments
from sys import argv
class TTTClient:
"""TTTClient deals with networking and communication with the TTTServer."""
def __init__(self):
"""Initializes the client and create a client socket."""
# Create a TCP/IP socket
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
def connect(self, address, port_number):
"""Keeps repeating connecting to the server and returns True if
connected successfully."""
while True:
try:
print("Connecting to the game server...");
# Connection time out 10 seconds
self.client_socket.settimeout(10);
# Connect to the specified host and port
self.client_socket.connect((address, int(port_number)));
# Return True if connected successfully
return True;
except:
# Caught an error
print("There is an error when trying to connect to " +
str(address) + "::" + str(port_number));
self.__connect_failed__();
return False;
def __connect_failed__(self):
"""(Private) This function will be called when the attempt to connect
failed. This function might be overridden by the GUI program."""
# Ask the user what to do with the error
choice = input("[A]bort, [C]hange address and port, or [R]etry?");
if(choice.lower() == "a"):
exit();
elif(choice.lower() == "c"):
address = input("Please enter the address:");
port_number = input("Please enter the port:");
def s_send(self, command_type, msg):
"""Sends a message to the server with an agreed command type token
to ensure the message is delivered safely."""
# A 1 byte command_type character is put at the front of the message
# as a communication convention
try:
self.client_socket.send((command_type + msg).encode());
except:
# If any error occurred, the connection might be lost
self.__connection_lost();
def s_recv(self, size, expected_type):
"""Receives a packet with specified size from the server and check
its integrity by comparing its command type token with the expected
one."""
try:
msg = self.client_socket.recv(size).decode();
# If received a quit signal from the server
if(msg[0] == "Q"):
why_quit = "";
try:
# Try receiving the whole reason why quit
why_quit = self.client_socket.recv(1024).decode();
except:
pass;
# Print the resaon
print(msg[1:] + why_quit);
# Throw an error
raise Exception;
# If received an echo signal from the server
elif(msg[0] == "E"):
# Echo the message back to the server
self.s_send("e", msg[1:]);
# Recursively retrive the desired message
return self.s_recv(size, expected_type);
# If the command type token is not the expected type
elif(msg[0] != expected_type):
print("The received command type \"" + msg[0] + "\" does not " +
"match the expected type \"" + expected_type + "\".");
# Connection lost
self.__connection_lost();
# If received an integer from the server
elif(msg[0] == "I"):
# Return the integer
return int(msg[1:]);
# In other case
else:
# Return the message
return msg[1:];
# Simply return the raw message if anything unexpected happended
# because it shouldn't matter any more
return msg;
except:
# If any error occurred, the connection might be lost
self.__connection_lost();
return None;
def __connection_lost(self):
"""(Private) This function will be called when the connection is lost."""
print("Error: connection lost.");
try:
# Try and send a message back to the server to notify connection lost
self.client_socket.send("q".encode());
except:
pass;
# Raise an error to finish
raise Exception;
def close(self):
"""Shut down the socket and close it"""
# Shut down the socket to prevent further sends/receives
self.client_socket.shutdown(socket.SHUT_RDWR);
# Close the socket
self.client_socket.close();
class TTTClientGame(TTTClient):
"""TTTClientGame deals with the game logic on the client side."""
def __init__(self):
"""Initializes the client game object."""
TTTClient.__init__(self);
def start_game(self):
"""Starts the game and gets basic game information from the server."""
# Receive the player's ID from the server
self.player_id = int(self.s_recv(128, "A"));
# Confirm the ID has been received
self.s_send("c","1");
# Tell the user that connection has been established
self.__connected__();
# Receive the assigned role from the server
self.role = str(self.s_recv(2, "R"));
# Confirm the assigned role has been received
self.s_send("c","2");
# Receive the mactched player's ID from the server
self.match_id = int(self.s_recv(128, "I"));
# Confirm the mactched player's ID has been received
self.s_send("c","3");
print(("You are now matched with player " + str(self.match_id)
+ "\nYou are the \"" + self.role + "\""));
# Call the __game_started() function, which might be implemented by
# the GUI program to interact with the user interface.
self.__game_started__();
# Start the main loop
self.__main_loop();
def __connected__(self):
"""(Private) This function is called when the client is successfully
connected to the server. This might be overridden by the GUI program."""
# Welcome the user
print("Welcome to Tic Tac Toe online, player " + str(self.player_id)
+ "\nPlease wait for another player to join the game...");
def __game_started__(self):
"""(Private) This function is called when the game is getting started."""
# This is a virtual function
# The actual implementation is in the subclass (the GUI program)
return;
def __main_loop(self):
"""The main game loop."""
while True:
# Get the board content from the server
board_content = self.s_recv(10, "B");
# Get the command from the server
command = self.s_recv(2, "C");
# Update the board
self.__update_board__(command, board_content);
if(command == "Y"):
# If it's this player's turn to move
self.__player_move__(board_content);
elif(command == "N"):
# If the player needs to just wait
self.__player_wait__();
# Get the move the other player made from the server
move = self.s_recv(2, "I");
self.__opponent_move_made__(move);
elif(command == "D"):
# If the result is a draw
print("It's a draw.");
break;
elif(command == "W"):
# If this player wins
print("You WIN!");
# Draw winning path
self.__draw_winning_path__(self.s_recv(4, "P"));
# Break the loop and finish
break;
elif(command == "L"):
# If this player loses
print("You lose.");
# Draw winning path
self.__draw_winning_path__(self.s_recv(4, "P"));
# Break the loop and finish
break;
else:
# If the server sends back anything unrecognizable
# Simply print it
print("Error: unknown message was sent from the server");
# And finish
break;
def __update_board__(self, command, board_string):
"""(Private) Updates the board. This function might be overridden by
the GUI program."""
if(command == "Y"):
# If it's this player's turn to move, print out the current
# board with " " converted to the corresponding position number
print("Current board:\n" + TTTClientGame.format_board(
TTTClientGame.show_board_pos(board_string)));
else:
# Print out the current board
print("Current board:\n" + TTTClientGame.format_board(
board_string));
def __player_move__(self, board_string):
"""(Private) Lets the user input the move and sends it back to the
server. This function might be overridden by the GUI program."""
while True:
# Prompt the user to enter a position
try:
position = int(input('Please enter the position (1~9):'));
except:
print("Invalid input.");
continue;
# Ensure user-input data is valid
if(position >= 1 and position <= 9):
# If the position is between 1 and 9
if(board_string[position - 1] != " "):
# If the position is already been taken,
# Print out a warning
print("That position has already been taken." +
"Please choose another one.");
else:
# If the user input is valid, break the loop
break;
else:
print("Please enter a value between 1 and 9 that" +
"corresponds to the position on the grid board.");
# Loop until the user enters a valid value
# Send the position back to the server
self.s_send("i", str(position));
def __player_wait__(self):
"""(Private) Lets the user know it's waiting for the other player to
make a move. This function might be overridden by the GUI program."""
print("Waiting for the other player to make a move...");
def __opponent_move_made__(self, move):
"""(Private) Shows the user the move that the other player has taken.
This function might be overridden by the GUI program."""
print("Your opponent took up number " + str(move));
def __draw_winning_path__(self, winning_path):
"""(Private) Shows to the user the path that has caused the game to
win or lose. This function might be overridden by the GUI program."""
# Generate a new human readable path string
readable_path = "";
for c in winning_path:
readable_path += str(int(c) + 1) + ", "
print("The path is: " + readable_path[:-2]);
def show_board_pos(s):
"""(Static) Converts the empty positions " " (a space) in the board
string to its corresponding position index number."""
new_s = list("123456789");
for i in range(0, 8):
if(s[i] != " "):
new_s[i] = s[i];
return "".join(new_s);
def format_board(s):
"""(Static) Formats the grid board."""
# If the length of the string is not 9
if(len(s) != 9):
# Then print out an error message
print("Error: there should be 9 symbols.");
# Throw an error
raise Exception;
# Draw the grid board
#print("|1|2|3|");
#print("|4|5|6|");
#print("|7|8|9|");
return("|" + s[0] + "|" + s[1] + "|" + s[2] + "|\n"
+ "|" + s[3] + "|" + s[4] + "|" + s[5] + "|\n"
+ "|" + s[6] + "|" + s[7] + "|" + s[8] + "|\n");
# Define the main program
def main():
# If there are more than 3 arguments
if(len(argv) >= 3):
# Set the address to argument 1, and port number to argument 2
address = argv[1];
port_number = argv[2];
else:
# Ask the user to input the address and port number
address = input("Please enter the address:");
port_number = input("Please enter the port:");
# Initialize the client object
client = TTTClientGame();
# Connect to the server
client.connect(address, port_number);
try:
# Start the game
client.start_game();
except:
print(("Game finished unexpectedly!"));
finally:
# Close the client
client.close();
if __name__ == "__main__":
# If this script is running as a standalone program,
# start the main program.
main();