Skip to content

Instantly share code, notes, and snippets.

@kennycason
Created October 7, 2013 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennycason/6860992 to your computer and use it in GitHub Desktop.
Save kennycason/6860992 to your computer and use it in GitHub Desktop.
Drop piece in Connect 4 column
numEmpty :: [Int] -> Int
numEmpty board = length $ filter (\x -> x == 0) board
addToColumn :: Int -> [Int] -> [Int]
addToColumn val board = xs ++ [val] ++ ys
where
n = (numEmpty board)
xs = replicate (n - 1) 0
ys = snd (splitAt n board)
-- simulate dropping a piece into a connect 4 column
main = do
let board = [0,0,0,0,0,1,-1,1]
print board
print $ addToColumn 1 board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment