O and X's or tic tac toe

How could i make ai for a noughts and crosses or tic tac toe game
so it would look like this


||||
|
|||
|||_|

when u click the box it turns to a circle.
then the “computer player” has a turn but how can i make it so the computer player makes his move blocking the “player 1’s” circles?

and could i make it so if 3 are in a line then player 1 wins

thanks in advance,
yhack