Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. {-
  2. Written by Abai Edmund
  3. Version 1.0
  4. Last updated: 15/11/2017
  5. -}
  6.  
  7. module Dom3 where
  8. import DomsMatch
  9. testBoard :: DomBoard
  10. testBoard = (Board (1,2) (3,4) [((2,2),P1, 1), ((2,3), P2, 2), ((3,4), P1, 3), ((1,2), P2, 4)])
  11. -- Helper functions to allow extracting elements from 3-tuple, history
  12. first (a,_,_) = a -- get the first element from a tuple
  13. second (_,b,_) = b -- get the second element from a tuple
  14. third (_,_,c) = c -- get the third element
  15. {-
  16. getDoms get all previous dominos that have been played
  17.  
  18. -}
  19. getDoms :: DomBoard -> [Dom]
  20. getDoms (Board _ _ history) = previousDoms
  21. where previousDoms = map first history -- get every domino
  22. {-
  23. Guess opponents potential hand.
  24. - Look at previously played dominos, and discount them
  25. - Look at the ends, (e.g if the ends are 5 & 5 at some point and the opponent didnt play (5,5), they dont have it)
  26. - General rule if ends = x and the next dom played isn't (x,x) they dont have it
  27. -Filter all dominos that the opponent doesnt have from Domset
  28. -}
  29. --HELPER FUNCTIONS FOR GUESS OPPONENTS HAND
  30. {-
  31. LookAtEnds:
  32. If LeftEnd == RightEnd and the next dom played isn't (LeftEnd,RightEnd)
  33. return true
  34. True means they dont have a domino
  35. -}
  36. lookAtEnds :: DomBoard -> Bool
  37. lookAtEnds (Board (lEnd,_) (_,rEnd) [(dom,_,_)])
  38. | (lEnd == rEnd) && (dom /= (lEnd,lEnd)) = True
  39. | otherwise = False
  40. --for a domino, (a,b) if a < b then (a,b)=(b,a)
  41. flipDom :: Dom -> Dom
  42. flipDom (a,b)
  43. | a < b = (a,b)
  44. | otherwise = (b,a)
  45. --remove all played dominos from the predicted opponents hand
  46. removePlayedDoms :: DomBoard -> Hand
  47. removePlayedDoms b = hand
  48. where playedDoms = getDoms b
  49.  
  50. hand = filter(\doms -> flipDom(doms) `notElem` playedDoms) domSet
  51. --remove all dominos that you have in your hand
  52. removeOwnDoms :: Hand->Hand-> Hand
  53. removeOwnDoms yourHand dominoSet = playerHand
  54. where playerHand = filter(\dominos -> dominos `elem` yourHand) dominoSet
  55. -- guess potential dominos opponent may have using lookAtEnds
  56. --guessDom :: DomBoard -> [Dom]
  57. --guessDom (Board L) = guessedDoms
  58. --where
  59. opponentsHand :: DomBoard -> Hand -> Hand
  60. opponentsHand b h = potentialHand
  61. where rmPlayedDoms = removePlayedDoms b
  62. potentialHand = removeOwnDoms h rmPlayedDoms
  63.  
  64.  
  65.  
  66. --create tactics for smart player to use
  67. {-
  68. Check to see if player has domino 9
  69. -}
  70. {--
  71. First move tactic, if player has the domino (5,4) then play it
  72. if not then play the highest scoring domino
  73. --}
  74. --check to see if player has the domino (5,4)
  75. has54 :: DomBoard -> Hand -> Bool
  76. has54 _ [] = False
  77. has54 InitBoard (h:t)
  78. | h == (5,4) = True
  79. | otherwise = has54 InitBoard t
  80.  
  81. firstMove :: DomBoard -> Hand -> (Dom,End)
  82. firstMove InitBoard hand
  83. | has54 InitBoard hand = ((5,4),L)
  84. | otherwise = (dom,end)
  85. where (dom,end,_) = hsd hand InitBoard
  86.  
  87. {-
  88. playHighest
  89. play the highest scoring domino unless it risks the opponent scoring more
  90. check to see if the opponent could potentially play a high scoring dom after you play
  91. your hsd
  92. e.g if the ends are 6 and 0 and your hsd is 6,6. The opponent could play (0,3) resulting in a score of 8
  93. or (0,6) resulting in a score of 6 for them
  94. Use their potential hand to figure out if their hsd will result in them getting a higher score
  95. -}
  96.  
  97.  
  98.  
  99.  
  100. {-
  101. Stitch game: if you are behind your opponent and your hand is weak (low scoring)
  102. try and make both players knock so the game restarts
  103. -}
  104. --HELPER FUNCTIONS FOR STITCH GAME
  105.  
  106. {-
  107. isPlayerKnocking - Look through a board state, the previous board state
  108. If two successive moves were made by the same player then one was knocking
  109. return true if theyre knocking
  110. -}
  111.  
  112. isPlayerKnocking :: DomBoard -> Bool
  113. isPlayerKnocking (Board _ _ (h:t)) -- history = [(dom,player,movenum)]
  114. | second(h) == second(t!!0) = True
  115. | otherwise = False
  116. {-
  117. ReturnKPips , return all the times the opponent was knocking
  118. and check what will make you knock, then try to make both players knock
  119. -}
  120. returnKPips :: DomBoard -> [Int]
  121. returnKPips (Board (l1, r1) (l2,r2) history)
  122. | isPlayerKnocking (Board (l1, r1) (l2,r2) history) = [r1, l2]
  123. | otherwise = error "returnKPips: This shouldn't be reached"
  124. {-
  125. amILosing : if youre losing returns true
  126. -}
  127. amILosing :: Player -> Scores -> Bool
  128. amILosing player (score1,score2)
  129. | (player == P1) && (score1 < score2) = True
  130. | (player == P2) && (score1 > score2) = True
  131. | otherwise = False
  132.  
  133. {-
  134. myScore: returns your score
  135. -}
  136. myScore :: Player -> Scores -> Int
  137. myScore player (s1,s2)
  138. | player == P1 = s1
  139. | player == P2 = s2
  140. {-
  141. scoreDiff : returns the difference between the two scores
  142. -}
  143. scoreDiff :: Scores -> Int
  144. scoreDiff (s1,s2)
  145. | s1 > s2 = s1-s2
  146. | otherwise = s2-s1
  147. {-
  148. isHandWeak: if you're losing and your highest scoring dom is 2 or less then
  149. return true. if you also dont have many dominos that score at all then return
  150. true
  151. -}
  152.  
  153. isHandWeak :: DomBoard -> Hand -> Player -> Scores -> Bool
  154. isHandWeak board hand player scores
  155. | (highest < 3 ) && losing && myscore < 59 && scorediff > 4 = True
  156. | otherwise = False
  157. where (_,_,highest) = hsd hand board
  158. losing = amILosing player scores
  159. myscore = myScore player scores
  160. scorediff = scoreDiff scores
  161.  
  162. {-
  163. makePlayerKnock, tries to play a domino that will make the other person knock
  164.  
  165. -}
  166. makePlayerKnock :: DomBoard -> Hand -> (Dom,End)
  167. makePlayerKnock
  168.  
  169. {-
  170. Stitch game , make both knock
  171. -}
  172.  
  173. {-
  174. getTo59:
  175. if you are able to play a domino that gets 61 then play that domino,
  176. otherwise play a domino that can get you to 59, or as close to 59 as possiblePlays
  177. -}
  178.  
  179. {-
  180. Evaluate risk: Function which takes the potential hand of the opponent, your hand
  181. the current board and checks the scores. If you can make a play that has a low risk high reward play it
  182. returns an integer 'risk factor'. if certain conditions are fufilled then the risk factor goes up
  183. if the risk is low enough, make a play. only use evaluate risk in the mid game
  184.  
  185. For example, if the opponent has 2 dominos left and you want to play (6,6), what are the chances the opponent
  186. has (0,6) or (0,3). They have 2 dominos left so quite low, it might be worth the risk especially if playing (6,6)
  187. will put you in a better position
  188. -}
  189.  
  190.  
  191. --CREATE SMART PLAYER
  192. smartPlayer :: DomsPlayer
  193. smartPlayer h InitBoard p s = firstMove InitBoard h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement