A Training Tool for Chess Players Based on Memory Recall
Repository
https://github.com/programarivm/pgn-chess
Hi dear readers!
As you most probably know already, last week I started to send a series of pull requests to PGN Chess. At this stage, this PHP library comes out of the box with a few CLI commands, among which is db-seed.php
to easily load thousands of PGN games into a database for further processing.
Remember: PGN Chess is a chess board representation to play and validate PGN games (player vs player) which also provides with a PHP CLI command to seed a database with PGN games.
Technology Stack
- PHP 7
- PDO
- MySQL
New Features
The fact of having a chess database powered by SQL opens the door to a whole new range of possibilities.
Let's see how we can do this.
Today, it is the turn of Feature/game metadata #15.
This one helps players learn about chess openings, grandmasters' games, events, as well as to improve their memory recall, all at once while they are having fun playing a very interesting chess game.
A bit more specifically, I am referring to the metadata()
method, and I'd even say it is a training tool for chess players actually.
Here is an example of the new method in action:
$game = new Game;
$game->play('w', 'd4');
$game->play('b', 'd5');
$game->play('w', 'Bf4');
$metadata = $game->metadata();
Now, $metadata
might be an array as the described below.
Array
(
[Event] => 11. KIIT Elite Open 2018
[Site] => Bhubaneswar IND
[Date] => 2018.05.28
[Round] => 6.5
[White] => Kravtsiv, Martyn
[Black] => Das, Sayantan
[Result] => 1-0
[WhiteElo] => 2655
[BlackElo] => 2437
[EventDate] => 2018.05.25
[ECO] => D02
[movetext] => 1.d4 d5 2.Bf4 Nf6 3.Nf3 c5 4.e3 Nc6 5.Nbd2 e6 6.c3 Bd6 7.Bg3 O-O 8.Bd3 b6 9.e4 dxe4 10.Nxe4 Be7 11.Nxf6+ Bxf6 12.dxc5 bxc5 13.Qc2 h6 14.h4 Qe7 15.O-O-O Rd8 16.Bh7+ Kh8 17.Rxd8+ Nxd8 18.Be4 Bb7 19.Rd1 Rc8 20.Qa4 Bxe4 21.Qxe4 Nc6 22.Bd6 Qe8 23.Bxc5 Na5 24.Qb4 Nc6 25.Qa4 e5 26.Qe4 Na5 27.Rd5 Qb5 28.Bb4 Qf1+ 29.Rd1 Qb5 30.Bxa5 Qxa5 31.a3 Qb5 32.g3 Rc4 33.Qd5 Qb3 34.Nxe5 Rxc3+ 35.bxc3 Qxc3+ 36.Kb1 Bxe5 37.Qd8+ Kh7 38.Qd3+ Qxd3+ 39.Rxd3 Kg6 40.Kc2 Bc7 41.Rd5 Bb6 42.f3 h5 43.Kd3 f6 44.Ke2 Bc7 45.g4 hxg4 46.fxg4 Bb6 47.a4 1-0
)
But it could be this one too.
Array
(
[Event] => 1. Longtou Cup 2018
[Site] => Qinhuangdao CHN
[Date] => 2018.05.28
[Round] => 3.2
[White] => Antipov, Mikhail Al
[Black] => Dai, Changren
[Result] => 1/2-1/2
[WhiteElo] => 2597
[BlackElo] => 2436
[EventDate] => 2018.05.26
[ECO] => D00
[movetext] => 1.d4 d5 2.Bf4 Nf6 3.e3 e6 4.Nd2 c5 5.c3 Nc6 6.Ngf3 Bd6 7.Bg3 O-O 8.Bb5 h6 9.Qe2 Bxg3 10.hxg3 Qb6 11.Rb1 Bd7 12.Bd3 Ng4 13.Nh4 f5 14.f3 Nf6 15.Ng6 Rfe8 16.f4 Kf7 17.Ne5+ Nxe5 18.fxe5 Ng4 19.Nf3 Qd8 20.Nh2 Nxh2 21.Rxh2 Qg5 22.Kf2 Kg8 23.Rh5 Qg6 24.dxc5 Rec8 25.Rbh1 Rxc5 26.R1h4 Rf8 27.g4 fxg4+ 28.Kg1 Bb5 29.Bxb5 Rxb5 30.Rxh6 Qb1+ 31.Kh2 1/2-1/2
)
Yep, $metadata
is randomish. It can be anything starting with d4
, d5
and Bf4
, and it will vary in subsequent calls according to the chess games stored in a particular database.
Are you curious on what's under the hood? The idea is really simple, let me show you in a nutshell the code that makes it happen.
// src/Board.php
...
$result = Pdo::getInstance()
->query("SELECT * FROM games WHERE movetext LIKE '$movetext%' ORDER BY RAND() LIMIT 1")
->fetch(\PDO::FETCH_ASSOC);
...
Easy peasy lemon squeezy.
In my opinion, the relevant difficulty of this exercise consists in making sure that all movetexts are written using the exact same format for the SQL query to work properly.
And here is where a movetext filter in src/PGN/Movetext.php
comes to the rescue in search for unnecessary blank spaces:
// src/PGN/Movetext.php
...
/**
* Filters a movetext.
*
* Example:
*
* 1.e4 e5 2. f4 exf4 3. Bc4 d5 4.Bxd5 Qh4+
*
* is transformed into:
*
* 1.e4 e5 2.f4 exf4 3.Bc4 d5 4.Bxd5 Qh4+
*
* @return string
*/
public static function filter(): string
{
$text = '';
for ($i = 0; $i < count(self::$movetext->numbers); $i++) {
$text .= self::$movetext->numbers[$i] . '.' .
self::$movetext->notations[$i*2] . ' ' .
self::$movetext->notations[$i*2+1] . ' ';
}
return trim($text);
}
...
That's all for now. I hope you liked today’s post. Thank you for reading and sharing your thoughts.
Documentation
For further information please read the Documentation.
License
The GNU General Public License.
Contributions
Would you help make this library better? Contributions are welcome.
- Feel free to send a pull request
- Drop an email at [email protected] with the subject "PGN Chess Contributions"
- Leave me a comment on Twitter
- Say hello on Google+
Thank you for your contribution. I really liked the way you have written everything in the pull requests. As basically it is a CLI tool, is there any way that frontend can call it and get the representation to be shown to the user?
Also, there are a lot of .pgn files are removed from the GitHub. It's better to write those files in the .gitignore.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @codingdefined!
So far this week you've reviewed 3 contributions. Keep up the good work!
Hey @programarivm
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Hi @programarivm! We are @steem-ua, a new Steem dApp, computing UserAuthority for all accounts on Steem. We are currently in test mode upvoting quality Utopian-io contributions! Nice work!
Congratulations @programarivm! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :
Award for the total payout received
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP