Advertisement
Guest User

warcaby

a guest
May 31st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import System.IO
  3.  
  4.  
  5. data Color = Black | White deriving (Show,Eq)
  6. data Pawn = Man | King deriving (Show,Eq)
  7. data F = F Color Pawn | Empty deriving (Show,Eq)
  8.  
  9. type State = Color
  10. type Board = [[(F,(Int,Int))]]
  11. type Positions = [(F,(Int,Int))]
  12.  
  13. figureToChar :: (F,(Int,Int)) -> Char
  14.  
  15. figureToChar x
  16.     | kind == (F Black Man) = 'b'
  17.     | kind == (F White Man) = 'w'
  18.     | kind == (F Black King) = 'B'
  19.     | kind == (F White King) = 'W'
  20.     | kind == (Empty) = '.'
  21.     where kind = fst x
  22.  
  23. size :: Int
  24. size = 8
  25. currentPlayer :: State
  26. currentPlayer = White
  27. board :: Board
  28. board = [[(Empty,(1,1)),(F Black Man,(1,2)),(Empty,(1,3)),(F Black Man,(1,4)),(Empty,(1,5)),(F Black Man,(1,6)),(Empty,(1,7)),(F Black Man,(1,8))],
  29.         [(F Black Man,(2,1)),(Empty,(2,2)),(F Black Man,(2,3)),(Empty,(2,4)),(F Black Man,(2,5)),(Empty,(2,6)),(F Black Man,(2,7)),(Empty,(2,8))],
  30.         [(Empty,(3,1)),(F Black Man,(3,2)),(Empty,(3,3)),(F Black Man,(3,4)),(Empty,(3,5)),(F Black Man,(3,6)),(Empty,(3,7)),(F Black Man,(3,8))],
  31.         [(Empty,(4,1)),(Empty,(4,2)),(Empty,(4,3)),(Empty,(4,4)),(Empty,(4,5)),(Empty,(4,6)),(Empty,(4,7)),(Empty,(4,8))],
  32.         [(Empty,(5,1)),(Empty,(5,2)),(Empty,(5,3)),(Empty,(5,4)),(Empty,(5,5)),(Empty,(5,6)),(Empty,(5,7)),(Empty,(5,8))],
  33.         [(F White Man,(6,1)),(Empty,(6,2)),(F White Man,(6,3)),(Empty,(6,4)),(F White Man,(6,5)),(Empty,(6,6)),(F White Man,(6,7)),(Empty,(6,8))],
  34.         [(Empty,(7,1)),(F White Man,(7,2)),(Empty,(7,3)),(F White Man,(7,4)),(Empty,(7,5)),(F White Man,(7,6)),(Empty,(7,7)),(F White Man,(7,8))],
  35.         [(F White Man,(8,1)),(Empty,(8,2)),(F White Man,(8,3)),(Empty,(8,4)),(F White Man,(8,5)),(Empty,(8,6)),(F White Man,(8,7)),(Empty,(8,8))]]
  36. whitePositions :: Positions
  37. whitePositions = [(F White Man,(6,1)),(F White Man,(6,3)),(F White Man,(6,5)),(F White Man,(6,7)),
  38.                  (F White Man,(7,2)),(F White Man,(7,4)),(F White Man,(7,6)),(F White Man,(7,8)),
  39.                  (F White Man,(8,1)),(F White Man,(8,3)),(F White Man,(8,5)),(F White Man,(8,7))]
  40.                  
  41. blackPositions :: Positions
  42. blackPositions = [(F Black Man,(1,2)),(F Black Man,(1,4)),(F Black Man,(1,6)),(F Black Man,(1,8)),
  43.                  (F Black Man,(2,1)),(F Black Man,(2,3)),(F Black Man,(2,5)),(F Black Man,(2,7)),
  44.                  (F Black Man,(3,2)),(F Black Man,(3,4)),(F Black Man,(3,6)),(F Black Man,(3,8))]
  45.                  
  46. getPossibleMoves :: (F,(Int,Int)) -> [(Int,Int)]
  47. getPossibleMoves x = case fst x of
  48.     F Black Man -> if snd (snd x) == 1
  49.         then [(fst(snd x) + 1,snd (snd x) + 1)]
  50.         else if snd (snd x) == 8
  51.             then [(fst(snd x) + 1,snd (snd x) - 1)]
  52.         else
  53.             [(fst(snd x) + 1,snd (snd x) - 1),(fst(snd x) + 1,snd (snd x) + 1)]
  54.     F White Man -> if snd (snd x) == 1
  55.         then [(fst(snd x) - 1,snd (snd x) + 1)]
  56.         else if snd (snd x) == 8
  57.             then [(fst(snd x) - 1,snd (snd x) - 1)]
  58.         else
  59.             [(fst(snd x) - 1,snd (snd x) - 1),(fst(snd x) + 1,snd (snd x) + 1)]
  60.     Empty -> []
  61.  
  62. checkMove :: (Int,Int) -> Positions -> Bool
  63. checkMove x y
  64.     | null y = False
  65.     | (snd (head y)) == x = True
  66.     | otherwise = checkMove x (tail y)
  67.  
  68. filterPossibleMoves :: [(Int,Int)] -> Positions -> [(Int,Int)]
  69. filterPossibleMoves x y
  70.     | null x = []
  71.     | checkMove (head x) y = [head x] ++ filterPossibleMoves (tail x) y
  72.     | otherwise = filterPossibleMoves (tail x) y
  73.  
  74. mappedMoves :: [[(Int,Int)]]
  75. mappedMoves = map getPossibleMoves whitePositions
  76.  
  77. filteredMoves :: [[(Int,Int)]]
  78. filteredMoves = map (filterPossibleMoves mappedMoves whitePositions)
  79.    
  80. showBoardLine :: [(F,(Int,Int))] -> String
  81. showBoardLine x = map figureToChar x ++ "\n"
  82.  
  83. showBoard :: Board -> String
  84. showBoard = concatMap showBoardLine
  85.  
  86. showInitBoard :: IO()
  87. showInitBoard = putStrLn $ showBoard board
  88.  
  89.  
  90. main :: IO ()
  91. main = do
  92.     putStrLn "Checkers starting"
  93.     showInitBoard
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement