Advertisement
Coriic

Untitled

May 31st, 2017
123
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, Show)
  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 innerMap) color =
  39.     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]
  40.         where
  41.             rate listOfCoordinates = sum (Prelude.map (\coords -> ((rateVerticalNeighbors coords listOfCoordinates) + (rateHorizontalNeighbors coords listOfCoordinates) +
  42.                 (rateRightDiagonalNeighbors coords listOfCoordinates) + (rateLeftDiagonalNeighbors coords listOfCoordinates))) listOfCoordinates)
  43.             rateVerticalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates 0 (-1) 0 1
  44.             rateHorizontalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates (-1) 0 1 0
  45.             rateRightDiagonalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates (-1) (-1) 1 1
  46.             rateLeftDiagonalNeighbors coords listOfCoordinates = rateNeighborsSequence coords listOfCoordinates (-1) 1 1 (-1)
  47.             rateNeighborsSequence (Coordinates x y) listOfCoordinates x_1 y_1 x_2 y_2
  48.                 | ((Coordinates (x+x_1) (y+y_1)) `elem` listOfCoordinates) || ((Coordinates (x+x_2) (y+y_2)) `elem` listOfCoordinates) = 1
  49.                 | otherwise = 0
  50.            
  51.  
  52. hasNeighbors (Coordinates x y) listOfCoordinates = ((Coordinates (x-1) (y-1)) `elem` listOfCoordinates) || ((Coordinates x (y-1)) `elem` listOfCoordinates) ||
  53.     ((Coordinates (x+1) (y-1)) `elem` listOfCoordinates) || ((Coordinates (x-1) y) `elem` listOfCoordinates) ||
  54.     ((Coordinates (x+1) y) `elem` listOfCoordinates) || ((Coordinates (x-1) (y+1)) `elem` listOfCoordinates) ||
  55.     ((Coordinates x (y+1)) `elem` listOfCoordinates) || ((Coordinates (x+1) (y+1)) `elem` listOfCoordinates)
  56.  
  57. 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