I have been using Haskell for over ten years now, and I'm starting to
get the hang of it. I was a math and English major and graduated from
Caltech in 1975. I've been programming since then in Fortran, Forth,
Perl, and most recently, Haskell.
I went ahead and started your project, just to see how complicated it
was. Here is the code so far:
module Board where
data Board = B {
nCols :: Int
, nRows :: Int
, b :: [[Int]] } deriving Show
mkBoard :: Int -> Int -> Board
mkBoard m n = B m n (replicate m (replicate n 0))
type Player = Int
mkPlayer, mkOpponennt :: Player
mkPlayer = 1
mkOpponennt = 2
col :: Int -> Board -> [Int]
col i bd = (b bd) !! (i-1)
replaceAt :: Int -> a -> [a] -> [a]
replaceAt i new lofl =
let
(before,(_:after)) = splitAt (i-1) lofl
in before ++ (new:after)
dropInSlot :: Board -> Int -> Player -> Board
dropInSlot bd i p =
let
c = col i bd
(before,after) = span (== 0) c
newCol = init before ++ (p:after)
newB = replaceAt i newCol (b bd)
in bd {b = newB}
playerToChar :: Player -> Char
playerToChar 0 = '.'
playerToChar 1 = 'X'
playerToChar 2 = 'O'
transpose :: [[a]] -> [[a]]
transpose [] = []
transpose ([] : xss) = transpose xss
transpose ((x:xs) : xss) = (x : [h | (h:_) <- xss]) : transpose (xs : [ t | (_:t) <- xss])
boardToStr playerToChar bd =
let
t = transpose (b bd)
withSpaces p = [' ', playerToChar p]
in unlines . map (concatMap withSpaces) $ t
play with it if you like