@@ -41,6 +41,34 @@ def get_version(self):
4141 self .server_conn .sendall (TicTacToe .GET_VERSION_PKT )
4242 return self .read_response ()
4343
44+ #caller guarantees turn is a single char, X or O
45+ def place (self , turn ):
46+ user_coords = input ("Please enter the coordinates you want to mark, eg: 0 0" ).split ()
47+ while len (user_coords ) != 2 :
48+ input = ("You made me almost seg fault! Give me two coordinates!" )
49+
50+ packet = turn + user_coords [0 ] + user_coords [1 ]
51+ self .server_conn .send (packet ) #send which player, and coordinates
52+
53+ #expects a response that is a 9 byte string representing the board
54+ def read_board (self ):
55+ response = self .read_response (self )
56+
57+ #print the board
58+ print (' 0 1 2' )
59+ print ('0 %s | %s | %s' , response [0 ], response [1 ], response [2 ])
60+ print (' ----------------' )
61+ print ('1 %s | %s | %s' , response [4 ], response [5 ], response [6 ])
62+ print (' ----------------' )
63+ print ('2 %s | %s | %s' , response [7 ], response [8 ], response [9 ])
64+
65+ #expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
66+ def get_winner (self ):
67+ response = self .read_response (self )
68+ return int .from_bytes (response ) == 0
69+
70+
71+
4472def play (connect_tuple ):
4573 game = TicTacToe (connect_tuple )
4674 print ("connecting" )
@@ -49,7 +77,6 @@ def play(connect_tuple):
4977 print (game .get_version ())
5078
5179
52-
5380def main ():
5481 if len (sys .argv ) != 3 :
5582 print ("Need <tictactoe host ip> <tictactoe host port>" )
0 commit comments