From HaskellWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Diary of a Tic-tac-toe program


module TicTacToe

data Board = String

place :: String -> Int -> Char -> String
place [] _ _ = []
place (x:xs) 1 c = c : xs
place (x:xs) n c = x : (place xs (n-1) c)

firstFree :: String -> Int
firstFree (x:xs) | x == ' ' = 1
                 | otherwise = 1 + (firstFree xs)

main = do
  putStrLn "Enter your move:"
  move1Num <- readLn
  let board1 = place "         " move1Num 'X'
  let board2 = place board1 (firstFree board1) 'O'
  putStrLn ("New board:" ++ board2)

  putStrLn "Enter your move:"
  move2Num <- readLn
  let board3 = place board2 move2Num 'X'
  let board4 = place board3 (firstFree board3) 'O'
  putStrLn ("New board:" ++ board4)

  putStrLn "Enter your move:"
  move3Num <- readLn
  let board5 = place board4 move3Num 'X'
  let board6 = place board5 (firstFree board5) 'O'
  putStrLn ("New board:" ++ board6)

  putStrLn "Enter your move:"
  move4Num <- readLn
  let board7 = place board6 move4Num 'X'
  let board8 = place board7 (firstFree board7) 'O'
  putStrLn ("New board:" ++ board8)

  putStrLn "Enter your move:"
  move5Num <- readLn
  let board9 = place board8 move5Num 'X'

  putStrLn ("Final board:" ++ board9)

Hideous, isn't it? Why, thank you! That is, in fact, the point. I want to start with an absolutely hideous piece of code, and add features, re-factoring as I go. It seems to me like there's actually a lot you can do with tic-tac-toe, and so I hope this will turn into something instructive, both for me and for anyone who wants to follow along, and/or contribute.

Feature ideas (in no particular order):

  • output a real board
  • game-end condition check
  • curses-based interface
  • tic-tac-toe variants
  • computer can be x or o
  • two-player mode
  • tournament mode
  • web interface
  • lazy alpha-beta player
  • random square player
  • brute-force player
  • computer vs. computer mode
  • genetically-evolved players

1/29/2007 Well, the TicTacToe project is now two days old, and it's time to check in on its growth. Here's the newest snapshot of its code:

module TicTacToe

import Control.Monad.State

main = runStateT code "         " >> return ()

code :: StateT String IO ()
code = do
  m1 <- lift readLn
  modify (place m1 'X')
  modify (makeMove)

  m2 <- lift readLn
  modify (place m2 'X')
  modify (makeMove)

  m3 <- lift readLn
  modify (place m3 'X')
  modify (makeMove)

  m4 <- lift readLn
  modify (place m4 'X')
  modify (makeMove)

  m5 <- lift readLn
  modify (place m5 'X')

  y <- get
  liftIO $ print y
  return ()

place :: Int -> Char -> String -> String
place _ _ [] = []
place 1 c (x:xs) = c : xs
place n c (x:xs) = x : (place (n-1) c xs)

findFree :: String -> Int
findFree (x:xs) | x == ' ' = 1
                | otherwise = 1 + findFree xs

makeMove :: String -> String
makeMove = flip place 'O' =<< findFree

Well, perhaps 'growth' isn't quite the correct term, since the code has actually shrunk by several lines. Nor has it 'grown' in the features it has, yet. What has happened is that we've used the State monad to get rid of the numerous boards threaded through main. Now there's a single board (which makes good logical sense), which we can access and modify using get, put, and modify. It's still as dumb as a rock, but at least it's a little cleaner!