Advertisement
Coriic

Untitled

May 30th, 2017
115
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. rateBoard (Board map) color =
  39.     rate [x | x <- Map.keys map, fromJust (Map.lookup x map) == color] - rate [x | x <- Map.keys map, fromJust (Map.lookup x map) == negateColor color]
  40.         where
  41.             rate listOfCoordinates = rateIterating listOfCoordinates listOfCoordinates
  42.             rateIterating [] _ = 0
  43.             rateIterating (coordinates:rest) listOfCoordinates = rateVerticalNeighbors coordinates listOfCoordinates +
  44.                 rateHorizontalNeighbors coordinates listOfCoordinates +rateRightDiagonalNeighbors coordinates listOfCoordinates +
  45.                 rateLeftDiagonalNeighbors coordinates listOfCoordinates + rateIterating rest listOfCoordinates
  46.             rateVerticalNeighbors (Coordinates x y) listOfCoordinates
  47.                 | ((Coordinates x (y-1)) `elem` listOfCoordinates) || ((Coordinates x (y+1)) `elem` listOfCoordinates) = 1
  48.                 | otherwise = 0
  49.             rateHorizontalNeighbors (Coordinates x y) listOfCoordinates
  50.                 | ((Coordinates (x-1) y) `elem` listOfCoordinates) || ((Coordinates (x+1) y) `elem` listOfCoordinates) = 1
  51.                 | otherwise = 0
  52.             rateRightDiagonalNeighbors (Coordinates x y) listOfCoordinates
  53.                 | ((Coordinates (x-1) (y-1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y+1)) `elem` listOfCoordinates) = 1
  54.                 | otherwise = 0
  55.             rateLeftDiagonalNeighbors (Coordinates x y) listOfCoordinates
  56.                 | ((Coordinates (x-1) (y+1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y-1)) `elem` listOfCoordinates) = 1
  57.                 | otherwise = 0
  58.  
  59. hasNeighbors (Coordinates x y) listOfCoordinates = ((Coordinates (x-1) (y-1)) `elem` listOfCoordinates) || ((Coordinates x (y-1)) `elem` listOfCoordinates) ||
  60.     ((Coordinates (x+1) (y-1)) `elem` listOfCoordinates) || ((Coordinates (x-1) y) `elem` listOfCoordinates) ||
  61.     ((Coordinates (x+1) y) `elem` listOfCoordinates) || ((Coordinates (x-1) (y+1)) `elem` listOfCoordinates) ||
  62.     ((Coordinates x (y+1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y+1)) `elem` listOfCoordinates)
  63.  
  64. 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