<s> '''<STR_LIT>''' <EOL> import random <EOL> board_repr = '''<STR_LIT>''' <EOL> def draw_board ( board ) : <EOL> '''<STR_LIT>''' <EOL> print ( board_repr . format ( '<STR_LIT>' , board [ <NUM_LIT:1> ] , board [ <NUM_LIT:2> ] , board [ <NUM_LIT:3> ] , board [ <NUM_LIT:4> ] , <EOL> board [ <NUM_LIT:5> ] , board [ <NUM_LIT:6> ] , board [ <NUM_LIT:7> ] , board [ <NUM_LIT:8> ] , board [ <NUM_LIT:9> ] ) ) <EOL> def input_player_letter ( ) : <EOL> '''<STR_LIT>''' <EOL> letter = '<STR_LIT>' <EOL> while letter not in ( '<STR_LIT:X>' , '<STR_LIT:O>' ) : <EOL> letter = input ( '<STR_LIT>' ) . upper ( ) <EOL> if letter == '<STR_LIT:X>' : <EOL> return [ '<STR_LIT:X>' , '<STR_LIT:O>' ] <EOL> else : <EOL> return [ '<STR_LIT:O>' , '<STR_LIT:X>' ] <EOL> def who_goes_first ( ) : <EOL> '''<STR_LIT>''' <EOL> if random . randint ( <NUM_LIT:0> , <NUM_LIT:1> ) == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def play_again ( ) : <EOL> return input ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . lower ( ) . startswith ( '<STR_LIT:y>' ) <EOL> def make_move ( board , letter , move ) : <EOL> board [ move ] = letter <EOL> def is_winner ( bo , le ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> ( bo [ <NUM_LIT:7> ] == le and bo [ <NUM_LIT:8> ] == le and bo [ <NUM_LIT:9> ] == le ) or <EOL> ( bo [ <NUM_LIT:4> ] == le and bo [ <NUM_LIT:5> ] == le and bo [ <NUM_LIT:6> ] == le ) or <EOL> ( bo [ <NUM_LIT:1> ] == le and bo [ <NUM_LIT:2> ] == le and bo [ <NUM_LIT:3> ] == le ) or <EOL> ( bo [ <NUM_LIT:7> ] == le and bo [ <NUM_LIT:4> ] == le and bo [ <NUM_LIT:1> ] == le ) or <EOL> ( bo [ <NUM_LIT:8> ] == le and bo [ <NUM_LIT:5> ] == le and bo [ <NUM_LIT:2> ] == le ) or <EOL> ( bo [ <NUM_LIT:9> ] == le and bo [ <NUM_LIT:6> ] == le and bo [ <NUM_LIT:3> ] == le ) or <EOL> ( bo [ <NUM_LIT:7> ] == le and bo [ <NUM_LIT:5> ] == le and bo [ <NUM_LIT:3> ] == le ) or <EOL> ( bo [ <NUM_LIT:9> ] == le and bo [ <NUM_LIT:5> ] == le and bo [ <NUM_LIT:1> ] == le ) ) <EOL> def get_board_copy ( board ) : <EOL> dupeBoard = [ ] <EOL> for i in board : <EOL> dupeBoard . append ( i ) <EOL> return dupeBoard <EOL> def is_space_free ( board , move ) : <EOL> return board [ move ] == '<STR_LIT:U+0020>' <EOL> def get_player_move ( board ) : <EOL> move = '<STR_LIT:U+0020>' <EOL> while move not in '<STR_LIT>' . split ( ) or not is_space_free ( board , int ( move ) ) : <EOL> print ( '<STR_LIT>' ) <EOL> move = input ( ) <EOL> return int ( move ) <EOL> def choose_random_move ( board , movesList ) : <EOL> possibleMoves = [ ] <EOL> for i in movesList : <EOL> if is_space_free ( board , i ) : <EOL> possibleMoves . append ( i ) <EOL> if len ( possibleMoves ) != <NUM_LIT:0> : <EOL> return random . choice ( possibleMoves ) <EOL> else : <EOL> return None <EOL> def get_computer_move ( board , computerLetter ) : <EOL> if computerLetter == '<STR_LIT:X>' : <EOL> playerLetter = '<STR_LIT:O>' <EOL> else : <EOL> playerLetter = '<STR_LIT:X>' <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> copy = get_board_copy ( board ) <EOL> if is_space_free ( copy , i ) : <EOL> make_move ( copy , computerLetter , i ) <EOL> if is_winner ( copy , computerLetter ) : <EOL> return i <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> copy = get_board_copy ( board ) <EOL> if is_space_free ( copy , i ) : <EOL> make_move ( copy , playerLetter , i ) <EOL> if is_winner ( copy , playerLetter ) : <EOL> return i <EOL> move = choose_random_move ( board , [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:9> ] ) <EOL> if move != None : <EOL> return move <EOL> if is_space_free ( board , <NUM_LIT:5> ) : <EOL> return <NUM_LIT:5> <EOL> return choose_random_move ( board , [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] ) <EOL> def is_board_full ( board ) : <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> if is_space_free ( board , i ) : <EOL> return False <EOL> return True <EOL> print ( '<STR_LIT>' ) <EOL> while True : <EOL> theBoard = [ '<STR_LIT:U+0020>' ] * <NUM_LIT:10> <EOL> playerLetter , computerLetter = input_player_letter ( ) <EOL> turn = who_goes_first ( ) <EOL> print ( '<STR_LIT>' + turn + '<STR_LIT>' ) <EOL> gameIsPlaying = True <EOL> while gameIsPlaying : <EOL> if turn == '<STR_LIT>' : <EOL> draw_board ( theBoard ) <EOL> move = get_player_move ( theBoard ) <EOL> make_move ( theBoard , playerLetter , move ) <EOL> if is_winner ( theBoard , playerLetter ) : <EOL> draw_board ( theBoard ) <EOL> print ( '<STR_LIT>' ) <EOL> gameIsPlaying = False <EOL> else : <EOL> if is_board_full ( theBoard ) : <EOL> draw_board ( theBoard ) <EOL> print ( '<STR_LIT>' ) <EOL> break <EOL> else : <EOL> turn = '<STR_LIT>' <EOL> else : <EOL> move = get_computer_move ( theBoard , computerLetter ) <EOL> make_move ( theBoard , computerLetter , move ) <EOL> if is_winner ( theBoard , computerLetter ) : <EOL> draw_board ( theBoard ) <EOL> print ( '<STR_LIT>' ) <EOL> gameIsPlaying = False <EOL> else : <EOL> if is_board_full ( theBoard ) : <EOL> draw_board ( theBoard ) <EOL> print ( '<STR_LIT>' ) <EOL> break <EOL> else : <EOL> turn = '<STR_LIT>' <EOL> if not play_again ( ) : <EOL> break </s>
