TODO in fen parser

This commit is contained in:
Alf Magne Kalleland
2013-02-12 02:20:28 +01:00
parent f00ceee0bb
commit 3d6e0ae60b
4 changed files with 196 additions and 15 deletions

View File

@@ -838,11 +838,10 @@ class FenParser0x88
function getFromAndToByNotation($notation)
{
$notation = str_replace(".","", $notation);
$notation = str_replace(".", "", $notation);
$ret = array('promoteTo' => $this->getPromoteByNotation($notation));
$color = $this->getColor();
$offset = 0;
if ($color === 'black') {
$offset = 112;
@@ -956,6 +955,7 @@ class FenParser0x88
}
}
}
if (!isset($ret['from'])) {
$config = $this->getValidMovesAndResult();
$moves = $config['moves'];
@@ -967,8 +967,17 @@ class FenParser0x88
}
}
}
// TODO some pgn files may not have correct notations for all moves. Example Nd7 which may be from b2 or f6.
// this may cause problems later on in the game. Figure out a way to handle this.
#if (count($foundPieces) === 2){
#$ret['from'] = $foundPieces[1];
#throw new Exception("Unable to decide which move to take for notation: ". $notation);
#}
if(!isset($ret['from'])){
$msg = "Fen: ".$this->fen ."\ncolor: ". $color. "\nnotation: ". $notation."\nRank:". $fromRank. "\nFile:". $fromFile."\n". count($foundPieces).", ". implode(",", $foundPieces);
throw new Exception($msg);
}

View File

@@ -6,10 +6,10 @@ class GameParser
private $game;
private $fen;
private $fenParser0x88;
public function __construct()
{
$this->moveParser = new FenParser0x88();
$this->fenParser0x88 = new FenParser0x88();
}
public function getParsedGame($game)
@@ -17,7 +17,7 @@ class GameParser
$this->game = $game;
$this->fen = $this->getStartFen();
$this->moveParser->newGame($this->fen);
$this->fenParser0x88->newGame($this->fen);
$this->parseMoves($this->game[CHESS_JSON::MOVE_MOVES]);
$this->addParsedProperty();
return $this->game;
@@ -32,8 +32,6 @@ class GameParser
{
foreach ($moves as &$move) {
$this->parseAMove($move);
}
}
@@ -44,21 +42,20 @@ class GameParser
}
if (strlen($move[CHESS_JSON::MOVE_NOTATION]) < 2) return;
if (isset($move[CHESS_JSON::MOVE_VARIATIONS])) {
$fen = $this->moveParser->getFen();
$fen = $this->fenParser0x88->getFen();
$this->parseVariations($move[CHESS_JSON::MOVE_VARIATIONS]);
$this->moveParser->setFen($fen);
$this->fenParser0x88->setFen($fen);
}
$move = $this->moveParser->getParsed($move);
$move = $this->fenParser0x88->getParsed($move);
}
private function parseVariations(&$variations)
{
foreach ($variations as &$variation) {
$fen = $this->moveParser->getFen();
$fen = $this->fenParser0x88->getFen();
$this->parseMoves($variation);
$this->moveParser->setFen($fen);
$this->fenParser0x88->setFen($fen);
}
}

View File

@@ -31,6 +31,7 @@ class PgnParser
{
$c = $this->pgnContent;
$c = preg_replace("/\\$[0-9]+/s", "", $c);
$c = str_replace("({", "( {", $c);
$c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c);
$c = preg_replace("/\r/s", "", $c);
$c = preg_replace("/\t/s", "", $c);

View File

