用于验证基于国际象棋的输入的正则表达式?
我正在使用 HTML/CSS/PHP 开发一个基于国际象棋的爱好项目。我之前对国际象棋并不熟悉,所以我决定制作一个工具,根据给定棋子的类型和方格显示允许哪些走法。
我有一个带有两个文本字段的 HTML 表单:一个用于该块的类型,另一个用于该块的当前方块。一切正常,但我想包括使用正则表达式的验证。
棋子类型的有效且不区分大小写的输入为p、pawn、r、rook、b、bishop、n、knight、q、queen、k、king。
正方形的有效且不区分大小写的输入是 LetterNumber,其中 Letter 可以是 AH,Number 可以是 < em>1-8。
所以我想知道使用正则表达式是否可能/实用,如果可以,有人可以让我知道它们是什么吗?我想我应该只使用带有逻辑或的条件语句来表示片段类型,但我很想知道是否还有其他解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编程国际象棋移动验证
您始终可以使用正则表达式至少设置要调查的棋子(无需调查棋子根本无法移动的棋子),例如,最多应检查四个棋子当拿起一个苦工时(两个打击位置,单步移动,可能还有最初的双步移动)。
我相信可以用这样的方法来完成:
1 算法获取要检查的字段数组。
字段检测阻塞块的路径(好的和坏的)。 (骑士不需要,因为他疯了;])
fields 检测要攻击的敌方棋子(在路径中,棋子除外。)
记住:
- 如果这个动作会让你受到限制,你就不能执行它。
- 如果你被检查,你只能避免检查或
- 只有“特殊规则”是易位和晋级。而且晋升并不会真正影响你的运动计算。易位可以用两个布尔值来完成,“皇后面完整”和“国王面完整”,从 true 开始,如果有任何移动,则中断为 false(不要像某些国际象棋模拟那样只是检测它们是否站在正确的位置。令人尴尬的是,将皇后移回起始位置不会为皇后易位提供机会。)
更体面的 PHP 国际象棋资源
这里有一些基于 php 的国际象棋游戏,我没有时间浏览它们。所有,但应该有很多移动验证可以学习:
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。嗯
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
输入上的正则表达式:
您可以评估表单的 onblur、onchange 和 onsubmit。
我确实同意在客户端和服务器端验证此举也很有意义。
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.