Skip to content

Instantly share code, notes, and snippets.

@lksnmnn
Forked from mheinzel/TestBotListAllMoves.hs
Created January 29, 2016 12:31
Show Gist options
  • Save lksnmnn/9f0d3347181495a34d85 to your computer and use it in GitHub Desktop.
Save lksnmnn/9f0d3347181495a34d85 to your computer and use it in GitHub Desktop.
Chess: FEN-Boards and possible moves
----------------------------------------------------------------------------
-- Test für die Funktion botListAllMoves :: String -> String -> [String]
--
-- Lizenz: MIT
-- Berlin, 25.01.2016
----------------------------------------------------------------------------
import Data.List (sort)
import ChessBot (botListAllMoves, botFindMove)
data TestCase = Case String String [String]
testCasesAllMoves :: [TestCase]
testCasesAllMoves = [
-- Paul
Case
"rnbkqbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBKQBNR"
"w"
["h2 h4","h2 h3","g2 g4","g2 g3","f2 f4","f2 f3","e2 e4","e2 e3",
"d2 d4","d2 d3","c2 c4","c2 c3","b2 b4","b2 b3","a2 a4","a2 a3",
"g1 h3","g1 f3","b1 c3","b1 a3"],
-- ravensinth:
Case
"3q2k1/1b5b/6p1/5P2/8/1R1b2Q1/1NP5/4K3"
"w"
["f5 f6","f5 g6","g3 h4","g3 f4","g3 e5","g3 d6","g3 c7","g3 b8",
"g3 h2","g3 f2","g3 h3","g3 f3","g3 e3","g3 d3","g3 g2","g3 g1",
"g3 g4","g3 g5","g3 g6","b3 c3","b3 d3","b3 a3","b3 b4","b3 b5",
"b3 b6","b3 b7","c2 c4","c2 c3","c2 d3","b2 c4","b2 a4","b2 d3",
"b2 d1","e1 d1","e1 d2","e1 f2"],
Case
"3q2k1/5r1b/6p1/5P2/8/1R4Q1/1NP5/4K3"
"b"
["g8 g7","g8 f8","g8 h8","d8 e7","d8 f6","d8 g5","d8 h4","d8 c7",
"d8 b6","d8 a5","d8 e8","d8 f8","d8 c8","d8 b8","d8 a8","d8 d7",
"d8 d6","d8 d5","d8 d4","d8 d3","d8 d2","d8 d1","f7 g7","f7 e7",
"f7 d7","f7 c7","f7 b7","f7 a7","f7 f6","f7 f5","f7 f8","g6 g5"],
Case
"3q1rk1/1b1nbppp/1p2pn2/1Ppp4/2PP4/4PN2/3NBPPP/B2Q1RK1"
"b"
["d8 c7","d8 e8","d8 c8","d8 b8","d8 a8","f8 e8","g8 h8","b7 c6",
"b7 a8","b7 a6","b7 c8","d7 b8","d7 e5","e7 d6","g7 g6","g7 g5",
"h7 h6","h7 h5","e6 e5","f6 h5","f6 g4","f6 e8","f6 e4","c5 d4",
"d5 c4"],
Case
"2r3k1/2q2ppp/1p2p3/1P1nP3/8/5Q1P/5PP1/B4RK1"
"w"
["f3 e4","f3 d5","f3 e2","f3 d1","f3 g4","f3 h5","f3 f4","f3 f5",
"f3 f6","f3 f7","f3 g3","f3 e3","f3 d3","f3 c3","f3 b3","f3 a3",
"h3 h4","g2 g3","g2 g4","a1 b2","a1 c3","a1 d4","f1 e1","f1 d1",
"f1 c1","f1 b1","g1 h1","g1 h2"],
Case
"3q2k1/P4r1b/6pR/8/8/4B3/2PN4/4K3"
"w"
["a7 a8","h6 g6","h6 h5","h6 h4","h6 h3","h6 h2","h6 h1","h6 h7",
"e3 f4","e3 g5","e3 d4","e3 c5","e3 b6","e3 f2","e3 g1","d2 e4",
"d2 c4","d2 f3","d2 b3","d2 f1","d2 b1","c2 c4","c2 c3","e1 d1",
"e1 e2"],
Case
"2r2rk1/pbq2ppp/1p1b4/PPpp4/3Pn3/3BPNP1/1B3P1P/R2Q1RK1"
"b"
["c8 d8","c8 e8","c8 b8","c8 a8","f8 e8","f8 d8","g8 h8","a7 a6",
"b7 c6","b7 a8","b7 a6","c7 b8","c7 d8","c7 c6","c7 d7","c7 e7",
"f7 f6","f7 f5","g7 g6","g7 g5","h7 h6","h7 h5","b6 a5","d6 e5",
"d6 f4","d6 g3","d6 e7","c5 c4","c5 d4","e4 g3","e4 c3","e4 g5",
"e4 f2","e4 d2","e4 f6"],
Case
"rn1qkb1r/pb1p1ppp/1p2pn2/1Pp5/2P5/4PN2/PB1P1PPP/RN1QKB1R"
"b"
["b8 c6","b8 a6","d8 e7","d8 c7","d8 c8","e8 e7","f8 e7","f8 d6",
"h8 g8","a7 a6","a7 a5","b7 c6","b7 d5","b7 e4","b7 f3","b7 a6",
"b7 c8","d7 d6","d7 d5","g7 g6","g7 g5","h7 h6","h7 h5","e6 e5",
"f6 h5","f6 d5","f6 g4","f6 e4","f6 g8"],
Case
"2rqk2r/pb1n1ppp/1p1bpn2/1Ppp4/P1PP4/4PN2/1B2BPPP/RN1Q1RK1"
"b"
["c8 c7","c8 c6","c8 b8","c8 a8","d8 e7","d8 c7","e8 f8","e8 e7",
"h8 g8","h8 f8","a7 a6","a7 a5","b7 c6","b7 a8","b7 a6","d7 b8",
"d7 f8","d7 e5","g7 g6","g7 g5","h7 h6","h7 h5","d6 e5","d6 f4",
"d6 g3","d6 h2","d6 c7","d6 b8","d6 e7","d6 f8","e6 e5","f6 h5",
"f6 g4","f6 e4","f6 g8","c5 d4","d5 c4"],
Case
"1r1q1rk1/1b1n1pb1/p1Bp3p/6p1/Q1P5/2N3P1/P2BPP1P/2R1R1K1"
"b"
["b8 c8","b8 a8","d8 e7","d8 f6","d8 c7","d8 b6","d8 a5","d8 e8",
"d8 c8","f8 e8","g8 h8","g8 h7","b7 c6","b7 a8","b7 c8","d7 f6",
"d7 b6","d7 e5","d7 c5","f7 f6","f7 f5","g7 f6","g7 e5","g7 d4",
"g7 c3","g7 h8","a6 a5","d6 d5","h6 h5","g5 g4"],
Case
"8/8/2P1Q3/1q2b1pk/7p/6P1/r4P1P/3R1RK1"
"w"
["c6 c7","e6 f5","e6 g4","e6 h3","e6 d7","e6 c8","e6 d5","e6 c4",
"e6 b3","e6 a2","e6 f7","e6 g8","e6 e5","e6 e7","e6 e8","e6 f6",
"e6 g6","e6 h6","e6 d6","g3 g4","g3 h4","f2 f3","f2 f4","h2 h3",
"d1 d2","d1 d3","d1 d4","d1 d5","d1 d6","d1 d7","d1 d8","d1 e1",
"d1 c1","d1 b1","d1 a1","f1 e1","g1 h1","g1 g2"]
]
testCasesCheckMate :: [TestCase]
testCasesCheckMate = [
Case
"8/8/2P1Q3/1q2b1pk/7p/6P1/r4P1P/3R1RK1"
"w"
["g3 g4"],
Case
"8/1r5p/R5p1/2p1kp2/1PP5/P3K2P/5PP1/8"
"w"
["f2 f4"],
Case
"rn5k/pp1r2pp/2p1Q2B/8/8/2N3K1/PPP2PP1/R4R2"
"w"
["e6 e8"],
Case
"3r3k/1p5p/p3Q3/4p1p1/4PbP1/1B1P4/P3KP1P/8"
"w"
["e6 f6"],
Case
"8/8/8/qn6/kn6/1n6/1KP5/8"
"w"
["c2 b3"],
Case
"3q2rk/pbpp2pp/1p5N/6b1/2P1P3/P7/1PP2PPP/R4RK1"
"w"
["h6 f7"],
Case
"r1b3k1/ppp2r1p/2np1qNQ/2b1pp2/2B1P3/3P4/PPP2PPP/R3K2R"
"w"
["h6 f8"],
-- This case tests pawn to queen promotion with check mate for white
Case
"4kr2/1P5R/8/8/8/8/8/K7"
"w"
["b7 b8"]
]
testAllMoves :: TestCase -> Bool
testAllMoves (Case board color solution) = let result = botListAllMoves board color
in sort result == sort solution
testCheckMateMoves :: TestCase -> Bool
testCheckMateMoves (Case board color solution) = let result = [botFindMove board color]
in result == solution
runAllMovesTests :: [TestCase] -> String
runAllMovesTests tests =
let failedTests = filter (not . testAllMoves) tests
showFailedTest (n, Case b c s) =
show n ++ ") " ++ b ++ " " ++ c ++ " failed!\n"
++ "should be:\n"
++ show (sort s) ++ "\n"
++ "is:\n"
++ show (sort (botListAllMoves b c)) ++ "\n"
in if null failedTests
then "All AllMove tests passed!"
else unlines . map showFailedTest . zip [1..] $ failedTests
runCheckMateTests :: [TestCase] -> String
runCheckMateTests tests =
let failedTests = filter (not . testCheckMateMoves) tests
showFailedMateTest (n, Case b c s) =
show n ++ ") " ++ b ++ " " ++ c ++ " failed!\n"
++ "should be:\n"
++ show s ++ "\n"
++ "is:\n"
++ show ([(botFindMove b c)]) ++ "\n"
in if null failedTests
then "All Check Mate tests passed!"
else unlines . map showFailedMateTest . zip [1..] $ failedTests
main :: IO ()
main = do putStrLn (runAllMovesTests testCasesAllMoves)
putStrLn (runCheckMateTests testCasesCheckMate)
@lksnmnn
Copy link
Author

lksnmnn commented Jan 29, 2016

Einfach in den gleichen Ordner wie eure ChessBot.hs kopieren und mit "ghc TestBotListAllMoves" kompilieren bzw. ohne vorheriges Kompilieren mit "runhaskell TestBotListAllMoves" ausführen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment