물리학 자, 하스켈
물리학 자 팩맨 (Pac-Man)은 뉴턴의 보편적 인 중력 법칙 이 게임에서 이길 수 있다고 생각합니다 . 그런 다음 게임 중에 아는 다른 모든 물체에 적용합니다. 물리학자는 나이가 많고 기억력이 좋지 않기 때문에 5 라운드 만 기억할 수 있습니다. Hooooly, 나쁜 기억은 실제로 그가 더 나은 점수를 얻는 데 도움이됩니다.
이 답변에는 두 가지 내용이 있습니다.
Main.hs
흥미로운 부분이 포함되어 있습니다.
Pacman.hs
, 단지 지루한 코드가 프로토콜을 처리합니다. 자신 만의 haskell 솔루션을 작성하는 데 사용할 수 있습니다. AI 코드가 포함되어 있지 않습니다.
아, 잠깐만 요 Makefile
.
여기에 그들이 온다 :
Main.hs
import Pacman
import Data.Complex
import Data.List
import Data.Function
import qualified Data.Map as Map
import Data.Maybe
import System.IO
data DebugInfo = DebugInfo {
debugGrid :: Grid
, debugForce :: Force
, debugAction :: Action
} deriving (Show)
data Physicist = Physicist [(Int, Object)] (Maybe DebugInfo)
type Force = Complex Double
calcForce :: Int -> Position -> PlayerType -> Object -> Force
calcForce n p1 t1 object = if d2 == 0 then 0 else base / (fromIntegral d2 ** 1.5 :+ 0)
where
(x1, y1) = p1
(x2, y2) = p2
wrap d = minimumBy (compare `on` abs) [d, n - d]
dx = wrap $ x2 - x1
dy = wrap $ y2 - y1
Object t2 p2 = object
d2 = dx * dx + dy * dy
base = (fromIntegral dx :+ fromIntegral dy) * case t1 of
PacmanPlayer -> case t2 of
Pellet -> 10.0
PowerPellet -> 200.0
Ghost -> -500.0
Pacman -> -20.0
Fruit -> 100.0
Empty -> -2.0
GhostPlayer -> case t2 of
Pellet -> 10.0
PowerPellet -> 200.0
Ghost -> -50.0
Pacman -> 500.0
Fruit -> 100.0
Empty -> -2.0
instance PlayerAI Physicist where
findAction player info = (action, player') where
Player {
playerType = type_
, playerField = field
, playerMemory = Physicist objectsWithAge _
} = player
n = fieldSize field
NormalRound pos _ objects = info
objectsWithAge' = combineObjects objectsWithAge objects
objects' = map snd objectsWithAge'
directionChoices = filter (not . gridHasWall grid) directions4
totalForce = sum $ map (calcForce n pos type_) objects'
grid = fromMaybe (error $ "invalid position " ++ show pos) $ (fieldGetGrid field) pos
action = if magnitude totalForce < 1e-10
then if null directionChoices
then Stay
else Move $ head directionChoices
else Move $ maximumBy (compare `on` (projectForce totalForce)) directionChoices
debugInfo = Just $ DebugInfo grid totalForce action
player' = player {
playerMemory = Physicist objectsWithAge' debugInfo
}
-- roundDebug player _ = do
-- let Physicist objects debugInfo = playerMemory player
-- type_ = playerType player
-- hPrint stderr (objects, debugInfo)
combineObjects :: [(Int, Object)] -> [Object] -> [(Int, Object)]
combineObjects xs ys = Map.elems $ foldr foldFunc initMap ys where
foldFunc object@(Object type_ pos) = Map.insert pos (0, object)
addAge (age, object) = (age + 1, object)
toItem (age, object@(Object _ pos)) = (pos, (age, object))
initMap = Map.fromList . map toItem . filter filterFunc . map addAge $ xs
filterFunc (age, object@(Object type_ _))
| type_ == Empty = True
| age < maxAge = True
| otherwise = False
maxAge = 5
projectForce :: Force -> Direction -> Double
projectForce (fx :+ fy) (Direction dx dy) = fx * fromIntegral dx + fy * fromIntegral dy
main :: IO ()
main = runAI $ Physicist [] Nothing
Pacman.hs
module Pacman (
Field(..)
, Grid(..)
, Direction(..)
, directions4, directions8
, Position
, newPosition
, Player(..)
, PlayerType(..)
, ObjectType(..)
, Object(..)
, RoundInfo(..)
, Action(..)
, runAI
, PlayerAI(..)
) where
import Data.Bits
import Data.Char
import Data.List
import Data.Maybe
import qualified Data.Map as Map
import qualified System.IO as SysIO
data Field = Field {
fieldGetGrid :: Position -> Maybe Grid
, fieldSize :: Int
}
data Grid = Grid {
gridHasWall :: Direction -> Bool
, gridPos :: Position
}
instance Show Grid where
show g = "Grid " ++ show (gridPos g) ++ ' ':reverse [if gridHasWall g d then '1' else '0' | d <- directions4]
data Direction = Direction Int Int
deriving (Show, Eq)
directions4, directions8 :: [Direction]
directions4 = map (uncurry Direction) [(-1, 0), (0, 1), (1, 0), (0, -1)]
directions8 = map (uncurry Direction) $ filter (/=(0, 0)) [(dx, dy) | dx <- [-1..1], dy <- [-1..1]]
type Position = (Int, Int)
newPosition :: (Int, Int) -> Position
newPosition = id
data Player a = Player {
playerType :: PlayerType
, playerField :: Field
, playerRounds :: Int
, playerMemory :: a
}
data PlayerType = PacmanPlayer | GhostPlayer
deriving (Show, Eq)
class PlayerAI a where
onGameStart :: a -> Field -> IO ()
onGameStart _ _ = return ()
onGameEnd :: a -> IO ()
onGameEnd _ = return ()
findAction :: Player a -> RoundInfo -> (Action, Player a)
roundDebug :: Player a -> RoundInfo -> IO ()
roundDebug _ _ = return ()
data ObjectType = Pacman | Ghost | Fruit | Pellet | PowerPellet | Empty
deriving (Eq, Show)
data Object = Object ObjectType Position
deriving (Show)
data RoundInfo = EndRound | NormalRound Position PlayerType [Object]
data Action = Stay | Move Direction
deriving (Show)
parseField :: String -> Field
parseField s = if validateField field
then field
else error $ "Invalid field: " ++ show ("n", n, "s", s, "fieldMap", fieldMap)
where
field = Field {
fieldGetGrid = flip Map.lookup fieldMap
, fieldSize = n
}
(n : _) = [x | x <- [0..], x * x == length s]
fieldMap = Map.fromList [
((i, j), parseGrid c (newPosition (i, j)))
| (i, row) <- zip [0..n-1] rows,
(j, c) <- zip [0..n-1] row
]
rows = reverse . snd $ foldr rowFoldHelper (s, []) [1..n]
rowFoldHelper _ (s, rows) =
let (row, s') = splitAt n s
in (s', row:rows)
validateField :: Field -> Bool
validateField field@(Field { fieldGetGrid=getGrid, fieldSize=n }) =
all (validateGrid field) $ map (fromJust.getGrid) [(i, j) | i <- [0..n-1], j <- [0..n-1]]
validateGrid :: Field -> Grid -> Bool
validateGrid
field@(Field { fieldGetGrid=getGrid, fieldSize=n })
grid@(Grid { gridPos=pos })
= all (==True) [gridHasWall grid d == gridHasWall (getNeighbour d) (reverse d) | d <- directions4]
where
reverse (Direction dx dy) = Direction (-dx) (-dy)
(x, y) = pos
getNeighbour (Direction dx dy) = fromJust . getGrid . newPosition $ (mod (x + dx) n, mod (y + dy) n)
parseGrid :: Char -> Position -> Grid
parseGrid c pos = Grid gridHasWall pos
where
walls = zip directions4 bits
bits = [((x `shiftR` i) .&. 1) == 1 | i <- [0..3]]
Just x = elemIndex (toLower c) "0123456789abcdef"
gridHasWall d = fromMaybe (error $ "No such direction " ++ show d) $
lookup d walls
parseRoundInfo :: String -> RoundInfo
parseRoundInfo s = if s == "Q" then EndRound else NormalRound pos playerType objects'
where
allObjects = map parseObject $ words s
Object type1 pos : objects = allObjects
objects' = if type1 `elem` [Empty, Ghost] then objects else allObjects
playerType = case type1 of
Ghost -> GhostPlayer
_ -> PacmanPlayer
parseObject :: String -> Object
parseObject s = Object type_ (newPosition (x, y)) where
(y, x) = read $ "(" ++ init s ++ ")"
type_ = case last s of
'P' -> Pacman
'G' -> Ghost
'o' -> Pellet
'O' -> PowerPellet
'F' -> Fruit
'X' -> Empty
c -> error $ "Unknown object type: " ++ [c]
sendAction :: Action -> IO ()
sendAction a = putStrLn name >> SysIO.hFlush SysIO.stdout where
name = (:[]) $ case a of
Stay -> 'X'
Move d -> fromMaybe (error $ "No such direction " ++ show d) $
lookup d $ zip directions4 "NESW"
runPlayer :: PlayerAI a => Player a -> IO ()
runPlayer player = do
roundInfo <- return . parseRoundInfo =<< getLine
case roundInfo of
EndRound -> return ()
info@(NormalRound _ type_' _) -> do
let
updateType :: Player a -> Player a
updateType player = player { playerType = type_' }
player' = updateType player
(action, player'') = findAction player' info
roundDebug player'' info
sendAction action
let
updateRounds :: Player a -> Player a
updateRounds player = player { playerRounds = playerRounds player + 1}
player''' = updateRounds player''
runPlayer player'''
runAI :: PlayerAI a => a -> IO ()
runAI mem = do
field <- return . parseField =<< getLine
let player = Player {
playerType = PacmanPlayer
, playerField = field
, playerRounds = 0
, playerMemory = mem
}
runPlayer player
메이크 파일
physicist: Main.hs Pacman.hs
ghc -O3 -Wall Main.hs -o physicist
command.txt
./physicist