-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
115 lines (99 loc) · 3.53 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
from socket import *
import pickle
import sys
MOVE = "move"
GAME_STARTING = "game starting"
WAITING = "waiting for second player"
WIN = "win"
LOSE = "lose"
CONNECTION_LOST_TO_SERVER = "Connection has been lost to the server"
COULD_NOT_CONNECT = "Could not connect to Server!"
CONNECTED_TO_SERVER = "Connected to Server!"
INPUT_INT_ERROR = "Please provide an integer!"
INPUT_RANGE_ERROR = "Please provide a number between 1-9"
class Client:
def __init__(self):
self.sock = socket(AF_INET, SOCK_STREAM)
self.host = gethostbyname(gethostname())
if len(sys.argv) > 1:
self.host = sys.argv[1]
if self.host == "''":
self.host = gethostbyname(gethostname())
self.port = 10001
if len(sys.argv) > 2:
self.port = int(sys.argv[2])
self.server_address = (self.host, self.port)
def set_player_name(self):
self.name = input("Please type in your name: ")
def connect_to_server(self):
try:
self.sock.connect(self.server_address)
except ConnectionRefusedError:
return COULD_NOT_CONNECT
return CONNECTED_TO_SERVER
def wait_for_server(self):
while True:
try:
data = self.sock.recv(1024).decode()
except (ConnectionError, OSError):
return CONNECTION_LOST_TO_SERVER
if data == WAITING:
print(WAITING)
elif data == GAME_STARTING:
break
return GAME_STARTING
def win_message(self):
print("******************")
print("You win %s!!!!!!!!!!!" % (self.name))
print("******************")
def lose_message(self):
print("******************")
print("You lose %s!!!!!!!!!!!" % (self.name))
print("******************")
def print_board(self, new_board):
print(' '.join(str(x) for x in range(1, len(new_board[0])+1)))
print("\n".join(" ".join(row) for row in new_board))
def run(self):
while True:
try:
data = pickle.loads(self.sock.recv(4096))
except ConnectionResetError:
print()
print("Connection has been lost to the server")
break
if MOVE in data:
updated_board = data[MOVE]
self.print_board(updated_board)
# Ask for valid column input
valid_input = False
while not valid_input:
user_input = input('It’s your turn %s, please enter column (1-9):' % (self.name))
try:
col = int(user_input)
except ValueError:
print(INPUT_INT_ERROR)
continue
if col in range(1, 10):
valid_input = True
else:
print(INPUT_RANGE_ERROR)
self.sock.sendall(user_input.encode())
if WIN in data:
self.win_message()
break
if LOSE in data:
self.lose_message()
break
self.sock.close()
if __name__ == "__main__":
c = Client()
c.set_player_name()
server_status = c.connect_to_server()
print(server_status)
if server_status == COULD_NOT_CONNECT:
pass
else:
server_status = c.wait_for_server()
print(server_status)
if server_status != CONNECTION_LOST_TO_SERVER:
c.run()