We can find the exact answer by recursion. In all final states of the board, the probability is either $0$ or $1$. In every other state $S$, the probability is the average of the probabilities you get in states obtained by a single move from $S$. There are fewer than $3^9 = 19683$ board states, so a computer has no trouble with this.
Here is some Mathematica code implementing this (the board is a list of length $9$; I represent empty squares by 0
, X's by 1
, and O's by -1
). I represent the final answer by a triple $(\Pr[\text{X wins}], \Pr[\text{O wins}], \Pr[\text{tie}])$.
(* lines[board] adds up the values along each winning line *)
lines[board_] :=
Total /@ {board[[{1, 2, 3}]], board[[{4, 5, 6}]], board[[{7, 8, 9}]],
board[[{1, 4, 7}]], board[[{2, 5, 8}]], board[[{3, 6, 9}]],
board[[{1, 5, 9}]], board[[{3, 5, 7}]]};
pwin[board_] := pwin[board] =
Which[Max[lines[board]] == 3, {1,0,0}, (* player 1 has won *)
Min[lines[board]] == -3, {0,1,0}, (* player -1 has won *)
FreeQ[board, 0], {0,0,1}, (* no more moves left: draw *)
Total[board] == 0, (* player 1's turn *)
Mean[pwin /@ ReplaceList[board, {x___, 0, y___} :> {x, 1, y}]],
True, (* player -1's turn *)
Mean[pwin /@ ReplaceList[board, {x___, 0, y___} :> {x, -1, y}]]]
pwin[{0,0,0,0,0,0,0,0,0}] (* outputs {737/1260, 121/420, 8/63} *)
It says that from an empty board, the probability of the first player winning is $\frac{737}{1260} \approx 0.5849$. Similar code says that the probability of a tie is $\frac{8}{63} \approx 0.127$ and the probability of the second player winning is $\frac{121}{420} \approx 0.2881$.
Another interesting result we can get in the same way: what if one player plays randomly, but the other player plays to maximize their chances of winning? (And when choosing between a draw and a loss, to avoid losing.) To find this, just replace one of the Mean
s in the code above by a more intelligent choice of move.
- If the first player plays to win, they win with probability $\frac{191}{192}$ and tie with probability $\frac1{192}$.
- If the second player plays to win, they win with probability $\frac{887}{945}$, tie with probability $\frac{43}{945}$, and lose with probability $\frac1{63}$. (Why do they lose with any probability, when there's a strategy that guarantees a tie? Because if you're playing against a random opponent, sometimes taking a risk of losing gives you a higher chance of winning.)