I'm working on a Chess-based hobby project with HTML/CSS/PHP. I wasn't familiar with chess beforehand, so I decided to make a tool that would show which moves were allowed based on the type and square of a given piece.
I have an HTML form with two text fields: one is for the type of the piece and the other one is for current square of said piece. Everything works, but I want to include validation using regular expressions.
The valid, case-insensitive inputs for the piece type are p, pawn, r, rook, b, bishop, n, knight, q, queen, k, king.
The valid, case-insensitive inputs for the square are LetterNumber where Letter can be A-H and Number can be 1-8.
So I'm wondering if using regular expressions to would be possible/practical and if so, could anyone let me know what they are? I'm thinking that I should just use a conditional statement with logical-ors for the piece type but I'm curious to know if there are other solutions.
Programming chess movement validation
You can always use regular expressions to at least set up which tiles to investigate (no need to investigate tiles where the piece simply cannot move) in example, a maximum of four tiles should be checked when picking up a peon (two strike locations, single move, and possibly the initial double move).
I belive it can be done with something like this :
1 algoritm gets a array of fields to be checked. fields detects path for blocking pieces (good and bad). (not needed by the Knight, since he's crazy ;] ) fields detects enemy pieces for striking (in path, with the exception of the pawn.)
remember : - If the move will put you in check, you can not perform it. - If you are in check, you can ONLY avoid check or - Only "special rules" are castling and promotion. And promotion doesn't really affect your movement calculation. Castling can be done with two booleans, "queen side intact" and "king side intact", starting true and breaking to false if any ever move (don't just detect if they stand on the right place as some chess sims do... its embarresing. Moving your queen back to her starting position does NOT open up for queens castling.)
More decent PHP Chess resources
Here are some php based chess games, i dont have the time to look through them all, but there should be plenty of move-validation to learn from :
http://www.bebogame.com/download/php/multiplayer_chess/multiplayer_chess.zip
http://sourceforge.net/projects/some-chess/files/Some%20Chess%202.0/Some%20Chess%202.0%20beta%203/SomeChess_2.0b3.tbz/download
http://www.redlionwebdesign.com/phpchess.htm
Regular Expressions on the inputs:
You could evaluate onblur, onchange, and onsubmit for the form.
I do agree that validating the move on the Client-Side and Server-Side would make a lot of sense as well.
I strongly recommend against regular expressions for a task like this. The valid moves of any given piece are relative to its current position and color and are better validated via a verification function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With