Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (76 sloc) 2.526 kb
82fdeef Initial commit
Rein Henrichs authored
1 -- | This is the documentation for the module Chess.
2 module Chess where
3
4 import Test.HUnit
5 import Test.QuickCheck
6
919aae1 End of episode 1
Rein Henrichs authored
7 type Board = [[Square]]
82fdeef Initial commit
Rein Henrichs authored
8
919aae1 End of episode 1
Rein Henrichs authored
9 initialBoardStr = unlines ["rnbqkbnr"
10 ,"pppppppp"
11 ," "
12 ," "
13 ," "
14 ," "
15 ,"pppppppp"
16 ,"rnbqkbnr"
17 ]
18
19 readBoard :: String -> Board
20 readBoard = map readRow . lines
21 where readRow = map readSquare
22
23 showBoard :: Board -> String
24 showBoard = unlines . map showRow
25 where showRow = map showSquare
26
27 type Square = Maybe Piece
28
29 -- | Show a square using FEN notation or ' ' for an empty square.
30 showSquare :: Square -> Char
31 showSquare = maybe ' ' showPiece
32
33 -- | Read a square using FEN notation or ' ' for an empty square.
34 readSquare :: Char -> Square
35 readSquare ' ' = Nothing
36 readSquare c = Just (readPiece c)
37
38 data Piece = Piece PColor PType deriving (Show)
39 data PColor = White | Black deriving (Show)
40 data PType = Pawn | Knight | Bishop | Rook | Queen | King deriving (Show)
41
42 -- | Shows a piece using FEN notation.
43 --
44 -- * White pieces are "PNBRQG"
45 -- * Black pieces are "pnbrqg"
46 showPiece :: Piece -> Char
47 showPiece (Piece White Pawn) = 'P'
48 showPiece (Piece White Knight) = 'N'
49 showPiece (Piece White Bishop) = 'B'
50 showPiece (Piece White Rook) = 'R'
51 showPiece (Piece White Queen) = 'Q'
52 showPiece (Piece White King) = 'K'
53 showPiece (Piece Black Pawn) = 'p'
54 showPiece (Piece Black Knight) = 'n'
55 showPiece (Piece Black Bishop) = 'b'
56 showPiece (Piece Black Rook) = 'r'
57 showPiece (Piece Black Queen) = 'q'
58 showPiece (Piece Black King) = 'k'
59
60 -- | Reads a piece using FEN notation.
61 --
62 -- * White pieces are "PNBRQG"
63 -- * Black pieces are "pnbrqg"
64 readPiece :: Char -> Piece
65 readPiece 'P' = (Piece White Pawn)
66 readPiece 'N' = (Piece White Knight)
67 readPiece 'B' = (Piece White Bishop)
68 readPiece 'R' = (Piece White Rook)
69 readPiece 'Q' = (Piece White Queen)
70 readPiece 'K' = (Piece White King)
71 readPiece 'p' = (Piece Black Pawn)
72 readPiece 'n' = (Piece Black Knight)
73 readPiece 'b' = (Piece Black Bishop)
74 readPiece 'r' = (Piece Black Rook)
75 readPiece 'q' = (Piece Black Queen)
76 readPiece 'k' = (Piece Black King)
82fdeef Initial commit
Rein Henrichs authored
77
78 -- Tests
79
80 tests = TestList $ map TestCase
81 [assertEqual "add tests here" 1 1
82 ]
83
84 prop_empty c1 = (c1::Int) == c1
85
86 runTests = do
87 runTestTT tests
88 quickCheck prop_empty
89
90 -- | For now, main will run our tests.
91 main :: IO ()
92 main = runTests
Something went wrong with that request. Please try again.