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.

75 lines
2.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. module Datatypes.Game (Game(..), Command(..), completed, new, cacheToScore, updateCache, powerPhrases, phraseConverter) where -- FIXME exports
  2. import Data.Hashable (hash)
  3. import qualified Data.List as List
  4. import Data.Map.Strict (Map)
  5. import qualified Data.Map.Strict as Map
  6. import Data.Set (Set)
  7. import qualified Data.Set as Set
  8. import Datatypes.Board (Board)
  9. import Datatypes.Unit (Unit)
  10. import qualified Datatypes.Unit as Unit
  11. data Command = MoveW
  12. | MoveE
  13. | MoveSW
  14. | MoveSE
  15. | RotateClockwise
  16. | RotateCounterclockwise
  17. deriving (Show,Eq,Ord)
  18. type UnitHash = Int
  19. type PhrasesCache = Map [Command] Int
  20. data Game = Game {
  21. board :: Board,
  22. units :: [Unit],
  23. visitedUnits :: Set UnitHash,
  24. oldLines :: Int,
  25. score :: Int,
  26. history :: [Command],
  27. phrasesCache :: PhrasesCache
  28. }
  29. deriving Show
  30. completed :: Game -> Bool
  31. completed game = null $ units game
  32. new :: Board -> [Unit] -> Game
  33. new b us = Game {
  34. board = b,
  35. units = (c:cs),
  36. visitedUnits = Set.singleton (hash c),
  37. oldLines = 0,
  38. score = 0,
  39. history = [],
  40. phrasesCache = Map.empty
  41. } where (c:cs) = map (flip Unit.centeredIn b) us
  42. phraseConverter :: [Command] -> String
  43. phraseConverter s = if s == reverse [MoveE, MoveSW, MoveW]
  44. then "ei!"
  45. else if s == reverse [MoveSW, MoveSW, MoveW, MoveSE, MoveSW, MoveSW, MoveW]
  46. then "ia! ia!"
  47. else "PUPPA"
  48. powerPhrases :: [[Command]]
  49. powerPhrases = [
  50. reverse [MoveE, MoveSW, MoveW],
  51. reverse [MoveSW, MoveSW, MoveW, MoveSE, MoveSW, MoveSW, MoveW]
  52. ]
  53. updateCache :: PhrasesCache -> [Command] -> PhrasesCache
  54. updateCache cache history = innerUpdate cache history powerPhrases where
  55. innerUpdate cache _ [] = cache
  56. innerUpdate cache history (p:ps) = innerUpdate (updatedCache cache history p) history ps
  57. updatedCache c h p | p `List.isPrefixOf` h = if Map.member p c
  58. then Map.update (\a -> Just $ a + 1) p c
  59. else Map.insert p 1 c
  60. updatedCache c _ _ = c
  61. cacheToScore :: PhrasesCache -> Int
  62. cacheToScore items = sum $ map evalScore (Map.toAscList items) where
  63. evalScore (phrase, count) = 2 * (length phrase) * count + if count > 0 then 300 else 0