Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Map as Map
- import Data.Maybe
- data Coordinates = Coordinates Int Int deriving (Eq, Show)
- instance Ord Coordinates where
- compare (Coordinates a b) (Coordinates c d)
- |(b == d) = a `compare` c
- |otherwise = b `compare` d
- data Color = White | Black deriving Eq
- instance Show Color where
- show White = "o"
- show Black = "x"
- data Board = Board (Map Coordinates Color)
- instance Show Board where
- show = toString
- negateColor color
- | (color==White) = Black
- | otherwise = White
- toString (Board map) =
- iterate([Coordinates x y | y <- [1..19], x <- [1..19]])
- where
- iterate [element] = (mapToCharacter element) ++ (newLine element)
- iterate (element:listOfCoordinates) = (mapToCharacter element) ++ (newLine element) ++ iterate(listOfCoordinates)
- mapToCharacter coordinates
- | (member coordinates map) = show(fromJust (Map.lookup coordinates map))
- | otherwise = " "
- newLine (Coordinates x y)
- | (x == 19) = "\n"
- | otherwise = ""
- 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
- rate listOfCoordinates = sum (Prelude.map (\coords -> ((rateVerticalNeighbors coords listOfCoordinates) + (rateHorizontalNeighbors coords listOfCoordinates) +
- (rateRightDiagonalNeighbors coords listOfCoordinates) + (rateLeftDiagonalNeighbors coords listOfCoordinates))) listOfCoordinates)
- rateVerticalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates 0 (-1) 0 1
- rateHorizontalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates (-1) 0 1 0
- rateRightDiagonalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates (-1) (-1) 1 1
- rateLeftDiagonalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates (-1) 1 1 (-1)
- rateNeighborsSequence (Coordinates x y) listOfCoordinates x_1 y_1 x_2 y_2
- | ((Coordinates (x+x_1) (y+y_1)) `elem` listOfCoordinates) || ((Coordinates (x+x_2) (y+y_2)) `elem` listOfCoordinates) = 1
- | otherwise = 0
- hasNeighbors (Coordinates x y) listOfCoordinates = ((Coordinates (x-1) (y-1)) `elem` listOfCoordinates) || ((Coordinates x (y-1)) `elem` listOfCoordinates) ||
- ((Coordinates (x+1) (y-1)) `elem` listOfCoordinates) || ((Coordinates (x-1) y) `elem` listOfCoordinates) ||
- ((Coordinates (x+1) y) `elem` listOfCoordinates) || ((Coordinates (x-1) (y+1)) `elem` listOfCoordinates) ||
- ((Coordinates x (y+1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y+1)) `elem` listOfCoordinates)
- possibleMoves (Board map) = [Coordinates x y|x<-[1..19], y<-[1..19], notMember (Coordinates x y) map, hasNeighbors (Coordinates x y) (Map.keys map)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement