Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Game(
- boardFromMinmax
- )where
- import Data.Maybe
- import Data.Tree
- import Data.List
- import Data.Map as Map
- import Board
- rateBoard:: Board->Color->Int
- rateBoard (Board innerMap) color =
- 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]
- where
- rates = rateDirections
- rate listOfCoordinates = sum (Prelude.map sumPoints (rateDirections listOfCoordinates))
- rateDirections:: [Coordinates]->[[Int]]
- rateDirections [] = [[0]]
- 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)])
- rateDirection:: Coordinates->[Coordinates]->Int->(Int,Int)->[Int]
- rateDirection _ [] seq (_,_) = [seq]
- rateDirection (Coordinates x y) ((Coordinates x1 y1):rest) seq (xTranslation, yTranslation)
- | (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)
- | otherwise = [seq] ++ rateDirection (Coordinates (x1+xTranslation) (y1+yTranslation)) rest 1 (xTranslation, yTranslation)
- sumPoints:: [Int]->Int
- sumPoints [] = 0
- sumPoints (xs:x)
- | xs == 5 = 2000 + (sumPoints x)
- | xs == 4 = 50 + (sumPoints x)
- | xs == 3 = 15 + (sumPoints x)
- | xs == 2 = 5 + (sumPoints x)
- | otherwise = 0 + (sumPoints x)
- rateBoards:: [Tree Board]->Color->[Int]
- rateBoards [] _ = []
- rateBoards ((Node currentNode _):listOfBoards) color = [rateBoard currentNode color] ++ rateBoards listOfBoards color
- possibleMoves:: Board->[Coordinates]
- possibleMoves (Board map)
- | (Map.null map) = [Coordinates x y | x<-[1..19], y<-[1..19]]
- | otherwise = [Coordinates x y | x<-[1..19], y<-[1..19], notMember (Coordinates x y) map, hasNeighbors (Coordinates x y) (Map.keys map)]
- buildTree:: Board->Color->Tree Board
- buildTree board color = Node board (Prelude.map (\board -> (buildTree board oppositeColor)) possibleBoards)
- where
- getListOfBoards _ _ [] = []
- getListOfBoards (Board b) color (x:xs) =[addToBoard (Board b) x color] ++ (getListOfBoards (Board b) color xs)
- oppositeColor = (negateColor color)
- possibleCoordinates = possibleMoves board
- possibleBoards = getListOfBoards board color possibleCoordinates
- minmaxAlfa:: [Tree Board]->Color->Int->Int->Int->Int->Int
- minmaxAlfa [] color level maxLevel alfa beta = alfa
- minmaxAlfa (x:xs) color level maxLevel alfa beta
- | (newAlfa>=beta) = beta
- | otherwise = minmaxAlfa xs color level maxLevel newAlfa beta
- where
- newAlfa = max alfa (exploreTree x color level maxLevel alfa beta)
- minmaxBeta:: [Tree Board]->Color->Int->Int->Int->Int->Int
- minmaxBeta [] color level maxLevel alfa beta = beta
- minmaxBeta (x:xs) color level maxLevel alfa beta
- | (alfa >= newBeta) = alfa
- | otherwise = minmaxBeta xs color level maxLevel alfa newBeta
- where
- newBeta = min beta (exploreTree x color level maxLevel alfa beta)
- exploreTree:: Tree Board->Color->Int->Int->Int->Int->Int
- exploreTree (Node node children) color level maxLevel alfa beta
- | Prelude.null children = rateBoard node color
- | level==maxLevel = minimum (rateBoards children color)
- | level `mod` 2 == 0 = minmaxBeta children color (level+1) maxLevel alfa beta
- | otherwise = minmaxAlfa children color (level+1) maxLevel alfa beta
- minmax:: Tree Board->Color->Int->Int
- minmax (Node node children) color maxLevel = exploreTree (Node node children) color 0 maxLevel (-1000) 1000
- boardFromMinmax:: Board->Color->Board
- boardFromMinmax board color = addToBoard board (moves!!fromJust(elemIndex (maximum minMaxOfMoves) minMaxOfMoves)) color
- where
- maxLevelOfMinmax = 4
- moves = possibleMoves board
- minMaxOfMoves = Prelude.map (\move -> (minmax (buildTree (addToBoard board move color ) color) color maxLevelOfMinmax)) moves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement