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.

76 lines
1.5KB

  1. module Client where
  2. import Eval
  3. import Lambda
  4. import AI
  5. import GoalTransform
  6. import System.IO
  7. import System.Environment
  8. import Control.Monad
  9. import qualified Data.Array.IO as A
  10. readMove :: Turn Move
  11. readMove = do
  12. lr <- liftIO getLine
  13. sf <- liftIO getLine
  14. st <- liftIO getLine
  15. if (lr == "1") then (do
  16. let f = read sf :: Card
  17. let t = read st :: Int
  18. return $ MoveLeft f t
  19. ) else (do
  20. let f = read sf :: Int
  21. let t = read st :: Card
  22. return $ MoveRight f t
  23. )
  24. stepClient :: Turn ()
  25. stepClient = readMove >>= runMove
  26. stepAI :: Turn Goal -> Turn ()
  27. stepAI ai = do
  28. try (do
  29. goal <- ai
  30. move <- moveGoal goal
  31. liftIO $ printMove move
  32. runMove move
  33. ) (\s -> do
  34. liftIO $ hPutStrLn stderr $ "Error calculating move"
  35. game <- getGame
  36. ownfields <- liftIO $ A.getAssocs (fields $ proponent game)
  37. let (a, _):_ = filter (\(_, (_, v)) -> v > 0) ownfields
  38. liftIO $ hPutStrLn stderr $ "=> I " ++ show a
  39. liftIO $ printMove (MoveLeft Card_I a)
  40. runMove (MoveLeft Card_I a)
  41. )
  42. run :: Turn Goal -> Bool -> IO ()
  43. run ai firstPlayer = do
  44. hSetBuffering stdin NoBuffering
  45. hSetBuffering stdout NoBuffering
  46. game <- initGame
  47. runTurn _loop1 game
  48. return ()
  49. where
  50. _loop1 :: Turn ()
  51. _loop1 = do
  52. when (firstPlayer) (stepAI ai)
  53. _loop
  54. _loop :: Turn ()
  55. _loop = do
  56. stepClient
  57. stepAI ai
  58. _loop
  59. getArgsFirstPlayer :: IO Bool
  60. getArgsFirstPlayer = do
  61. arg:_ <- getArgs
  62. return $ arg == "0"
  63. main = getArgsFirstPlayer >>= run aimove