11import sys
22import socket
3+ import time
4+ import random
5+
6+ names = ['Mav' , 'Adam' , 'Teddy' , 'Ari' , 'Donovan' , 'Diana' ,
7+ 'Eduardo' , 'Emanuel' , 'Nourya' , 'Ron' , 'Tobias' , 'Vrushank' ]
38
49def client_error_wrapper (error_msg ):
510 print ("*" * len (str (error_msg )))
@@ -47,12 +52,15 @@ def place(self, turn):
4752 while len (user_coords ) != 2 :
4853 input = ("You made me almost seg fault! Give me two coordinates!" )
4954
50- packet = turn + user_coords [0 ] + user_coords [1 ]
51- self .server_conn .send (packet ) #send which player, and coordinates
55+ turn_enum = 1
56+ if turn == 'O' :
57+ turn_enum = 2
58+ packet = turn .encode ("utf-8" ) + user_coords [0 ].encode ("utf-8" ) + user_coords [1 ].encode ("utf-8" )
59+ self .server_conn .sendall (packet ) #send which player, and coordinates
5260
5361 #expects a response that is a 9 byte string representing the board
5462 def read_board (self ):
55- response = self .read_response (self )
63+ response = self .read_response ()
5664
5765 #print the board
5866 print (' 0 1 2' )
@@ -64,10 +72,8 @@ def read_board(self):
6472
6573 #expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
6674 def get_winner (self ):
67- response = self .read_response (self )
68- return int .from_bytes (response ) == 0
69-
70-
75+ response = self .read_response ()
76+ return int .from_bytes (response , NET_ORDER )
7177
7278def play (connect_tuple ):
7379 game = TicTacToe (connect_tuple )
@@ -76,6 +82,23 @@ def play(connect_tuple):
7682 print ("connected" )
7783 print (game .get_version ())
7884
85+ print ("WELCOME TO ZERO TICTACTOE. 100% VULNERABILITY-FREE CODE GUARANTEED" )
86+ game .read_board ()
87+
88+ #gameplay
89+ turn = 'X'
90+ while game .get_winner == 0 :
91+ print ('Player %s\' s' )
92+ game .place (game , turn )
93+ print ('Good move. I think...' )
94+ time .sleep (1 )
95+ game .read_board ()
96+ turn = turn == 'X' if turn == 'Y' else 'Y'
97+
98+ #game ended
99+ name = names [random (0 , len (names ))] # fingers crossed
100+ print ('CONGRATS %s YOU WIN!' , name )
101+
79102
80103def main ():
81104 if len (sys .argv ) != 3 :
0 commit comments