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 = ""
- rateBoard (Board map) color =
- 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]
- where
- rate listOfCoordinates = rateIterating listOfCoordinates listOfCoordinates
- rateIterating [] _ = 0
- rateIterating (coordinates:rest) listOfCoordinates = rateVerticalNeighbors coordinates listOfCoordinates +
- rateHorizontalNeighbors coordinates listOfCoordinates +rateRightDiagonalNeighbors coordinates listOfCoordinates +
- rateLeftDiagonalNeighbors coordinates listOfCoordinates + rateIterating rest listOfCoordinates
- 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
- 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