2

I'm trying to implement a function in Haskell that returns a list containing all possible moves for the player who's up. The function's only argument is a String composed of an actual state of the board (in Forsyth-Edwards Notation) followed by the moving player(b/w).

Notation example : rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w (starting board state)

So far I used a function to split the string into ranks, but I'm having a hard time coming up with an implementation.

Any tips?

6
  • 1
    Are you asking for specifically Haskell tips, or just general implementation tips? Naively, I would first create a function taking as input a piece type, coordinate and colour and outputting the "target" coordinates to which it can move, along with the coordinates traversed along the route. Input the FEN notation into a matrix of 1s for occupied cells and 0s for empty ones. Then loop over the players' pieces, calling this function and ruling out those "target" coordinates for which one of the cells "en route" is occupied (i.e has a 1 in the matrix). – Mark Butler Feb 15 at 23:32
  • 2
    You'll have to deal with a lot of cases separately - pawns capturing diagonally, en passant, castling (and no castling when under attack), checks and checkmates etc. – Mark Butler Feb 15 at 23:32
  • 1
    I was mainly asking for general tips, and this actually looks like a good place to start. Fyi this is a slightly simplified version of chess, so I don't have to tackle castling or en passant. – httplease Feb 16 at 8:02
  • 1
    This is an open-ended question, but that's why OP has posed it. Not all questions are factual. If OP had more "focus", the question would not need to be asked. There is certainly enough here to begin a useful dialogue, and I think the discussion already might be useful to others. The all-too-familiar knee-jerk closing of a perfectly ok question is unhelpful and is also unwelcoming to the a newcomer. – Laska Feb 16 at 11:22
  • @httplease to add to my comment, you might find this recent YouTube video "coding a Chess AI" very useful. It begins with a relatively sensible approach to listing legal moves. Implementation is Python not Haskell, but it is written functionally so should translate. – Mark Butler Feb 19 at 22:42
2

FEN is fine for unambiguous communication of a position (if you include castling and e.p. state too) but the internal representation should be as a board, with some kind of movement so you can efficiently check for legal moves, blocks, checks etc. Would hate to do all that with regex!

This quite a well-travelled area, and there must be much material online. You have made the choice to use Haskell here: what is the motivation for this? Are you trying to learn Haskell, or do you think it’s appropriate for certain aspects of a chess engine.

Chess is a finite directed binary graph, with each position just a node. There is certain machinery to reach that level of abstraction, but ultimately I would think a functional language like Haskell might really begin to show its worth when you have reached that conceptual level.

Depends also what you want your engine to do: find good moves, solve problems, analyse endgames, tablebases, retrograde analysis, support fairy chess. So please say more! :)

3
  • 1
    First of all thanks for the answer ;) What I'm trying to describe here is only a part of the engine, which is only supposed to handle the computing of possible/legal moves for a given player/game state. I should also mention that I'm implementing here a slightly different version of chess(Crazyhouse) with no castling or en passant capturing. As to why I've decided on Haskell, the main requirement of the project is that I do this functionally, so there really isn't much wiggle room there :) – httplease Feb 16 at 7:48
  • Hi httplease thanks for the classification. Leaving castling & en passant out for the first pass seems like a sensible approach, but you may want to leave the conceptual hooks in to add them later if necessary. The notion of state is even more essential to crazyhouse because each player has a handful of pieces to throw onto the board at any point. The main thing that strikes me about crazyhouse is that you can easily have 200 legal moves, most of which are trivially legal (unless there is a check). Orthodox chess is the hardest subset: might as well do that before adding the crazy stuff, imho – Laska Feb 16 at 9:13
  • 1
    That's true, I didn't realize that the extra moves(due to the crazyhouse concept) would add that much to the complexity. – httplease Feb 16 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.