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
- 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 = ""
- rateVerticalNeighbors (Coordinates x y) listOfCoordinates
- | ((Coordinates x (y-1)) `elem` listOfCoordinates) || ((Coordinates x (y+1)) `elem` listOfCoordinates) = 1
- | otherwise = 0
- rateHorizontalNeighbors (Coordinates x y) listOfCoordinates
- | ((Coordinates (x-1) y) `elem` listOfCoordinates) || ((Coordinates (x+1) y) `elem` listOfCoordinates) = 1
- | otherwise = 0
- rateRightDiagonalNeighbors (Coordinates x y) listOfCoordinates
- | ((Coordinates (x-1) (y-1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y+1)) `elem` listOfCoordinates) = 1
- | otherwise = 0
- rateLeftDiagonalNeighbors (Coordinates x y) listOfCoordinates
- | ((Coordinates (x-1) (y+1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y-1)) `elem` listOfCoordinates) = 1
- | otherwise = 0
- rateBoard (Board map) color =
- rate 0 [x | x <- Map.keys map, fromJust (Map.lookup coordinates map) == color] - rate 0 [x | x <- Map.keys map, fromJust (Map.lookup coordinates map) == negate color]
- where
- rate:: Int->[Coordinates]->Int
- rate index listOfCoordinates =
- if index == (length listOfCoordinates - 1)
- then 0
- else rateVerticalNeighbors ((listOfCoordinates !! index) listOfCoordinates) + rateHorizontalNeighbors ((listOfCoordinates !! index) listOfCoordinates) +rateRightDiagonalNeighbors ((listOfCoordinates !! index) listOfCoordinates) + rateLeftDiagonalNeighbors ((listOfCoordinates !! index) listOfCoordinates) + rateIterating (index+1 listOfCoordinates)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement