Skip to content

Commit 5d49dc6

Browse files
committed
Some semanticly equivalent refactorings
1 parent b7942c1 commit 5d49dc6

File tree

6 files changed

+60
-60
lines changed

6 files changed

+60
-60
lines changed

MinimalChess/Board.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ private void AddCaptures(int square, Action<Move> moveHandler)
409409

410410
public bool IsChecked(Color color)
411411
{
412-
Piece king = color == Color.Black ? Piece.BlackKing : Piece.WhiteKing;
412+
Piece king = Piece.King.OfColor(color);
413413
for (int square = 0; square < 64; square++)
414414
if(_state[square] == king)
415415
return IsSquareAttackedBy(square, Pieces.Flip(color));
@@ -583,14 +583,14 @@ private void AddBlackPawnMoves(Action<Move> moveHandler, int square)
583583
private void AddWhitePawnAttacks(Action<Move> moveHandler, int square)
584584
{
585585
foreach (int target in Attacks.WhitePawn[square])
586-
if (_state[target].Color() == Color.Black || target == _enPassantSquare)
586+
if (_state[target].IsBlack() || target == _enPassantSquare)
587587
AddWhitePawnMove(moveHandler, square, target);
588588
}
589589

590590
private void AddBlackPawnAttacks(Action<Move> moveHandler, int square)
591591
{
592592
foreach (int target in Attacks.BlackPawn[square])
593-
if (_state[target].Color() == Color.White || target == _enPassantSquare)
593+
if (_state[target].IsWhite() || target == _enPassantSquare)
594594
AddBlackPawnMove(moveHandler, square, target);
595595
}
596596

MinimalChess/Evaluation.cs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,38 @@ public void Update(Piece oldPiece, Piece newPiece, int index)
3737

3838
private void AddPiece(Piece piece, int squareIndex)
3939
{
40-
short color = (short)piece.Color();
41-
int pieceIndex = PieceTableIndex(piece);
42-
int tableIndex = SquareTableIndex(squareIndex, piece);
40+
int pieceIndex = PieceIndex(piece);
4341
Phase += PhaseValues[pieceIndex];
44-
MidgameScore += (short)(color * MidgameTables[pieceIndex, tableIndex]);
45-
EndgameScore += (short)(color * EndgameTables[pieceIndex, tableIndex]);
42+
43+
if (piece.IsWhite())
44+
AddScore(pieceIndex, squareIndex ^ 56);
45+
else
46+
SubtractScore(pieceIndex, squareIndex);
4647
}
4748

4849
private void RemovePiece(Piece piece, int squareIndex)
4950
{
50-
int color = (int)piece.Color();
51-
int pieceIndex = PieceTableIndex(piece);
52-
int tableIndex = SquareTableIndex(squareIndex, piece);
51+
int pieceIndex = PieceIndex(piece);
5352
Phase -= PhaseValues[pieceIndex];
54-
MidgameScore -= (short)(color * MidgameTables[pieceIndex, tableIndex]);
55-
EndgameScore -= (short)(color * EndgameTables[pieceIndex, tableIndex]);
53+
54+
if (piece.IsWhite())
55+
SubtractScore(pieceIndex, squareIndex ^ 56);
56+
else
57+
AddScore(pieceIndex, squareIndex);
58+
}
59+
60+
private void AddScore(int pieceIndex, int squareIndex)
61+
{
62+
int tableIndex = (pieceIndex << 6) | squareIndex;
63+
MidgameScore += MidgameTables[tableIndex];
64+
EndgameScore += EndgameTables[tableIndex];
65+
}
66+
67+
private void SubtractScore(int pieceIndex, int squareIndex)
68+
{
69+
int tableIndex = (pieceIndex << 6) | squareIndex;
70+
MidgameScore -= MidgameTables[tableIndex];
71+
EndgameScore -= EndgameTables[tableIndex];
5672
}
5773
}
5874

@@ -75,10 +91,8 @@ public static double Linstep(double edge0, double edge1, double value)
7591
return Math.Min(1, Math.Max(0, (value - edge0) / (edge1 - edge0)));
7692
}
7793

78-
private static int PieceTableIndex(Piece piece) => ((int)piece >> 2) - 1;
94+
private static int PieceIndex(Piece piece) => ((int)piece >> 2) - 1;
7995

80-
//a black piece has 2nd bit set, white has not. Square ^ 56 flips the file, so that tables work for black
81-
private static int SquareTableIndex(int square, Piece piece) => square ^ (28 * ((int)piece & 2));
8296

