# Define a grammar to emmulate chess rules

Is it possible to define a 《chess language》: language={alphabet = {(chess pieces, squares of chess board)}, grammar={rules of movement of pieces over the board}}?

I looked online but I cannot find a way to define the grammar. Every way I can think of, is either stupidly long and complicated, or does not cover all the rules.

I presume the hard part is that the grammar has to take in consideration the state of the board before evaluating a move as valid or not.

Do you have insight how to approach this problem i.e: define a grammar that emulates chess rules?

The way chess apps work(not engines), is that at every move they calculate all possible moves and then check if the user's move is among the calculated moves, else it is invalid.

I want to accept the user's move and only then check if it's a valid one.

• A grammar is not the way to go. You can write a simple algorithm which checks whether a suggested move is legal. Aug 4, 2018 at 13:04
• Can you elaborate more please? How to do so without calculating all the possible moves everytime? Aug 4, 2018 at 18:27
• The first step is to check that the piece moves according to its specific rules, and that there is no other piece in the way. Then other checks are necessary, such as not moving a king in check. Some of the rules also depend on the history, for example en passant and castling, so you will need to maintain a small state as well. Aug 4, 2018 at 19:44
• By the way, the rules of chess depend on past positions, so a grammar of the form you desire cannot express them. Aug 4, 2018 at 19:50
• See the first comment. A grammar is not the way to go. Really, it's not. You will get nowhere trying to use a grammar. Aug 5, 2018 at 14:45

"The way chess apps work(not engines), is that at every move they calculate all possible moves and then check if the user's move is among the calculated moves, else it is invalid."

No, no, no, no, that's not how you check if a move is valid.

If you want all possible moves, then you generate all possible moves. If you want to check if a move is valid, then you check if that move is valid, and nothing else. There's no reason to know anything about all the other moves if all you want to know is to check if one move is valid.