Skip to content

Commit

Permalink
Added fix for #56
Browse files Browse the repository at this point in the history
  • Loading branch information
rudzen committed Feb 10, 2023
1 parent 5696001 commit 824a024
Show file tree
Hide file tree
Showing 8 changed files with 445 additions and 280 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,4 @@ MigrationBackup/

Perft/Logs/
.idea
.sonarqube
106 changes: 106 additions & 0 deletions src/Rudzoft.ChessLib.Test/NotationTests/FanTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
ChessLib, a chess data structure library
MIT License
Copyright (c) 2017-2022 Rudy Alex Kohn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using Rudzoft.ChessLib.Extensions;
using Rudzoft.ChessLib.Factories;
using Rudzoft.ChessLib.Notation;
using Rudzoft.ChessLib.Notation.Notations;
using Rudzoft.ChessLib.Types;

namespace Rudzoft.ChessLib.Test.NotationTests;

public sealed class FanTests
{
[Theory]
[InlineData("8/6k1/8/8/8/8/1K1N1N2/8 w - - 0 1", MoveNotations.Fan, PieceTypes.Knight, Squares.d2, Squares.f2,
Squares.e4)]
public void FanRankAmbiguities(string fen, MoveNotations moveNotation, PieceTypes movingPt, Squares fromSqOne,
Squares fromSqTwo, Squares toSq)
{
var g = GameFactory.Create(fen);
var pc = movingPt.MakePiece(g.Pos.SideToMove);

var fromOne = new Square(fromSqOne);
var fromTwo = new Square(fromSqTwo);
var to = new Square(toSq);

Assert.True(fromOne.IsOk);
Assert.True(fromTwo.IsOk);
Assert.True(to.IsOk);

var pieceChar = pc.GetUnicodeChar();
var toString = to.ToString();

var moveOne = Move.Create(fromOne, to);
var moveTwo = Move.Create(fromTwo, to);

var ambiguity = MoveNotation.Create(g.Pos);

var expectedOne = $"{pieceChar}{fromOne.FileChar}{toString}";
var expectedTwo = $"{pieceChar}{fromTwo.FileChar}{toString}";

var actualOne = ambiguity.ToNotation(moveNotation).Convert(moveOne);
var actualTwo = ambiguity.ToNotation(moveNotation).Convert(moveTwo);

Assert.Equal(expectedOne, actualOne);
Assert.Equal(expectedTwo, actualTwo);
}

[Theory]
[InlineData("8/6k1/8/8/3N4/8/1K1N4/8 w - - 0 1", MoveNotations.Fan, PieceTypes.Knight, Squares.d2, Squares.d4,
Squares.f3)]
public void FanFileAmbiguities(string fen, MoveNotations moveNotation, PieceTypes movingPt, Squares fromSqOne,
Squares fromSqTwo, Squares toSq)
{
var g = GameFactory.Create(fen);
var pc = movingPt.MakePiece(g.Pos.SideToMove);

var fromOne = new Square(fromSqOne);
var fromTwo = new Square(fromSqTwo);
var to = new Square(toSq);

Assert.True(fromOne.IsOk);
Assert.True(fromTwo.IsOk);
Assert.True(to.IsOk);

var pieceChar = pc.GetUnicodeChar();
var toString = to.ToString();

var moveOne = Move.Create(fromOne, to);
var moveTwo = Move.Create(fromTwo, to);

var ambiguity = MoveNotation.Create(g.Pos);

var expectedOne = $"{pieceChar}{fromOne.RankChar}{toString}";
var expectedTwo = $"{pieceChar}{fromTwo.RankChar}{toString}";

var actualOne = ambiguity.ToNotation(moveNotation).Convert(moveOne);
var actualTwo = ambiguity.ToNotation(moveNotation).Convert(moveTwo);

Assert.Equal(expectedOne, actualOne);
Assert.Equal(expectedTwo, actualTwo);
}
}
81 changes: 81 additions & 0 deletions src/Rudzoft.ChessLib.Test/NotationTests/IccfTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
ChessLib, a chess data structure library
MIT License
Copyright (c) 2017-2022 Rudy Alex Kohn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using Rudzoft.ChessLib.Factories;
using Rudzoft.ChessLib.Notation;
using Rudzoft.ChessLib.Notation.Notations;
using Rudzoft.ChessLib.Types;

namespace Rudzoft.ChessLib.Test.NotationTests;

public sealed class IccfTests
{
[Fact]
public void IccfRegularMove()
{
const string fen = "8/6k1/8/8/3N4/8/1K1N4/8 w - - 0 1";
const MoveNotations notation = MoveNotations.ICCF;
const string expectedPrimary = "4263";

var board = new Board();
var pieceValue = new PieceValue();
var pos = new Position(board, pieceValue);
var g = GameFactory.Create(pos);
g.NewGame(fen);

var w1 = Move.Create(Squares.d2, Squares.f3);

var ambiguity = MoveNotation.Create(pos);

var actualPrimary = ambiguity.ToNotation(notation).Convert(w1);

Assert.Equal(expectedPrimary, actualPrimary);
}

[Theory]
[InlineData("8/1P4k1/8/8/8/8/1K6/8 w - - 0 1", PieceTypes.Queen, "27281")]
[InlineData("8/1P4k1/8/8/8/8/1K6/8 w - - 0 1", PieceTypes.Rook, "27282")]
[InlineData("8/1P4k1/8/8/8/8/1K6/8 w - - 0 1", PieceTypes.Bishop, "27283")]
[InlineData("8/1P4k1/8/8/8/8/1K6/8 w - - 0 1", PieceTypes.Knight, "27284")]
public void IccfPromotionMove(string fen, PieceTypes promoPt, string expected)
{
const MoveNotations notation = MoveNotations.ICCF;

var board = new Board();
var pieceValue = new PieceValue();
var pos = new Position(board, pieceValue);
var g = GameFactory.Create(pos);
g.NewGame(fen);

var w1 = Move.Create(Squares.b7, Squares.b8, MoveTypes.Promotion, promoPt);

var ambiguity = MoveNotation.Create(pos);

var actual = ambiguity.ToNotation(notation).Convert(w1);

Assert.Equal(expected, actual);
}
}
Loading

0 comments on commit 824a024

Please sign in to comment.