8397
/*
8498
Added dynamic mobility component to 'chillipepper003' from 'mobility13_7e', the same values as in 0.4.3d which have proven best in tests despite a rather high MSE
@@ -91,8 +105,8 @@ public static double Linstep(double edge0, double edge1, double value)
91105
//Pawn = 0, Knight = 155, Bishop = 305; Rook = 405, Queen = 1050, King = 0
92106
static readonly short[] PhaseValues = new short[6] { 0, 155, 305, 405, 1050, 0 };
93107

94-
static readonly short[,] MidgameTables = new short[6, 64]{
95-
{ //PAWN MG
108+
static readonly short[] MidgameTables = new short[6 * 64]
109+
{ //PAWN MG
96110
100, 100, 100, 100, 100, 100, 100, 100,
97111
176, 214, 147, 194, 189, 214, 132, 77,
98112
82, 88, 106, 113, 150, 146, 110, 73,
@@ -101,8 +115,7 @@ public static double Linstep(double edge0, double edge1, double value)
101115
55, 70, 68, 69, 76, 81, 101, 66,
102116
52, 84, 66, 60, 69, 99, 117, 60,
103117
100, 100, 100, 100, 100, 100, 100, 100,
104-
},
105-
{ //KNIGHT MG
118+
//KNIGHT MG
106119
116, 228, 271, 270, 338, 213, 278, 191,
107120
225, 247, 353, 331, 321, 360, 300, 281,
108121
258, 354, 343, 362, 389, 428, 375, 347,
@@ -111,8 +124,7 @@ public static double Linstep(double edge0, double edge1, double value)
111124
287, 297, 316, 319, 327, 320, 327, 294,
112125
276, 259, 300, 304, 308, 322, 296, 292,
113126
208, 290, 257, 274, 296, 284, 293, 284,
114-
},
115-
{ //BISHOP MG
127+
//BISHOP MG
116128
292, 338, 254, 283, 299, 294, 337, 323,
117129
316, 342, 319, 319, 360, 385, 343, 295,
118130
342, 377, 373, 374, 368, 392, 385, 363,
@@ -121,8 +133,7 @@ public static double Linstep(double edge0, double edge1, double value)
121133
335, 350, 351, 347, 352, 361, 350, 344,
122134
333, 354, 354, 339, 344, 353, 367, 333,
123135
309, 341, 342, 325, 334, 332, 302, 313,
124-
},
125-
{ //ROOK MG
136+
//ROOK MG
126137
493, 511, 487, 515, 514, 483, 485, 495,
127138
493, 498, 529, 534, 546, 544, 483, 508,
128139
465, 490, 499, 497, 483, 519, 531, 480,
@@ -131,8 +142,7 @@ public static double Linstep(double edge0, double edge1, double value)
131142
441, 461, 468, 465, 478, 481, 478, 452,
132143
443, 472, 467, 476, 483, 500, 487, 423,
133144
459, 463, 470, 479, 480, 480, 446, 458,
134-
},
135-
{ //QUEEN MG
145+
//QUEEN MG
136146
865, 902, 922, 911, 964, 948, 933, 928,
137147
886, 865, 903, 921, 888, 951, 923, 940,
138148
902, 901, 907, 919, 936, 978, 965, 966,
@@ -141,8 +151,7 @@ public static double Linstep(double edge0, double edge1, double value)
141151
895, 916, 900, 902, 904, 912, 924, 917,
142152
874, 899, 918, 908, 915, 924, 911, 906,
143153
906, 899, 906, 918, 898, 890, 878, 858,
144-
},
145-
{ //KING MG
154+
//KING MG
146155
-11, 70, 55, 31, -37, -16, 22, 22,
147156
37, 24, 25, 36, 16, 8, -12, -31,
148157
33, 26, 42, 11, 11, 40, 35, -2,
@@ -151,11 +160,10 @@ public static double Linstep(double edge0, double edge1, double value)
151160
7, -2, -37, -77, -79, -60, -23, -26,
152161
12, 15, -13, -72, -56, -28, 15, 17,
153162
-6, 44, 29, -58, 8, -25, 34, 28,
154-
}
155163
};
156164

157-
static readonly short[,] EndgameTables = new short[6, 64]{
158-
{ //PAWN EG
165+
static readonly short[] EndgameTables = new short[6 * 64]
166+
{ //PAWN EG
159167
100, 100, 100, 100, 100, 100, 100, 100,
160168
277, 270, 252, 229, 240, 233, 264, 285,
161169
190, 197, 182, 168, 155, 150, 180, 181,
@@ -164,8 +172,7 @@ public static double Linstep(double edge0, double edge1, double value)
164172
96, 96, 85, 92, 88, 83, 85, 82,
165173
107, 99, 97, 97, 100, 89, 89, 84,
166174
100, 100, 100, 100, 100, 100, 100, 100,
167-
},
168-
{ //KNIGHT EG
175+
//KNIGHT EG
169176
229, 236, 269, 250, 257, 249, 219, 188,
170177
252, 274, 263, 281, 273, 258, 260, 229,
171178
253, 264, 290, 289, 278, 275, 263, 243,
@@ -174,8 +181,7 @@ public static double Linstep(double edge0, double edge1, double value)
174181
258, 276, 278, 290, 287, 274, 260, 255,
175182
241, 259, 270, 277, 276, 262, 260, 237,
176183
253, 233, 258, 264, 261, 260, 234, 215,
177-
},
178-
{ //BISHOP EG
184+
//BISHOP EG
179185
288, 278, 287, 292, 293, 290, 287, 277,
180186
289, 294, 301, 288, 296, 289, 294, 281,
181187
292, 289, 296, 292, 296, 300, 296, 293,
@@ -184,8 +190,7 @@ public static double Linstep(double edge0, double edge1, double value)
184190
285, 294, 304, 303, 306, 294, 290, 280,
185191
285, 284, 291, 299, 300, 290, 284, 271,
186192
277, 292, 286, 295, 294, 288, 290, 285,
187-
},
188-
{ //ROOK EG
193+
//ROOK EG
189194
506, 500, 508, 502, 504, 507, 505, 503,
190195
505, 506, 502, 502, 491, 497, 506, 501,
191196
504, 503, 499, 500, 500, 495, 496, 496,
@@ -194,8 +199,7 @@ public static double Linstep(double edge0, double edge1, double value)
194199
500, 503, 500, 505, 498, 498, 499, 489,
195200
496, 495, 502, 505, 498, 498, 491, 499,
196201
492, 497, 498, 496, 493, 493, 497, 480,
197-
},
198-
{ //QUEEN EG
202+
//QUEEN EG
199203
918, 937, 943, 945, 934, 926, 924, 942,
200204
907, 945, 946, 951, 982, 933, 928, 912,
201205
896, 921, 926, 967, 963, 937, 924, 915,
@@ -204,8 +208,7 @@ public static double Linstep(double edge0, double edge1, double value)
204208
911, 892, 933, 928, 934, 942, 934, 924,
205209
907, 898, 883, 903, 903, 893, 886, 888,
206210
886, 887, 890, 872, 916, 890, 906, 879,
207-
},
208-
{ //KING EG
211+
//KING EG
209212
-74, -43, -23, -25, -11, 10, 1, -12,
210213
-18, 6, 4, 9, 7, 26, 14, 8,
211214
-3, 6, 10, 6, 8, 24, 27, 3,
@@ -214,7 +217,7 @@ public static double Linstep(double edge0, double edge1, double value)
214217
-27, -10, 9, 20, 23, 14, 2, -12,
215218
-32, -17, 4, 14, 15, 5, -10, -22,
216219
-55, -40, -23, -6, -20, -8, -28, -47,
217-
}};
220+
};
218221

219222
public static short[] MobilityValues = new short[13 * 6]
220223
{
@@ -269,7 +272,7 @@ public static int GetMobility(Board board, int square)
269272
private static int GetMobility(Board board, Piece piece, byte[] targets)
270273
{
271274
int result = 0;
272-
int row = 13 * PieceTableIndex(piece);
275+
int row = 13 * PieceIndex(piece);
273276
foreach (int square in targets)
274277
{
275278
Piece targetPiece = board[square];
@@ -283,7 +286,7 @@ private static int GetMobility(Board board, Piece piece, byte[] targets)
283286
private static int GetMobilitySlider(Board board, Piece piece, byte[][] targets)
284287
{
285288
int result = 0;
286-
int row = 13 * PieceTableIndex(piece);
289+
int row = 13 * PieceIndex(piece);
287290
foreach (var axis in targets)
288291
foreach(var square in axis)
289292
{

MinimalChess/Piece.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ public static class Pieces
5454
//Pawn = 1, Knight = 2, Bishop = 3; Rook = 4, Queen = 5, King = 6
5555
public static int Order(Piece piece) => (int)piece >> 2;
5656

57-
public static Piece Type(Piece piece) => piece & Piece.TypeMask;
58-
5957
//subtracting 2 maps Piece.White (3) to Color.White (1) and Piece.Black (1) to Color.Black (-1)
6058
public static Color Color(this Piece piece) => (Color)((piece & Piece.ColorMask) - 2);
6159

60+
public static bool IsWhite(this Piece piece) => (piece & Piece.ColorMask) == Piece.White;
61+
public static bool IsBlack(this Piece piece) => (piece & Piece.ColorMask) == Piece.Black;
62+
6263
//Use Piece.TypeMask to clear the two bits used for color, then set correct color bits
6364
//adding 2 maps Color.White (1) to Piece.White (3) and Color.Black (-1) to Piece.Black (1)
64-
public static Piece OfColor(this Piece piece, Color color) => Type(piece) | (Piece)(color + 2);
65+
public static Piece OfColor(this Piece piece, Color color) => (piece & Piece.TypeMask) | (Piece)(color + 2);
6566

6667
public static Color Flip(Color color) => (Color)(-(int)color);
6768

MinimalChessBoard/AlgebraicNotation.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ public static Move ToMove(Board board, string notation)
3838
{
3939
//pawn capture
4040
int toSquare = Notation.ToSquare(notation.Substring(2, 2));
41-
return SelectMove(board, Piece.Pawn, toSquare, promotion, notation[0]);
41+
return SelectMove(board, Piece.Pawn.OfColor(board.SideToMove), toSquare, promotion, notation[0]);
4242
}
4343
else
4444
{
4545
//pawn move
4646
int toSquare = Notation.ToSquare(notation);
47-
return SelectMove(board, Piece.Pawn, toSquare, promotion);
47+
return SelectMove(board, Piece.Pawn.OfColor(board.SideToMove), toSquare, promotion);
4848
}
4949
}
5050

51-
Piece piece = Notation.ToPiece(notation[0]);
51+
Piece piece = Notation.ToPiece(notation[0]).OfColor(board.SideToMove);
5252
if (notation[1] == 'x')
5353
{
5454
//capture
@@ -83,9 +83,9 @@ private static Move SelectMove(Board board, Piece moving, int toSquare, Piece pr
8383
{
8484
if (move.ToSquare != toSquare)
8585
continue;
86-
if (Pieces.Type(move.Promotion) != Pieces.Type(promotion))
86+
if (move.Promotion != promotion)
8787
continue;
88-
if (Pieces.Type(board[move.FromSquare]) != Pieces.Type(moving))
88+
if (board[move.FromSquare] != moving)
8989
continue;
9090
if (fileOrRank != null && !Notation.ToSquareName(move.FromSquare).Contains(fileOrRank.Value))
9191
continue;

MinimalChessBoard/Program.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ private static void SetColor(Piece piece, int rank, int file, Move move)
160160
Console.BackgroundColor = ConsoleColor.DarkCyan;
161161
}
162162

163-
if (piece.Color() == Color.White)
164-
Console.ForegroundColor = ConsoleColor.White;
165-
else
166-
Console.ForegroundColor = ConsoleColor.Gray;
163+
Console.ForegroundColor = piece.IsWhite() ? ConsoleColor.White : ConsoleColor.Gray;
167164
}
168165

169166
private static void ListMoves(Board board, int depth)
@@ -297,7 +294,7 @@ private static void CompareBestMove(string filePath, int timeBudgetMs)
297294
List<Move> bestMoves = new List<Move>();
298295
while (!file.EndOfStream)
299296
{
300-
ParseEpd(file.ReadLine(), out Board board, ref bestMoves);
297+
ParseEpd(file.ReadLine(), out Board board, bestMoves);
301298
Transpositions.Clear();
302299
IterativeSearch search = new IterativeSearch(board);
303300
Move pvMove = default;
@@ -328,8 +325,7 @@ private static void CompareBestMove(string filePath, int timeBudgetMs)
328325
Console.WriteLine($"Best move found in {foundBest} / {count} positions!");
329326
}
330327

331-
332-
private static void ParseEpd(string epd, out Board board, ref List<Move> bestMoves)
328+
private static void ParseEpd(string epd, out Board board, List<Move> bestMoves)
333329
{
334330
//The parser expects a fen-string with bm delimited by a ';'
335331
//Example: 2q1r1k1/1ppb4/r2p1Pp1/p4n1p/2P1n3/5NPP/PP3Q1K/2BRRB2 w - - bm f7+; id "ECM.001";

MinimalChessEngine/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace MinimalChessEngine
77
{
88
public static class Program
99
{
10-
const string NAME_VERSION = "MinimalChess 0.6.1";
10+
const string NAME_VERSION = "MinimalChess 0.6.2";
1111
const string AUTHOR = "Thomas Jahn";
1212

1313
static Engine _engine = new Engine();

0 commit comments

Comments
 (0)