You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AI.hs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. module AI (aimove) where
  2. import Eval
  3. import Lambda
  4. import GoalTransform
  5. import qualified Data.Array.IO as A
  6. cannon 1 = "dec"
  7. cannon 2 = "S dec dec"
  8. cannon n = "S(" ++ cannon (n-1) ++ ") dec"
  9. keepalive func 0 = "S(" ++ func ++ ")(get)0"
  10. keepalive func 1 = "S(K(S(" ++ func ++ ")(get)))(succ)0"
  11. keepalive func slot = "S(" ++ func ++ ")(S (K get) (K " ++ show slot ++ "))0"
  12. findAliveSlot :: (Game -> Player) -> Turn Int
  13. findAliveSlot player = do
  14. game <- getGame
  15. slots <- liftIO $ A.getAssocs (fields $ player game)
  16. let (slot, _):_ = filter (\(_, (_, vit)) -> vit > 0) slots
  17. return slot
  18. findHighAliveSlot :: (Game -> Player) -> Turn Int
  19. findHighAliveSlot player = do
  20. game <- getGame
  21. slots <- liftIO $ A.getAssocs (fields $ player game)
  22. let (slot, _):_ = filter (\(_, (_, vit)) -> vit > 0) $ reverse slots
  23. return slot
  24. aimove :: Turn Goal
  25. aimove = do
  26. target <- findAliveSlot opponent
  27. let l = if (target == 255) then "S(dec)(S(get)(I))" else "S(S(K(dec))(K(" ++ show (255-target) ++ ")))(S(get)(I))"
  28. -- let l = if (target == 255) then "S(dec)(S(get)(I))" else "S(K(S(dec)(S(get)(I))))(K(" ++ show (255-target) ++ "))"
  29. let pos = 1 -- if (target == 255) then 1 else 255-target
  30. haveLoop <- satGoal (0, read l)
  31. if haveLoop then do
  32. haveLoop <- satGoal (pos, read l)
  33. if haveLoop then do
  34. return (Just pos, read (l ++ "0"))
  35. else
  36. return (Just pos, read "get 0")
  37. else do
  38. return (Just 0, read l)