@@ -42,14 +42,14 @@ def __init__(self, connect_tuple, debug = True):
42
42
def read_response (self ):
43
43
resp_len = self .server_conn .recv (4 )
44
44
if len (resp_len ) < 4 :
45
- client_error_wrapper ("unable to read resposne length" )
46
- raise Exception ("need 4 bytes in length resposne , you have " + str (len (resp_len )))
45
+ client_error_wrapper ("unable to read response length" )
46
+ raise Exception ("need 4 bytes in length response , you have " + str (len (resp_len )))
47
47
elif len (resp_len ) > 4 :
48
- client_error_wrapper ("unable to read resposne length" )
49
- raise Exception ("need 4 bytes in length resposne , you have " + str (len (resp_len )))
48
+ client_error_wrapper ("unable to read response length" )
49
+ raise Exception ("need 4 bytes in length response , you have " + str (len (resp_len )))
50
50
resp_len = int .from_bytes (resp_len , NET_ORDER )
51
51
if self .debug :
52
- print ("getting msg resposne len:" , resp_len )
52
+ print ("getting msg response len:" , resp_len )
53
53
54
54
resp_data = self .server_conn .recv (resp_len )
55
55
return resp_data
@@ -68,30 +68,36 @@ def get_version(self):
68
68
69
69
#caller guarantees turn is a single char, X or O
70
70
def place (self , turn ):
71
- user_coords = input ("Please enter the coordinates you want to mark, eg: 0 0" ).split ()
71
+ user_coords = input ("Please enter the coordinates you want to mark in x-y order , eg: 0 0\n > " ).split (' ' )
72
72
while len (user_coords ) != 2 :
73
- input = ( "You made me almost seg fault! Give me two coordinates!" )
73
+ user_coords = input ( "Watch it! you made me almost seg fault! Give me two coordinates!"). split ( ' ' )
74
74
75
75
turn_enum = 1
76
76
if turn == 'O' :
77
77
turn_enum = 2
78
- packet = int .to_bytes (turn_enum , 4 , NET_ORDER ) + user_coords [0 ].encode ("utf-8" ) + user_coords [1 ].encode ("utf-8" )
78
+
79
+ packet = int .to_bytes (turn_enum , 4 , NET_ORDER ) + int .to_bytes (int (user_coords [0 ]), 4 , NET_ORDER ) + int .to_bytes (int (user_coords [1 ]), 4 , NET_ORDER )
79
80
self .server_conn .sendall (int .to_bytes (7 , 4 , NET_ORDER ))
80
81
self .server_conn .sendall (packet ) #send which player, and coordinates
81
82
83
+
82
84
#expects a response that is a 9 byte string representing the board
83
85
def read_board (self ):
84
86
self .server_conn .sendall (int .to_bytes (8 , 4 , NET_ORDER ))
85
87
response = self .read_response ()
86
- print (response )
87
88
88
89
#print the board //TODO convert ascii code from stuff
89
- print (' 0 1 2' )
90
- print (f'0 { response [0 ]} | { response [1 ]} | { response [2 ]} ' )
91
- print (' --------------' )
92
- print (f'1 { response [3 ]} | { response [4 ]} | { response [5 ]} ' )
93
- print (' --------------' )
94
- print (f'2 { response [6 ]} | { response [7 ]} | { response [8 ]} ' )
90
+ print ()
91
+ print ()
92
+ print (' 0 1 2' )
93
+ print ()
94
+ print (f'0 { chr (response [0 ])} | { chr (response [1 ])} | { chr (response [2 ])} ' )
95
+ print (' ---------------' )
96
+ print (f'1 { chr (response [3 ])} | { chr (response [4 ])} | { chr (response [5 ])} ' )
97
+ print (' ---------------' )
98
+ print (f'2 { chr (response [6 ])} | { chr (response [7 ])} | { chr (response [8 ])} ' )
99
+ print ()
100
+ print ()
95
101
96
102
#expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
97
103
def get_winner (self ):
@@ -109,20 +115,20 @@ def play(connect_tuple):
109
115
print ("WELCOME TO ZERO TICTACTOE. 100% VULNERABILITY-FREE CODE GUARANTEED" )
110
116
game .read_board ()
111
117
112
- time .sleep (1 )
113
118
#gameplay
114
119
turn = 'X'
115
120
while game .get_winner () == 0 :
116
- print ('Player %s\' s' )
117
- game .place (game , turn )
121
+ print ('Player %s\' s turn' % turn )
122
+ game .place (turn )
118
123
print ('Good move. I think...' )
119
- time . sleep ( 1 )
124
+ print ( '-' * 20 )
120
125
game .read_board ()
121
- turn = turn == 'X' if turn == 'Y ' else 'Y '
126
+ turn = 'X' if turn == 'O ' else 'O '
122
127
123
128
#game ended
124
129
name = random .choice (names ) # fingers crossed
125
- print ('CONGRATS %s YOU WIN!' % name )
130
+ print ('Congrats %s, YOU WIN!' % name )
131
+ print ('hope I guessed that right :^)' )
126
132
127
133
128
134
def main ():
0 commit comments