A simple computer player (Tic-Tac-Toe)

Read a board (as in stage 1). It is X’s turn if both have the same number of marks, otherwise O’s. Pick a move with these rules, in order: win now if you can; otherwise block the opponent’s winning cell; otherwise take the centre; then the first free corner (top-left, top-right, bottom-left, bottom-right); then the first free side (top, left, right, bottom). When several cells win (or block), take the first in reading order. Print <player plays <row <col. If the game is already over print its status.

Input: Three lines.

Output: One line.

Difficulty: Medium. Write your answer in the browser editor, run the examples, then submit it against hidden tests. Python and JavaScript are free; other languages are part of licode Club.

Hint

  • Try each empty cell: place the mark, check for a winner, undo.

More Tic-Tac-Toe practice