vindinum - board entities as enum

This commit is contained in:
weiss
2020-04-21 22:05:41 +02:00
parent 49481147ff
commit fbe7b56be0
18 changed files with 403 additions and 43 deletions

View File

@@ -1,26 +1,25 @@
{-# LANGUAGE ScopedTypeVariables, LambdaCase, MultiWayIf #-}
module Player
( runMain
, Board
) where
import System.IO
import Control.Monad
import System.Random
import Data.Char (digitToInt)
import Data.List (minimumBy)
import Data.List as L
import BotRunner
import Graph
import Simulation.Data
import Simulation.Board (simulate)
data BoardEntity = BSpawnPoint Int | BWall | BTavern | BMine | BEmpty deriving (Show, Eq)
type Board = [[BoardEntity]]
type IndexedBoard = [(Pos, BoardEntity)]
type Pos = (Int, Int)
-- Id, Pos, life, gold
data Entity
= Hero Int Pos Int Int
| Mine Int Pos
= EHero Int Pos Int Int
| EMine Int Pos
runMain :: IO ()
runMain = runBot True bot
@@ -34,14 +33,14 @@ bot readLine writeLine = do
let size = read input_line :: Int
board' <- replicateM size getLine
let board :: Board = map (\br -> map (\se -> if
| se == '.' -> BEmpty
| se == '#' -> BWall
| se == 'T' -> BTavern
| se == 'M' -> BMine
| otherwise -> BSpawnPoint $ digitToInt se) br) board'
let board :: Board = fmap (\br -> fmap (\se -> if
| se == '.' -> Air
| se == '#' -> Wall
| se == 'T' -> Tavern
| se == 'M' -> Mine
| otherwise -> SpawnPoint) br) board' -- TODO: $ digitToInt se) br) board'
input_line <- getLine
let iBoard :: IndexedBoard = concatMap (\(i_r, br) -> map (\(i_c, bc) -> ((i_c, i_r), bc)) br) $ zip [0..9] $ map (zip [0..9]) board
let iBoard :: IndexedBoard = Prelude.concatMap (\(i_r, br) -> fmap (\(i_c, bc) -> ((i_c, i_r), bc)) br) $ zip [0..9] $ map (zip [0..9]) board
let myId = read input_line :: Int -- ID of your hero
@@ -60,41 +59,37 @@ bot readLine writeLine = do
let life = read (input!!4) :: Int -- the life of a hero (-1 for mines)
let gold = read (input!!5) :: Int -- the gold of a hero (-1 for mines)
pure $ if entitytype == "HERO"
then Hero id (x,y) life gold
else Mine id (x,y)
then EHero id (x,y) life gold
else EMine id (x,y)
let heroes = filter (\e -> case e of
Hero _ _ _ _ -> True
EHero _ _ _ _ -> True
_ -> False) entities
let hero = head $ filter (\e -> case e of
Hero id _ _ _ -> id == myId
EHero id _ _ _ -> id == myId
_ -> False) heroes
let mines = filter (\e -> case e of
Mine oId _ -> oId /= myId
EMine oId _ -> oId /= myId
_ -> False) entities
let minMine = minimumBy (\e1 e2 -> compare (dist (posFromEntity e1) (posFromEntity hero)) (dist (posFromEntity e2) (posFromEntity hero))) mines
let minTavernPos = minimumBy (\p1 p2 -> compare (dist p1 (posFromEntity hero)) (dist p2 (posFromEntity hero))) $ map (\(p, be) -> p) $ filter (\(p, be) -> isTavern be) iBoard
-- hPrint stderr minMine
-- WAIT | NORTH | EAST | SOUTH | WEST
r <- randomRIO (0,3) :: IO Int
let dir = if r == 0
then "NORTH"
else if r == 1
then "EAST"
else if r == 2
then "SOUTH"
else
"WEST"
let minEMine = L.minimumBy (\e1 e2 -> compare (dist (posFromEntity e1) (posFromEntity hero)) (dist (posFromEntity e2) (posFromEntity hero))) mines
let minTavernPos = L.minimumBy (\p1 p2 -> compare (dist p1 (posFromEntity hero)) (dist p2 (posFromEntity hero))) $ map (\(p, be) -> p) $ filter (\(p, be) -> isTavern be) iBoard
putStrLn $ case life hero of
Just lp -> if lp < 30 then moveToPos minTavernPos else moveToEntity minMine
Nothing -> moveToEntity minMine
let myMines = filter (\e -> case e of
EMine oId _ -> oId == myId
_ -> False) entities
let (val, pos) = simulate board (posFromEntity hero) (gameState hero $ length myMines)
hPrint stderr val
putStrLn $ moveToPos pos
-- putStrLn $ case life hero of
-- Just lp -> if lp < 30 then moveToPos minTavernPos else moveToEntity minEMine
-- Nothing -> moveToEntity minEMine
moveToEntity :: Entity -> String
moveToEntity e = case e of
Hero _ p _ _ -> cout p
Mine _ p -> cout p
EHero _ p _ _ -> cout p
EMine _ p -> cout p
where cout (x,y) = "MOVE " <> (show x) <> " " <> (show y)
moveToPos :: (Int, Int) -> String
@@ -104,15 +99,19 @@ dist :: Pos -> Pos -> Int
dist (x1, y1) (x2, y2) = abs (x2 - x1) + abs (y2 - y1)
life :: Entity -> Maybe Int
life (Hero _ _ l _) = Just l
life (EHero _ _ l _) = Just l
life _ = Nothing
posFromEntity :: Entity -> (Int, Int)
posFromEntity (Hero _ p _ _) = p
posFromEntity (Mine _ p) = p
posFromEntity (EHero _ p _ _) = p
posFromEntity (EMine _ p) = p
gameState :: Entity -> Int -> (Int, Int, Int)
gameState (EHero _ _ l g) mines = (g, l, mines)
gameState (EMine _ _) mines = (-1, -1, mines)
isTavern :: BoardEntity -> Bool
isTavern BTavern = True
isTavern Tavern = True
isTavern _ = False
addEdge' :: Ord v => Graph v -> [v] -> Graph v

View File

@@ -0,0 +1,88 @@
module Simulation.Board
( simulate
) where
-- import Prelude
import qualified Data.Vector as V
import Control.Monad.State as S
import Control.Monad.State.Class
import Data.List as L
import Simulation.Data
spawnPoint = fromEnum SpawnPoint
wall = fromEnum Wall
tavern = fromEnum Tavern
mine = fromEnum Mine
air = fromEnum Air
size = 10 -- TODO: Allow for variable board sizes
searchDepth = 6
fromPlayerBoard :: Board -> BoardInternal
fromPlayerBoard pBoardInternal = fmap (fmap $ fromEnum) asVector
where asVector = V.fromList $ fmap V.fromList pBoardInternal
emptyBoard :: BoardInternal
emptyBoard = V.generate 9 (\_ -> V.replicate 9 air)
-- All valid board positions are possible. For example the player could move
-- back and forth between two fields infinitely
-- Caution: if the player moved inside a Tavern or Mine he needs to be reset to his initial position afterwards
-- TODO: Check if tailrec
simulate :: Board -> Pos -> GameState -> (Int, Pos)
simulate board pos = evalState sim
where sim = simulateMove (fromPlayerBoard board) pos searchDepth (-1,-1)
simulateMove :: BoardInternal -> Pos -> Int -> Pos -> State GameState (Int, Pos)
simulateMove board pos depth prevPos
| depth == 0 = do
evalMove board pos
gold <- evalGameState
pure $ (gold, pos)
| otherwise = do
evalMove board pos
let bPos = boardPos board pos
let pos' = if bPos == tavern || bPos == mine then prevPos else pos -- move back out of tavern/mine
vals <- S.mapM (\pos'' -> simulateMove board pos'' (depth-1) pos') moves
-- let valsWithPos = zip (fmap fst vals) moves -- return poss of current move, not of submoves
-- pure $ L.maximumBy (\(v1, _) (v2, _) -> compare v1 v2) valsWithPos
pure $ L.maximumBy (\(v1, _) (v2, _) -> compare v1 v2) vals
where
moves :: [Pos]
moves = filter (posValid board) $ possibleMoves pos
-- update State according to hero position on board
-- executed every move
evalMove :: BoardInternal -> Pos -> State GameState ()
evalMove board pos
| entity == Air = modify (\(gold, life, mines) -> (gold+mines, life-1, mines))
| entity == SpawnPoint = modify (\(gold, life, mines) -> (gold+mines, life-1, mines))
| entity == Tavern = modify ( \(gold, life, mines) -> (gold+mines-2, min 100 (life+50), mines) ) -- TODO: Check if life is +19
| entity == Mine = modify (\(gold, life, mines) -> (gold+mines, life-1, mines))
| entity == Wall = pure () -- should never happen
where
entity = toEnum $ boardPos board pos
-- retuns the evalutaion of the current move
-- executed if maximum depth is reached
evalGameState :: State GameState Int
evalGameState = do
(gold, _, _) <- get
pure gold
-- get BoardInternalEntity Enum of Pos on BoardInternal
boardPos :: BoardInternal -> Pos -> BoardEntityEnum
boardPos board (x,y) = fromEnum $ (board V.! x) V.! y
posValid :: BoardInternal -> Pos -> Bool
posValid board pos@(x,y) = onBoardInternal && boardPos' /= wall
where
boardPos' = boardPos board pos
onBoardInternal = x >= 0 && x < size && y >= 0 && y < size
possibleMoves :: Pos -> [Pos]
possibleMoves (x,y) = [ (x+1, y), (x, y+1), (x-1, y), (x, y-1) ]
data Tree v = Node v (Tree v) | Leaf v

View File

@@ -0,0 +1,15 @@
module Simulation.Data where
import qualified Data.Vector as V
data BoardEntity = SpawnPoint | Wall | Tavern | Mine | Air deriving (Show, Eq, Enum)
type BoardEntityEnum = Int
type Board = [[BoardEntity]]
type IndexedBoard = [(Pos, BoardEntity)]
type BoardInternal = V.Vector (V.Vector BoardEntityEnum)
type Pos = (Int, Int)
-- (gold, life, numMines)
type GameState = (Int, Int, Int)