@@ -55,7 +55,6 @@
33. Qc4 Nxg3+ 34. hxg3
{White is now winning easily.}
34...Qd8 35. Qc1 Rb3 36. Kg2 Qf8 37. Qc7
1-0
[Event "?"]
[White "Capablanca"]
@@ -115,4 +114,179 @@
25. Bf3
25... f4 {Desperation.}
26. exf4 {Black resigned. If 26... Qxf4 27. Qxf4 Rxf4 28. Rxb6.}
1-0
[Event "?"]
[Site "Hastings"]
[Date "1895"]
[White "Pillsbury"]
[Black "Tarasch"]
[Result "1-0"]
1. d4 d5
2. c4 e6
3. Nc3 Nf6
4. Bg5 {It was Pillsbury who first demonstrated the
strength of the this move, which today is routine}
4..Be7
5. Nf3 Nd7
6. Rc1 O-O
7. e3 b6 {In order to develop the Queen Bishop on
Bb7. This was the most popular way of defending the
Queen's gambit declined at the time.}
8. cxd5 {Depriving Black of the opportunity to play
dxc4 when the diagonal b7-g2 would be open for his
Queen Bishop.} 8...exd5 ({The classical
continuation more common today is} 8..Nf6xd5 {which
accomplishes some exchanging after} 9. Bxe7, Qxe7 10.
Nxd5, e6xd5)
9. Bd3 Bb7
10. O-O c5 {A strategical necessity. Otherwise, this
pawn will remain backward and vulnerable to White's
Rook on the half open Queen Bishop file.}
11. Re1 {Whatever the purpose of this move, it turns
out to be a loss of time, for the Rook later goes
back to f1.}
11...c4 {This move releases the
tension in the center in order to commence a queen
side attack. White will counter this by an attack on
the other wing.}
12. Bb1 a6 {In order to play b5. Black's plan is to
advance his queen side pawns with the ultimate aim of
obtaining a passed pawn.}
13. Ne5 {The knight is here aggressively posted in the
neighborhood of Black's King.} 13...b5
14. f4 {This move furthers White's attack in several
ways. Should Black ever play Nxe5, White will
recapture with the Bishop Pawn and open his King
Bishop file. The White's King Bishop Pawn may later
in the game advance to f5, threatening to break up
Black's kingside by f6. White's King Rook now can
advance to Black's kingside by Rf1-Rf3 and Rg3 or
Rh3.} 14...Re8 {To be able to bring his Queen Knight
to Nf8, where it is an excellent defensive piece.}
15. Qf3 {Bringing the Queen to the attack.} 15...Nf8
16. Ne2 {Transferring the Knight to the kingside.}
16...Ne4 $1 {Black blocks the White King Bishop
diagonal and exchanges off his King Bishop. Each
exchange favors Black, for they lessen the vigor of a
kingside attack. Queen side attacks, by contrast,
are concerned with obtaining a passed pawn, which is
even more advantageous in the end game then the
middle game.}
17. Bxe7 {Black was threatening to win a piece with
f6} 17...Rxe7
18. Bxe4 {White is not happy to give up his King
Bishop, but there is no way to drive away Black's
Knight at e4, and as long as it remains there it is
more effective than the Bishop, whose diagonal itblocks.}
18...dxe4
19. Qg3 {Black is not the only one who has gained
something from these exchanges. Now that Black's
Queen Bishop Pawn no longer has the support of a
Queen Pawn, he is much less free to advance his queen
side pawns, and his attack on the queen side is thus
slowed. White's backward King Pawn, which previously
was indirectly under pressure by Black's King Rook,
now has shelter behind Black's Pawn at the latter
e4.} 19...f6 {This slightly weakens Black's
kingside, but it is worth it to prevent White's King
Bishop Pawn from ever advancing to f6.}
20. Ng4 {Threatening 21. Nxf6+.} 20...Kh8
21. f5 {Cramping Black's kingside and vacating his f4
which can now be occupied by a Rook or a Knight.}
21...Qd7
22. Rf1 {See note at move 11.} 22...Rd8 {Preparing
Qd6 to defend his f6 pawn a second time.}
23. Rf4 {White is embarking upon a plan to attack
Black's isolated King Pawn , and tie up Black's
pieces to its defense.} 23...Qd6
24. Qh4 Rde8
25. Nc3 Bd5 {To be able to guard the King Pawn again
by Qc6.}
26. Nf2 Qc6
27. Rf1 {White must be careful now about removing any
pieces from the queen side, for Black can play b4,
followed by Qa4, menacing White's queen side pawns.}
27...b4
28. Ne2 Qa4 ({If Black tries to advance his c Pawn with} 28..c3 {there follows} 29. bxc3, bxc3 30. Nd1, c2 31. Ndc3, Bc4 32. d5, Bxd5 33. Rc1
{and Black loses his Queen Bishop Pawn.})
29 Ng4 Nd7 (29...Qxa2? 30. Nxf6 g7xf6 31.
Qxf6+ Kg8 (31..Rg7 32. Rg4) 32. Rg4+ {winning} )
30. R4f2 $1 {Defending the Queen Rook Pawn by a
clever combination.} Kg8
(30..Qxa2 31. Nf4, Bf7
32. Ng6 $1, Bxg6 33. f5xg6 h6 (33..Nf8 34. Nxf6, g7xf6 35.Rxf6, Kg8 36. Rf7
{forces mate.}) 34.Nxh6, g7xh6 35. Qxh6+, Kg8 36. Rf5 $1 { and black is
defenseless against 37. Rh5 and Qh8} )
31. Nc1 {Guarding his Queen Rook Pawn. If he had done
this on move
twenty-nine, Black would have had the crushing reply
Qc7.} 31...c3
{Black gets a passed Pawn.}
32. b3 Qc6 {Both sides now have clear cut plans.
Black will advance his
Queen Rook Pawn to a4, exchange pawns and bring a
Rook to a3 winning White's Queen Knight Pawn. White
will counter by advancing his King Knight Pawn to g5
and opening up his King Knight file.}
33. h3 {Making room for the Knight at h2.} 33...a5
34. Nh2 {White's attack looks slower than Blacks, but
White has a
stroke of genius prepared.} 34...a4
35. g4 axb3
36. axb3 Ra8 {With hindsight, the defensive 36..h6
might have been
better.}
37. g5
37..Ra3 (37...fxg5 38. Qxg5 Nf6 (38...Qf6 39. Qg3 -- 40. Ng4 ) 39. Ng4 {taking advantage
of black's pinned Knight, Followed by 40. Ne5, when the Knight will be devastatingly
powerful.} )
38. Ng4 Bb3 ({At this moment neither Tarasch nor the
onlookers had any doubt that white was finished. In
the February 1971 issue of Chess Review, Frank
Rhoden relates that Mr. E.G. Taylor, a Hastings chess
club member who actually witnessed the game, told him
that after Tarasch made his 38th move, "The
spectators began to drift away, thinking there was
nothing more to see." But now comes one of the most
dramatic surprises ever seen on a chessboard. With
hindsight, several annotators have advocated that
black play 38.. Rxb3, which sacrifices the exchange
for a pawn. Black would then have his Bishop
available for the defense and obtain two dangerous
connected passed pawns. But 38...Rxb3 is no better
than the move played.}
38..Rxb3 39.Nxb3, Bxb3 40. Rg2, Kh8 41. g5xf6, g7xf6 42.
Ne5 $1, Nxe5 43.dxe5 c2 (43..Rxe5 44. Qh6
{ threatening mate at both g7 and f8 wins.}) 44. e6
{and black is finished. If then} 44.. Qc3 45. Qh7
{These variations were given by Horowitz and Reinfeld
in their revision of R.N. Cole's book, Battles Royal of the Chessboard.
})
39. Rg2 {Threatening to win a piece with 40. gxf6,
Nxf6 41. Nxf6+}
39...Kh8
40. gxf6 gxf6 {If 40...Nxf6 41. Ne5 followed by
42. Ng6+}
41. Nxb3 Rxb3
42. Nh6 Rg7 {White threatened 43. Rg8#, and if
42...Re8, 43. Nf7#}
43. Rxg7 Kxg7
44. Qg3 $3 {The move that turns the
tables....If 44..Kf8 45. Qg8+ 46. Qxb3} Kxh6
45. Kh1 $1 {Threatening 46. Rg1 and 47. Qh4#. The only
was for Black to prevent this is to play as he does.}
45...Qd5
46. Rg1 Qxf5
47. Qh4+ Qh5
48. Qf4+ Qg5
49. Rxg5 fxg5
50. Qd6+ Kh5
51. Qxd7 c2
({A blunder, but Black was lost. If}
51..Rb1+ ( 51..Kg6 52. Qe6+ ) 52. Kg2, Rb2+ 53. Kg3, Kg6 54. Qc6+ Kf5 55. d5 {wins
easily.})
52. Qxh7#
1-0
1-0