API of ruleset
From a bottom-up perspective, the next thing that needs designing is the chess ruleset. Ideally the ruleset will be separated from the board/AI implementation. This issue, then, is a little bit of brainstorming on the overview of the API.
rules
will be either a module or a class. I don't believe we'll need to hold any instance variables, so a module makes most sense. But for most people, a class/object is more "natural" to pass around. But seeing as everything is an object in Python, a module will probably suffice.
It will, to my estimate, contain the following functions:
-
legal_moves(position)
- all legal moves (from-to) for a given position -
lines_of_attack(position)
- all squares under attack. Not sure if it needs to return from-to's? In any case, not all attacks are legal moves, but do count towards a check[mate]. -
do_move(position, move, force)
- return a new position. don't perform checks if force is true. -
is_check(position)
- obvious -
is_stale(position)
- no possible moves -
is_checkmate(position)
- both of the above -
is_stalemate(position)
- stale without check -
is_gameover(position)
- obvious. possibly return enum of the type of gameover -
is_draw(position)
- 50-move or insufficient material -
is_threefold_repetition(?)
- A sort of draw when the same position occurs thrice. This would require building a historykeeper into the ruleset. Do this? -
is_insufficient_material(position)
- not enough material to play
Of these functions, only lines_of_attack
isn't meant to be exposed in the proposed Game API.
is_threefold_repetition
is rather annoying, because it would mean building a historykeeper into the ruleset rather than into the Game. This is a bit less elegant than I'd intended. But it appears necessary for a to-the-book chess implementation. Thoughts?