Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit c5a77ad1 authored by ariqbasyar's avatar ariqbasyar
Browse files

init data and code templates

parent dd4635bf
No related branches found
No related tags found
No related merge requests found
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
module Game where
import Data.Aeson
import GHC.Generics
data Game = Game
{ next :: String
-- board :: [[Char]]
}
deriving (Generic, FromJSON, ToJSON, Show)
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE RecordWildCards #-}
module Lib where module Lib where
import Aws.Lambda import Aws.Lambda
import Data.Aeson import Data.Aeson
import GHC.Generics import qualified Data.ByteString.Lazy.Char8 as ByteString
import Minimax (minimax)
data Person = Person import Request (Request (..))
{ personName :: String, import Response (Response (..))
personAge :: Int
}
deriving (Generic)
instance FromJSON Person
instance ToJSON Person
handler :: Person -> Context () -> IO (Either String Person) handler :: Request -> Context () -> IO (Either String Response)
handler person context = handler Request {..} context = do
if personAge person > 0 case decode (ByteString.pack body) of
then return (Right person) Just game ->
else return (Left "A person's age must be positive") pure $
Right
Response
{ statusCode = 200,
body = minimax game
}
Nothing ->
pure $
Right
Response
{ statusCode = 400,
body = "invalid game"
}
module Minimax where module Minimax where
import Game (Game (..))
nextMove :: String -> String
nextMove str
| str == "X" = "O"
| otherwise = "X"
minimax :: Game -> String
minimax game =
"next " ++ nextMove (next game)
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
module Request where
import Data.Aeson
import GHC.Generics
data Request = Request
{ resource :: String,
body :: String
}
deriving (Generic, FromJSON)
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
module Response where
import Data.Aeson
import GHC.Generics
data Response = Response
{ statusCode :: Int,
body :: String
}
deriving (Generic, ToJSON)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment