Search code examples
haskelltestingchess

Property-based testing for a chess game


I'm trying to wrap my mind around property-based testing and chess. Currently I represent my chess game as a 2d-array and the only pieces I have implemented are pawns and knights for grokking this.

The pawn and knight represent their moves as the set of allMoves(x,y) \ invalidMoves(board,x,y). So one property I can think of is to test that allMoves(x,y) ∪ invalidMoves(board,x,y) === allMoves. But beyond that I'm not sure what else to test. I assume that I need to set up a simplified oracle model for the chess board but I'm not sure what such a model would be.


Solution

  • Start by just saying some obvious things that are true about real-life chess boards, no matter how dumb they sound. Many of those will be reasonable properties to test. Here are some ideas:

    • When you move a piece, it moves from the place where it was to the place where the move puts it.
    • No legal knight move is ever a legal pawn move.
    • A pawn never moves more than two squares at once.
    • A knight never moves adjacent to its starting position.
    • A move should only involve positions actually on the board.
    • Moving onto another piece decreases the number of pieces on the board.

    Many more ideas like this exist. They seem simple, but I guarantee your early implementations will miss some of them. Figure out how to write these invariants as properties, and grow your test suite from there.