-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
749 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ChessNotationConverter.Models | ||
{ | ||
internal class Evaluation | ||
{ | ||
internal Position Position { get; private set; } | ||
internal float Score { get; private set; } | ||
internal int Hash { get; } | ||
internal Evaluation(Position position, float score) | ||
{ | ||
Position = position; | ||
Score = score; | ||
Hash = position.Matrix.GetHashCode(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ChessNotationConverter.Models; | ||
|
||
namespace ChessNotationConverter | ||
{ | ||
public static class PositionEvaluator | ||
{ | ||
internal static Evaluation EvaluatePosition(Game game, int positionIndex) | ||
{ | ||
float result; | ||
|
||
// this assumes white won | ||
// using logarithmic function to avoid late-middle game confusion | ||
result = 1 - (float)Math.Log(game.MoveCount + 1 - positionIndex) / (float)Math.Log(game.MoveCount + 1); | ||
|
||
if(game.Outcome == -1) | ||
{ | ||
// negate for black | ||
result = -result; | ||
} | ||
else if (game.Outcome == 0) | ||
{ | ||
// always 0 for draw - assume no difference in eval for white/black | ||
result = 0f; | ||
} | ||
|
||
return new Evaluation(game.Positions[positionIndex], result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using DecisionEngine.Models; | ||
|
||
namespace DecisionEngine.Helpers | ||
{ | ||
public static class KingHelper | ||
{ | ||
private static readonly int[][] offsets = [ | ||
[1, -1], | ||
[1, 0], | ||
[1, 1], | ||
[0, -1], | ||
[0, 1], | ||
[-1, -1], | ||
[-1, 0], | ||
[-1, 1] | ||
]; | ||
|
||
public static List<int[,]> FindAllLegalMoves(int[,] board, Player player) | ||
{ | ||
var result = new List<int[,]>(); | ||
|
||
var kingValue = player == Player.White ? 9 : -9; | ||
|
||
for (var i = 0; i < 8; i++) | ||
{ | ||
for (var j = 0; j < 8; j++) | ||
{ | ||
if (board[i, j] != kingValue) | ||
continue; | ||
|
||
var potentialSquares = GetPotentialMoveSquares(board, new Square(i, j)); | ||
|
||
foreach (var square in potentialSquares) | ||
{ | ||
var playerCondition = player == Player.White | ||
? BoardHelper.IntValueAt(board, (int)square.Row, (int)square.Column) <= 0 | ||
: BoardHelper.IntValueAt(board, (int)square.Row, (int)square.Column) >= 0; | ||
|
||
if (playerCondition) | ||
{ | ||
var newBoard = BoardHelper.DeepCopy(board); | ||
newBoard[i, j] = 0; | ||
newBoard[(int)square.Row, (int)square.Column] = kingValue; | ||
result.Add(newBoard); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static List<Square> GetPotentialMoveSquares(int[,] board, Square square) | ||
{ | ||
var result = new List<Square>(); | ||
|
||
foreach (var offset in offsets) | ||
{ | ||
var proposedRow = (int)square.Row + offset[0]; | ||
var proposedColumn = (int)square.Column + offset[1]; | ||
|
||
if(BoardHelper.AreValidCoords(proposedRow, proposedColumn)) | ||
{ | ||
result.Add(new Square(proposedRow, proposedColumn)); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
Oops, something went wrong.