module Main where
import Game
import PlayGame
type Column = [Player]
data ConnectFor = CF [Column] Player deriving (Eq)
instance Show ConnectFor where
show (CF board _) = unlines $ reverse $ showBoard 6 ( board)
where
showBoard 0 b = []
showBoard n b
= showLine b
: showBoard (n-1) (map (\xs -> if null xs then [] else tail xs) b)
showLine b = (foldl (\x y -> x++ "|" ++ y ) "" (theLine b))++"|"
theLine b = (map (\xs -> if null xs then " " else asChar$head xs) b)
asChar One = "1"
asChar Two = "2"
asChar None = " "
instance Game ConnectFor where
start = CF (take 7 (repeat (take 6 $ repeat None))) One
moves game@(CF board _)
|hasWinner game = []
|otherwise = map snd $filter (\(x,y)-> elem None x) $zip board [1..]
makeMove (CF board currentPlayer) move
= (CF newboard $nextPlayer currentPlayer)
where
newboard = addMove move board
addMove 1 ((None:x):xs) = (currentPlayer:x):xs
addMove 1 ((pl:x):xs) = (pl:c):xs
where
(c:_) = addMove 1 (x:xs)
addMove n (x:xs) = x:addMove (n-1) xs
currentPlayer (CF _ currentPl) = currentPl
hasWinner (CF board _)
= or [columnWins c | c<- board]
|| (rowWins board)
|| or [columnWins c | c<- dia1 board]
where
columnWins (x1:xs@(x2:x3:x4:_))
= (x1 /= None && x1==x2 && x1==x3 && x1 == x4) || columnWins xs
columnWins _ = False
rowWins columns = or [columnWins r | r <- rows]
where
rows = getRows 6 columns
getRows 0 cs = []
getRows n cs = (map getHead cs):(getRows (n-1) $map getTail cs)
getHead [] = None
getHead (x:_) = x
getTail [] = []
getTail (x:xs) = xs
getNTail 0 cs = cs
getNTail n cs
|n>0 = getNTail (n-1) $ getTail (cs++[None])
|otherwise = None:getNTail (n+1) (init cs)
dia1 board = getRows 6 $ map (\(cs,n)->getNTail n cs) (zip board [3,2..])
eval _ = 42
bestMove a = head$moves a
vierGewinntAlleSpiele = createGameTree (start::ConnectFor)
main = playGame (start :: ConnectFor)