Advertisement
Coriic

Untitled

Jun 7th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Game(
  2.         boardFromMinmax
  3.     )where
  4.  
  5. import Data.Maybe
  6. import Data.Tree
  7. import Data.List
  8. import Data.Map as Map
  9. import Board
  10.  
  11. rateBoard:: Board->Color->Int
  12. rateBoard (Board innerMap) color =
  13.     rate [x | x <- Map.keys innerMap, fromJust (Map.lookup x innerMap) == color] - rate [x | x <- Map.keys innerMap, fromJust (Map.lookup x innerMap) == negateColor color]
  14.         where
  15.             rates = rateDirections
  16.             rate listOfCoordinates = sum (Prelude.map sumPoints (rateDirections listOfCoordinates))
  17.  
  18. rateDirections:: [Coordinates]->[[Int]]
  19. rateDirections [] = [[0]]
  20. rateDirections (coords:rest) = (Prelude.map (rateDirection coords (coords:rest) 0) [(x,y) | x<-[0..1], y<-[-1..1], not(x==0 && y==(-1)) && not(x==0 && y==0)])
  21.  
  22. rateDirection:: Coordinates->[Coordinates]->Int->(Int,Int)->[Int]
  23. rateDirection _ [] seq (_,_) = [seq]
  24. rateDirection (Coordinates x y) ((Coordinates x1 y1):rest) seq (xTranslation, yTranslation)
  25.     | (Coordinates x y) `elem` ((Coordinates x1 y1):rest) = rateDirection (Coordinates (x+xTranslation) (y+yTranslation)) (Data.List.delete (Coordinates x y) ((Coordinates x1 y1):rest)) (seq+1) (xTranslation, yTranslation)
  26.     | otherwise = [seq] ++ rateDirection (Coordinates (x1+xTranslation) (y1+yTranslation)) rest 1 (xTranslation, yTranslation)
  27.  
  28. sumPoints:: [Int]->Int
  29. sumPoints [] = 0
  30. sumPoints (xs:x)
  31.     | xs == 5 = 2000 + (sumPoints x)
  32.     | xs == 4 = 50 + (sumPoints x)
  33.     | xs == 3 = 15 + (sumPoints x)
  34.     | xs == 2 = 5 + (sumPoints x)
  35.     | otherwise = 0 + (sumPoints x)
  36.  
  37. rateBoards:: [Tree Board]->Color->[Int]
  38. rateBoards [] _ = []
  39. rateBoards ((Node currentNode _):listOfBoards) color = [rateBoard currentNode color] ++ rateBoards listOfBoards color
  40.  
  41. possibleMoves:: Board->[Coordinates]
  42. possibleMoves (Board map)
  43.     | (Map.null map) = [Coordinates x y | x<-[1..19], y<-[1..19]]
  44.     | otherwise = [Coordinates x y | x<-[1..19], y<-[1..19], notMember (Coordinates x y) map, hasNeighbors (Coordinates x y) (Map.keys map)]
  45.  
  46. buildTree:: Board->Color->Tree Board
  47. buildTree board color = Node board (Prelude.map (\board -> (buildTree board oppositeColor)) possibleBoards)
  48.     where
  49.         getListOfBoards _ _ [] = []
  50.         getListOfBoards (Board b) color (x:xs) =[addToBoard (Board b) x color] ++ (getListOfBoards (Board b) color xs)
  51.         oppositeColor = (negateColor color)
  52.         possibleCoordinates = possibleMoves board
  53.         possibleBoards = getListOfBoards board color possibleCoordinates
  54.  
  55. minmaxAlfa:: [Tree Board]->Color->Int->Int->Int->Int->Int
  56. minmaxAlfa [] color level maxLevel alfa beta = alfa
  57. minmaxAlfa (x:xs) color level maxLevel alfa beta
  58.     | (newAlfa>=beta) = beta
  59.     | otherwise  = minmaxAlfa xs color level maxLevel newAlfa beta
  60.     where
  61.         newAlfa = max alfa (exploreTree x color level maxLevel alfa beta)
  62.  
  63. minmaxBeta:: [Tree Board]->Color->Int->Int->Int->Int->Int
  64. minmaxBeta [] color level maxLevel alfa beta = beta
  65. minmaxBeta (x:xs) color level maxLevel alfa beta
  66.     | (alfa >= newBeta) = alfa
  67.     | otherwise = minmaxBeta xs color level maxLevel alfa newBeta
  68.     where
  69.         newBeta = min beta (exploreTree x color level maxLevel alfa beta)
  70.  
  71. exploreTree:: Tree Board->Color->Int->Int->Int->Int->Int
  72. exploreTree (Node node children) color level maxLevel alfa beta
  73.     | Prelude.null children = rateBoard node color
  74.     | level==maxLevel = minimum (rateBoards children color)
  75.     | level `mod` 2 == 0 = minmaxBeta children color (level+1) maxLevel alfa beta
  76.     | otherwise = minmaxAlfa children color (level+1) maxLevel alfa beta
  77.  
  78. minmax:: Tree Board->Color->Int->Int
  79. minmax (Node node children) color maxLevel = exploreTree (Node node children) color 0 maxLevel (-1000) 1000
  80.  
  81. boardFromMinmax:: Board->Color->Board
  82. boardFromMinmax board color = addToBoard board (moves!!fromJust(elemIndex (maximum minMaxOfMoves) minMaxOfMoves)) color
  83.     where
  84.         maxLevelOfMinmax = 4
  85.         moves = possibleMoves board
  86.         minMaxOfMoves = Prelude.map (\move -> (minmax (buildTree (addToBoard board move color ) color) color maxLevelOfMinmax)) moves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement