Skip to content

Commit 55a907c

Browse files
committed
Did a little code review and cleaned up and fixed a few things here and there. Nothing major.
1 parent be7d687 commit 55a907c

File tree

15 files changed

+28
-77
lines changed

15 files changed

+28
-77
lines changed

MinimalChess/Board.cs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,12 @@ private set
104104

105105
//Rank - the eight horizontal rows of the chess board are called ranks.
106106
//File - the eight vertical columns of the chess board are called files.
107-
public Piece this[int rank, int file]
108-
{
109-
get => _state[rank * 8 + file];
110-
}
107+
public Piece this[int rank, int file] => _state[rank * 8 + file];
111108

112109
public void SetupPosition(string fen)
113110
{
114111
//Startpos in FEN looks like this: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
115-
https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
112+
//https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
116113
string[] fields = fen.Split();
117114
if (fields.Length < 4)
118115
throw new ArgumentException($"FEN needs at least 4 fields. Has only {fields.Length} fields.");
@@ -645,36 +642,6 @@ private void SetCastlingRights(CastlingRights flag, bool state)
645642
_zobristHash ^= Zobrist.Castling(_castlingRights);
646643
}
647644

648-
public override bool Equals(object obj)
649-
{
650-
if (obj is Board board)
651-
return Equals(board);
652-
653-
return false;
654-
}
655-
656-
public bool Equals(Board other)
657-
{
658-
//Used for detecting repetition
659-
//From: https://en.wikipedia.org/wiki/Threefold_repetition
660-
//Two positions are by definition "the same" if...
661-
//1.) the same player has the move
662-
if (other._sideToMove != _sideToMove)
663-
return false;
664-
//2.) the remaining castling rights are the same and
665-
if (other._castlingRights != _castlingRights)
666-
return false;
667-
//3.) the possibility to capture en passant is the same.
668-
if (other._enPassantSquare != _enPassantSquare)
669-
return false;
670-
//4.) the same types of pieces occupy the same squares
671-
for (int square = 0; square < 64; square++)
672-
if (other._state[square] != _state[square])
673-
return false;
674-
675-
return true;
676-
}
677-
678645
private void InitZobristHash()
679646
{
680647
//Side to move

MinimalChess/Evaluation.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ public static double Linstep(double edge0, double edge1, double value)
220220
0, 30, 3, 12, 5, -99, 0, 6, 4, 7, -8, 6, 0, // K
221221
};
222222

223-
public static int DynamicScore;
224-
225223
public static int ComputeMobility(Board board)
226224
{
227225
int mobility = 0;

MinimalChess/History.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace MinimalChess
1+
namespace MinimalChess
82
{
93
public class History
104
{
@@ -45,7 +39,7 @@ public float Value(Board context, Move move)
4539
int iPiece = PieceIndex(context[move.FromSquare]);
4640
float a = Positive[move.ToSquare, iPiece];
4741
float b = Negative[move.ToSquare, iPiece];
48-
return a / (a + b + 1);//Interval [0..1]
42+
return a / (a + b + 1);//ratio of good increments in the range of [0..1]
4943
}
5044
}
5145
}

MinimalChess/IterativeSearch.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class IterativeSearch
2020
private History _history;
2121
private KillSwitch _killSwitch;
2222
private long _maxNodes;
23+
private int _mobilityBonus;
2324

2425
public IterativeSearch(Board board, long maxNodes = long.MaxValue)
2526
{
@@ -38,7 +39,7 @@ public IterativeSearch(int searchDepth, Board board) : this(board)
3839
public void SearchDeeper(Func<bool> killSwitch = null)
3940
{
4041
Depth++;
41-
_killers.Resize(Depth);
42+
_killers.Expand(Depth);
4243
_history.Scale();
4344
StorePVinTT(PrincipalVariation, Depth);
4445
_killSwitch = new KillSwitch(killSwitch);
@@ -69,7 +70,7 @@ private void StorePVinTT(Move[] pv, int depth)
6970
{
7071
if (depth <= 0)
7172
{
72-
Evaluation.DynamicScore = Evaluation.ComputeMobility(position);
73+
_mobilityBonus = Evaluation.ComputeMobility(position);
7374
return (QEval(position, window), Array.Empty<Move>());
7475
}
7576

@@ -103,7 +104,7 @@ private void StorePVinTT(Move[] pv, int depth)
103104
//moves after the PV node are unlikely to raise alpha. try to avoid a full evaluation!
104105
if(expandedNodes > 1)
105106
{
106-
bool tactical = isChecked || (move.Promotion > Piece.Queen) || child.IsChecked(child.SideToMove);
107+
bool tactical = isChecked || child.IsChecked(child.SideToMove);
107108

108109
//some moves are hopeless and can be skipped without deeper evaluation
109110
if (depth <= 4 && !tactical)
@@ -176,7 +177,7 @@ private int QEval(Board position, SearchWindow window)
176177
//if inCheck we can't use standPat, need to escape check!
177178
if (!inCheck)
178179
{
179-
int standPatScore = Evaluation.DynamicScore + position.Score;
180+
int standPatScore = position.Score + _mobilityBonus;
180181
//Cut will raise alpha and perform beta cutoff when standPatScore is too good
181182
if (window.Cut(standPatScore, color))
182183
return window.GetScore(color);

MinimalChess/KillSwitch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MinimalChess
44
{
5-
public struct KillSwitch
5+
struct KillSwitch
66
{
77
Func<bool> _killSwitch;
88
bool _aborted;

MinimalChess/KillerMoves.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class KillerMoves
1010

1111
public KillerMoves(int width)
1212
{
13-
_moves = new Move[0];
13+
_moves = Array.Empty<Move>();
1414
_depth = 0;
1515
_width = width;
1616
}
1717

18-
public void Resize(int depth)
18+
public void Expand(int depth)
1919
{
2020
_depth = Math.Max(_depth, depth);
2121
Array.Resize(ref _moves, _depth * _width);

MinimalChess/Move.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Move(string uciMoveNotation)
3030
throw new ArgumentException($"Long algebraic notation expected. '{uciMoveNotation}' is too long!");
3131

3232
//expected format is the long algebraic notation without piece names
33-
https://en.wikipedia.org/wiki/Algebraic_notation_(chess)
33+
//https://en.wikipedia.org/wiki/Algebraic_notation_(chess)
3434
//Examples: e2e4, e7e5, e1g1(white short castling), e7e8q(for promotion)
3535
string fromSquare = uciMoveNotation.Substring(0, 2);
3636
string toSquare = uciMoveNotation.Substring(2, 2);

MinimalChess/Notation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static string ToSquareName(byte squareIndex)
5858
public static byte ToSquare(string squareNotation)
5959
{
6060
//Each square has a unique identification of file letter followed by rank number.
61-
https://en.wikipedia.org/wiki/Algebraic_notation_(chess)
61+
//https://en.wikipedia.org/wiki/Algebraic_notation_(chess)
6262
//Examples: White's king starts the game on square e1; Black's knight on b8 can move to open squares a6 or c6.
6363

6464
//Map letters [a..h] to [0..7] with ASCII('a') == 97

MinimalChess/Playmaker.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
1+
using System.Collections.Generic;
42

53
namespace MinimalChess
64
{

MinimalChessBoard/AlgebraicNotation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static Move ToMove(Board board, string notation)
7777
throw new ArgumentException($"Move notation {notation} could not be parsed!");
7878
}
7979

80-
private static Move SelectMove(Board board, Piece moving, int toSquare, Piece promotion, char? disambiguate = null)
80+
private static Move SelectMove(Board board, Piece moving, int toSquare, Piece promotion, char? fileOrRank = null)
8181
{
8282
foreach (var move in new LegalMoves(board))
8383
{
@@ -87,7 +87,7 @@ private static Move SelectMove(Board board, Piece moving, int toSquare, Piece pr
8787
continue;
8888
if (Pieces.Type(board[move.FromSquare]) != Pieces.Type(moving))
8989
continue;
90-
if (disambiguate is char fileOrRank && !Notation.ToSquareName(move.FromSquare).Contains(fileOrRank))
90+
if (fileOrRank != null && !Notation.ToSquareName(move.FromSquare).Contains(fileOrRank.Value))
9191
continue;
9292

9393
return move; //this is the move!

0 commit comments

Comments
 (0)