@@ -42,14 +42,14 @@ def __init__(self, connect_tuple, debug = True):
4242 def read_response (self ):
4343 resp_len = self .server_conn .recv (4 )
4444 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 )))
4747 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 )))
5050 resp_len = int .from_bytes (resp_len , NET_ORDER )
5151 if self .debug :
52- print ("getting msg resposne len:" , resp_len )
52+ print ("getting msg response len:" , resp_len )
5353
5454 resp_data = self .server_conn .recv (resp_len )
5555 return resp_data
@@ -68,30 +68,36 @@ def get_version(self):
6868
6969 #caller guarantees turn is a single char, X or O
7070 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 (' ' )
7272 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 ( ' ' )
7474
7575 turn_enum = 1
7676 if turn == 'O' :
7777 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 )
7980 self .server_conn .sendall (int .to_bytes (7 , 4 , NET_ORDER ))
8081 self .server_conn .sendall (packet ) #send which player, and coordinates
8182
83+
8284 #expects a response that is a 9 byte string representing the board
8385 def read_board (self ):
8486 self .server_conn .sendall (int .to_bytes (8 , 4 , NET_ORDER ))
8587 response = self .read_response ()
86- print (response )
8788
8889 #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 ()
95101
96102 #expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
97103 def get_winner (self ):
@@ -109,20 +115,20 @@ def play(connect_tuple):
109115 print ("WELCOME TO ZERO TICTACTOE. 100% VULNERABILITY-FREE CODE GUARANTEED" )
110116 game .read_board ()
111117
112- time .sleep (1 )
113118 #gameplay
114119 turn = 'X'
115120 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 )
118123 print ('Good move. I think...' )
119- time . sleep ( 1 )
124+ print ( '-' * 20 )
120125 game .read_board ()
121- turn = turn == 'X' if turn == 'Y ' else 'Y '
126+ turn = 'X' if turn == 'O ' else 'O '
122127
123128 #game ended
124129 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 :^)' )
126132
127133
128134def main ():
0 commit comments