Advertisement
Coriic

Untitled

May 30th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Map as Map
  2. import Data.Maybe
  3.  
  4. data Coordinates = Coordinates Int Int deriving Eq
  5.  
  6. instance Ord Coordinates where
  7.     compare (Coordinates a b) (Coordinates c d)
  8.         |(b == d) = a `compare` c
  9.         |otherwise = b `compare` d
  10.  
  11. data Color = White | Black deriving Eq
  12.  
  13. instance Show Color where
  14.    show White = "o"
  15.    show Black = "x"
  16.  
  17. data Board = Board (Map Coordinates Color)
  18.  
  19. instance Show Board where
  20.     show = toString
  21.  
  22. negateColor color
  23.     | (color==White) = Black
  24.     | otherwise = White
  25.  
  26. toString (Board map) =
  27.     iterate([Coordinates x y | y <- [1..19], x <- [1..19]])
  28.         where
  29.             iterate [element] = (mapToCharacter element) ++ (newLine element)
  30.             iterate (element:listOfCoordinates) = (mapToCharacter element) ++ (newLine element) ++ iterate(listOfCoordinates)
  31.             mapToCharacter coordinates
  32.                 | (member coordinates map) = show(fromJust (Map.lookup coordinates map))
  33.                 | otherwise = " "
  34.             newLine (Coordinates x y)
  35.                 | (x == 19) = "\n"
  36.                 | otherwise = ""
  37.  
  38. rateVerticalNeighbors (Coordinates x y) listOfCoordinates
  39.     | ((Coordinates x (y-1)) `elem` listOfCoordinates) || ((Coordinates x (y+1)) `elem` listOfCoordinates) = 1
  40.     | otherwise = 0
  41.  
  42. rateHorizontalNeighbors (Coordinates x y) listOfCoordinates
  43.     | ((Coordinates (x-1) y) `elem` listOfCoordinates) || ((Coordinates (x+1) y) `elem` listOfCoordinates) = 1
  44.     | otherwise = 0
  45.  
  46. rateRightDiagonalNeighbors (Coordinates x y) listOfCoordinates
  47.     | ((Coordinates (x-1) (y-1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y+1)) `elem` listOfCoordinates) = 1
  48.     | otherwise = 0
  49.  
  50. rateLeftDiagonalNeighbors (Coordinates x y) listOfCoordinates
  51.     | ((Coordinates (x-1) (y+1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y-1)) `elem` listOfCoordinates) = 1
  52.     | otherwise = 0
  53.  
  54.  
  55.  
  56. rateBoard (Board map) color =
  57.     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]
  58.         where
  59.             rate:: Int->[Coordinates]->Int
  60.             rate index listOfCoordinates =
  61.                 if index == (length listOfCoordinates - 1)
  62.                     then 0
  63.                     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