This is the core logic of a Tic Tac Toe game. Create a front end, and a human player (if required) and you are good to go.
Dan Pelensky
MIT