Tic-Tac-Toe using Markov Decision Processes
An agent that plays optimal Tic-Tac-Toe by modeling the game as a Markov Decision Process (MDP) and solving for the optimal policy directly, rather than learning one through trial and error.
The MDP formulation
The game is modeled as a tuple , where each board state is a grid of , and each action places the current player's mark on an empty cell.
Since the game is deterministic, the transition function collapses to
and the reward is only ever non-zero at a terminal state:
Solving for the optimal policy
With the full state space small enough to enumerate, the optimal value function is computed exactly via the Bellman optimality equation
solved by backward induction from terminal states, and the optimal policy follows directly:
Because and the game tree is finite, this reduces to ordinary minimax with no discounting — the MDP framing mainly matters once the opponent's policy is treated as stochastic rather than adversarial.
Result
The resulting agent never loses: it wins against any suboptimal opponent and draws against an optimal one, matching the known game-theoretic result for Tic-Tac-Toe.
Source code: github.com/aarondavis-git/TicTacToe-MarkovDecisionProcess