jak3122 / VoteChessBot

5 stars 4 forks source link

Parse moves case-insensitively #1

Open hassan-c opened 6 years ago

hassan-c commented 6 years ago

This suggestion comes from AbsoluteMadness on Lichess: https://lichess.org/forum/team-votechess/votechess-suggestions#3

If the move parser was case-insensitive, it'd make things a little easier, since I've noticed that many people often type "/o-o-o" but it isn't recognised by VoteChess since it expects "/O-O-O", or they may mistakenly write "/E4" instead of "/e4". But since VoteChess depends on the chess.js library, which treats SAN moves as case-sensitive, we can't just pass "E4" to the move() function since it would be treated as an invalid move and the function would return null.

Also, as I (hasc) mentioned in the thread, simply making the move lowercase could introduce ambiguity in the case where you have a piece on c3 and both your b-pawn and bishop are attacking it. Then, if somebody typed "BxC3" when they meant "Bxc3", it would be turned into "bxc3", which is an entirely different move.

So we need to convert moves to the proper format (so "o-o-o" to "O-O-O" and "E4" to "e4") before they are passed to the move() function. The way I would do it is as follows: Suppose a user has typed a move. If the typed move (e.g. "e4") has an exact match in the list of legal moves, then go ahead and make that move. Otherwise (e.g. "BXC3"), make the move lowercase and compare it with the lowercased list of legal moves. If there is exactly one match, we make that move. In the rare case, the lowercased move may have multiple matches, in which case we don't count that move at all and simply ignore it since we can't know whether the user meant "Bxc3" or "bxc3".

Does this sound like a good solution?

jak3122 commented 6 years ago

Thanks for the writeup! Yes, I was intending something similar. I'll get around to implementing it soon.

hassan-c commented 6 years ago

No problem! Ah right, awesome. I could help out if needed :-)