Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings #-}
- module Main where
- import Network.HTTP.Conduit
- import Network.HTTP.Types
- import qualified Data.ByteString.Lazy as L
- import Data.Char (chr)
- import qualified Data.ByteString as B
- import qualified Data.ByteString.Lazy.Char8 as C
- import Language.Haskell.TH.Ppr
- data BoardMove = BoardMove {
- bMove ::(Int, Int, Char)
- } deriving Show
- type Board = [BoardMove]
- url :: String
- url = "http://tictactoe.homedir.eu/game/kim2/player/2"
- type Move = (Int, Int, Char)
- type Moves = [Move]
- parse :: String -> Moves
- parse ('M':'a':'p':'(':rest) = reverse $ stringTuples [] rest
- parse _ = error "Not a map"
- readDigit :: String -> (Int, String)
- readDigit ('0':rest) = (0, rest)
- readDigit ('1':rest) = (1, rest)
- readDigit ('2':rest) = (2, rest)
- readDigit _ = error "Digit expected"
- readPlayer :: String -> (Char, String)
- readPlayer ('x': rest) = ('x', rest)
- readPlayer ('o': rest) = ('o', rest)
- readPlayer _ = error "Player expected"
- readSeparator :: String -> String
- readSeparator (',':rest) = readSeparator rest
- readSeparator ('(':rest) = ('(':rest)
- readSeparator ('M':'a':'p':rest) = rest
- readSeparator ('0':rest) = readSeparator rest
- readSeparator ('1':rest) = readSeparator rest
- readSeparator ('2':rest) = readSeparator rest
- readSeparator ('3':rest) = readSeparator rest
- readSeparator ('4':rest) = readSeparator rest
- readSeparator ('5':rest) = readSeparator rest
- readSeparator ('6':rest) = readSeparator rest
- readSeparator ('7':rest) = readSeparator rest
- readSeparator ('8':rest) = readSeparator rest
- readSeparator ('9':rest) = readSeparator rest
- readSeparator('-':'>':rest) = readSeparator rest
- readSeparator(' ':rest) = readSeparator rest
- readSeparator _ = "No separator"
- superParser :: String -> String
- superParser('x':rest) = superParser rest
- superParser('y':rest) = superParser rest
- superParser('v':rest) = superParser rest
- superParser('-':'>':rest) = spaceParser rest
- superParser(' ':rest) = superParser rest
- superParser(',':rest) = superParser rest
- superParser _ = error "superParser error"
- spaceParser :: String -> String
- spaceParser (' ':rest) = spaceParser rest
- spaceParser ('0':rest) = ('0':rest)
- spaceParser ('1':rest) = ('1':rest)
- spaceParser ('2':rest) = ('2':rest)
- spaceParser ('x':rest) = ('x':rest)
- spaceParser ('o':rest) = ('o':rest)
- spaceParser _ = error "spaceParser"
- stringTuples acc ")" = acc
- stringTuples acc rest =
- let
- sepRest = readSeparator rest
- (tuple, restt) = stringTuple sepRest
- in
- stringTuples (tuple:acc) restt
- stringTuple :: String -> ((Int, Int, Char), String)
- stringTuple ('(':rest) =
- let
- resto = superParser rest
- (x, restx) = readDigit resto
- sep1Rest = superParser restx
- (y, resty) = readDigit sep1Rest
- sep2Rest = superParser resty
- (p, restp) = readPlayer sep2Rest
- in
- case restp of
- (')':t) -> ((x, y, p), t)
- _ -> error "Tuple without closing bracket"
- stringTuple _ = error "No tuple"
- validate :: String -> Bool
- validate rest =
- let
- x = parse rest
- in
- if x == [] then True
- else validCheck x
- validCheck :: Moves -> Bool
- validCheck moves =
- let
- x = cmp moves moves
- y = greatMoves moves
- z = cmp2 moves
- in
- if x == True && y == True && z == True then True
- else False
- cmp :: Moves -> Moves -> Bool
- cmp moves moves2 =
- let
- l1 = moves
- l2 = tail moves2
- l2Tail = tail l2
- l1Tup = head l1
- l2Tup = head l2
- l1Tail = tail l1
- in
- if l1Tail == [] then True
- else if l1Tup == l2Tup then False
- else if l2Tail == [] then cmp l1Tail l1Tail
- else cmp l1 l2
- cmp2 :: Moves -> Bool
- cmp2 moves =
- let
- l1 = head moves
- l1t = tail moves
- l2 = head l1t
- l1tup = getCord l1
- l2tup = getCord l2
- in
- if l1t == [] then True
- else if l1tup == l2tup then False
- else cmp2 l1t
- logikavienas :: Moves -> Moves
- logikavienas moves = [(0,0,'x')]
- logikadu :: Moves -> Moves
- logikadu moves =
- let
- ot = head moves
- in
- if ot == (1,1, 'o') then [(2,2, 'x'),ot,(0,0,'x')]
- else if ot /= (0,1, 'o') then [(0,2, 'x'),ot,(0,0, 'x')]
- else if ot == (0,1, 'o') then [(2,0, 'x'),ot,(0,0,'x')]
- else []
- {--logikatres :: Moves -> Moves
- logikatres moves =
- let
- rest = tail moves
- ot = head moves
- rest2 = tail rest
- ot2 = head rest
- in
- if ot == (1,1,'o') && ot2 /= (0,1,'o') then [(0,1,'x'),ot,(0,2,'x'),ot2,(0,0,'x')]
- else if ot == (1,1,'o') && ot2 == (0,1,'o') then [(1,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot /= (0,1,'o') && ot2 /= (1,1,'o') && ot2 /= (0,1,'o') then [(1,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (0,1,'o') && ot2 /= (1,1,'o') && ot2 /= (0,1,'o') then [(1,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (0,2,'o') then [(2,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (2,0,'o') then [(0,2,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (0,1,'o') then [(2,1,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (2,1,'o') then [(0,1,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (1,0,'o') then [(1,2,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (1,2,'o') then [(1,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else []
- --}
- {--
- logikaquatro :: Moves -> Moves
- let
- rest = tail moves
- ot = head moves
- rest = tail rest
- ot2 = head rest
- rest = tail rest
- rest = tail rest
- ot3 = head rest
- in
- if ot3 != (1,1,'o') && ot2 == (0,1,'o') then [(0,1,'x'),ot,(0,2,'x'),ot2,(0,0,'x')]
- else if ot == (1,1,'o') && ot2 == (0,1,'o') then [(1,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (0,2,'o') then [(2,0,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (2,0,'o') then [(0,2,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (a,b,c) && mod a 3 != 1 then [(mod a+2 3,b,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- else if ot == (a,b,c) && mod a 3 == 1 then [(a,mod b+2 3,'x'),ot,(2,0,'x'),ot2,(0,0,'x')]
- --}
- greatMoves :: Moves -> Bool
- greatMoves moves =
- let
- l2 = tail moves
- l1 = moves
- l1T = head moves
- l2T = head l2
- l1H = getPlayer l1T
- l2H = getPlayer l2T
- in
- if l2 == [] then True
- else if l1H == l2H then False
- else greatMoves l2
- getPlayer :: Move -> Char
- getPlayer (_,_,p) = p
- getCord :: Move -> (Int,Int)
- getCord (a,b,_) = (a,b)
- getMove :: Moves -> Move
- getMove a = head a
- win :: Board -> Char -> Bool
- win b c =
- if checkRow 0 b c || checkRow 1 b c || checkRow 2 b c then True
- else if checkCol 0 b c || checkCol 1 b c || checkCol 2 b c then True
- else if length (filter(\(BoardMove(r, col, m)) -> r == 0 && col == 0 && m == c) b) + length (filter(\(BoardMove(r, col, m)) -> r == 1 && col == 1 && m == c) b) + length (filter(\(BoardMove(r, col, m)) -> r == 2 && col == 2 && m == c) b) == 3 then True
- else if length (filter(\(BoardMove(r, col, m)) -> r == 0 && col == 2 && m == c) b) + length (filter(\(BoardMove(r, col, m)) -> r == 1 && col == 1 && m == c) b) + length (filter(\(BoardMove(r, col, m)) -> r == 2 && col == 0 && m == c) b) == 3 then True
- else False
- checkRow :: Int -> Board -> Char -> Bool
- checkRow num b c = length (filter(\(BoardMove(r, _, m)) -> r == num && m == c) b) == 3
- checkCol :: Int -> Board -> Char -> Bool
- checkCol num b c = length (filter(\(BoardMove(_, col, m)) -> col == num && m == c) b) == 3
- addToBoard :: BoardMove -> Board -> Board
- addToBoard m [] = [m]
- addToBoard m b = (m:b)
- buildGetRequest :: IO Request
- buildGetRequest = do
- req0 <- parseUrl "http://tictactoe.homedir.eu/game/pivo74/player/1"
- return(req0 { method = methodGet
- , requestHeaders = [("Accept", "application/scala+map")]
- })
- --buildPostRequest :: String -> IO Request
- buildPostRequest message = do
- req0 <- parseUrl "http://tictactoe.homedir.eu/game/pivo74/player/1"
- return(req0 { method = methodPost
- , requestHeaders = [("Content-Type", "application/scala+map")]
- , requestBody = RequestBodyLBS $ C.pack message} {--$ C.pack msg--}
- )
- sendRequest a = do
- manager <- newManager conduitManagerSettings
- req <- a
- res <- httpLbs req manager
- return (responseBody res)
- readSmth buildReq = do
- a <- sendRequest buildReq
- return a
- requestToString b = do
- c <- sendRequest b
- let d = lazyToString c
- return d
- lazyToString lazy =
- let
- byte = L.toStrict lazy
- c = bsToStr byte
- in
- c
- bsToStr :: B.ByteString -> String
- bsToStr = map (chr . fromEnum) . B.unpack
- parseGetRequest toParse = do
- stringParse <- requestToString toParse
- let parsed = parse stringParse
- return parsed
- row :: Moves -> Int -> Char -> Bool
- row moves x a=(length(filter (\(x1,_,a1)->(x==x1&&a==a1)) moves)==2) && (length(filter (\(x1,_,_)->(x==x1)) moves)==2)
- col :: Moves -> Int -> Char -> Bool
- col moves y a=(length(filter (\(_,y1,a1)->(y==y1&&a==a1)) moves)==2) && (length(filter (\(_,y1,_)->(y==y1)) moves)==2)
- diag1 :: Moves -> Char -> Bool
- diag1 moves a=(length(filter (\(x,y,a1)->(((x==0&&y==0)||(x==1&&y==1)||(x==2&&y==2))&&a==a1)) moves)==2) && (length(filter (\(x,y,_)->(((x==0&&y==0)||(x==1&&y==1)||(x==2&&y==2)))) moves)==2)
- diag2 :: Moves -> Char -> Bool
- diag2 moves a=(length(filter (\(x,y,a1)->(((x==2&&y==0)||(x==1&&y==1)||(x==0&&y==2))&&a==a1)) moves)==2) && (length(filter (\(x,y,_)->(((x==2&&y==0)||(x==1&&y==1)||(x==0&&y==2)))) moves)==2)
- rowput::Moves -> Int -> Moves
- rowput moves x=
- if length(filter (\(x1,y,_)->(x==x1&&y==0)) moves)==0 then (x,0,'x') : moves
- else if length(filter (\(x1,y,_)->(x==x1&&y==1)) moves)==0 then (x,1,'x') : moves
- else (x,2,'x') : moves
- colput::Moves -> Int -> Moves
- colput moves y=
- if length(filter (\(x,y1,_)->(y==y1&&x==0)) moves)==0 then (0,y,'x') : moves
- else if length(filter (\(x,y1,_)->(y==y1&&x==1)) moves)==0 then (1,y,'x') : moves
- else (2,y,'x') : moves
- diag1put::Moves -> Moves
- diag1put moves=
- if length(filter (\(x,y,_)->((x==0&&y==0))) moves)==0 then (0,0,'x') : moves
- else if length(filter (\(x,y,_)->((x==1&&y==1))) moves)==0 then (1,1,'x') : moves
- else (2,2,'x') : moves
- diag2put::Moves -> Moves
- diag2put moves=
- if length(filter (\(x,y,_)->((x==2&&y==0))) moves)==0 then (2,0,'x') : moves
- else if length(filter (\(x,y,_)->((x==1&&y==1))) moves)==0 then (1,1,'x') : moves
- else (0,2,'x') : moves
- put::Moves -> Moves
- put moves=
- if length(filter (\(x,y,_)->(x==1&&y==1)) moves)==0 then (1,1,'x') : moves
- else if length(filter (\(x,y,_)->(x==1&&y==0)) moves)==0 then (1,0,'x') : moves
- else if length(filter (\(x,y,_)->(x==2&&y==0)) moves)==0 then (2,0,'x') : moves
- else if length(filter (\(x,y,_)->(x==0&&y==1)) moves)==0 then (0,1,'x') : moves
- else if length(filter (\(x,y,_)->(x==0&&y==0)) moves)==0 then (0,0,'x') : moves
- else if length(filter (\(x,y,_)->(x==2&&y==1)) moves)==0 then (2,1,'x') : moves
- else if length(filter (\(x,y,_)->(x==0&&y==2)) moves)==0 then (0,2,'x') : moves
- else if length(filter (\(x,y,_)->(x==1&&y==2)) moves)==0 then (1,2,'x') : moves
- else (2,2,'x') : moves
- check2::Moves -> Moves
- check2 moves=
- if row moves 0 'x' then rowput moves 0
- else if row moves 1 'x' then rowput moves 1
- else if row moves 2 'x' then rowput moves 2
- else if col moves 0 'x' then colput moves 0
- else if col moves 1 'x' then colput moves 1
- else if col moves 2 'x' then colput moves 2
- else if diag1 moves 'x' then diag1put moves
- else if diag2 moves 'x' then diag2put moves
- else if row moves 0 'o' then rowput moves 0
- else if row moves 1 'o' then rowput moves 1
- else if row moves 2 'o' then rowput moves 2
- else if col moves 0 'o' then colput moves 0
- else if col moves 1 'o' then colput moves 1
- else if col moves 2 'o' then colput moves 2
- else if diag1 moves 'o' then diag1put moves
- else if diag2 moves 'o' then diag2put moves
- else put moves
- movesToString:: Moves -> String
- movesToString moves =
- let
- string = stringTuples2 moves 0 ""
- in
- string
- stringTuples2:: Moves -> Int -> String -> String
- stringTuples2 moves counter string =
- let
- headTuple = head moves
- movesTail = tail moves
- str = string++ stringTuple2 counter headTuple
- c = counter + 1
- in
- if movesTail /= [] || counter == 9 then stringTuples2 movesTail c str
- else str++ ")"
- stringTuple2:: Int -> Move -> String
- stringTuple2 counter tuple =
- let
- turnNr = turnNumber counter
- in
- if tuple == (0,0,'x') then turnNr++ "x -> 0, y -> 0, v-> x)"
- else if tuple == (0,1,'x') then turnNr++ "x -> 0, y -> 1, v-> x)"
- else if tuple == (0,2,'x') then turnNr++ "x -> 0, y -> 2, v-> x)"
- else if tuple == (1,0,'x') then turnNr++ "x -> 1, y -> 0, v-> x)"
- else if tuple == (1,1,'x') then turnNr++ "x -> 1, y -> 1, v-> x)"
- else if tuple == (1,2,'x') then turnNr++ "x -> 1, y -> 2, v-> x)"
- else if tuple == (2,0,'x') then turnNr++ "x -> 2, y -> 0, v-> x)"
- else if tuple == (2,1,'x') then turnNr++ "x -> 2, y -> 1, v-> x)"
- else if tuple == (2,2,'x') then turnNr++ "x -> 2, y -> 2, v-> x)"
- else if tuple == (0,0,'o') then turnNr++ "x -> 0, y -> 0, v-> o)"
- else if tuple == (0,1,'o') then turnNr++ "x -> 0, y -> 1, v-> o)"
- else if tuple == (0,2,'o') then turnNr++ "x -> 0, y -> 2, v-> o)"
- else if tuple == (1,0,'o') then turnNr++ "x -> 1, y -> 0, v-> o)"
- else if tuple == (1,1,'o') then turnNr++ "x -> 1, y -> 1, v-> o)"
- else if tuple == (1,2,'o') then turnNr++ "x -> 1, y -> 2, v-> o)"
- else if tuple == (2,0,'o') then turnNr++ "x -> 2, y -> 0, v-> o)"
- else if tuple == (2,1,'o') then turnNr++ "x -> 2, y -> 1, v-> o)"
- else if tuple == (2,2,'o') then turnNr++ "x -> 2, y -> 2, v-> o)"
- else ""
- turnNumber:: Int -> String
- turnNumber counter =
- if counter == 0 then "Map(0 -> Map("
- else if counter == 1 then ", 1 -> Map("
- else if counter == 2 then ", 2 -> Map("
- else if counter == 3 then ", 3 -> Map("
- else if counter == 4 then ", 4 -> Map("
- else if counter == 5 then ", 5 -> Map("
- else if counter == 6 then ", 6 -> Map("
- else if counter == 7 then ", 7 -> Map("
- else if counter == 8 then ", 8 -> Map("
- else ""
- postBoard b = sendRequest xe
- where
- --pm=packMess b
- xe = buildPostRequest b
- main :: IO()
- main = do
- putStrLn "Game End"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement