The following code should evaluate every possible game in tic tac toe and return the results as a list
data Piece = Naught | Cross deriving (Show, Eq)
data Result = Win | Lose | Draw deriving (Show, Eq)
type Board = Array Integer (Maybe Piece)
emptyBoard :: Board
evaluateBoard :: Board -> (Maybe Result)
allPossibleMoves :: Board -> [Board]
walkPaths :: Bool -> Board -> [Result]
walkPaths inv b =
if result /= Nothing
then [fromJust result]
else concat $ map (walkPaths (not inv)) (allPossibleMoves $ invertBoard b)
where
result = if inv then invertResult evald else evald
evald = evaluateBoard b
allGamesResults = walkPaths False emptyBoard
This is giving me (wins/loss for first to move):
- Total: 255168
- Wins: 77904
- Lose: 131184
- Draw: 46080
Providing these results are correct (I can’t find any data to verify mine with), why doesn’t the first to move have more wins?
UPDATE:
Source code can be found here
Thanks to scree from #haskell on freenode for the answer: