Skip to content

Commit d3608b5

Browse files
committed
AnyLegalMoves removed, functionality is now part of LegalMoves.
MoveMargin increased and GC set to SustainedLowLatency to prevent rare time forfeits. Changed the name to mark this as a RC for version 0.4!
1 parent 8042220 commit d3608b5

File tree

8 files changed

+44
-69
lines changed

8 files changed

+44
-69
lines changed

MinimalChess/Board.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,47 +261,47 @@ private bool IsCastling(Piece moving, Move move, out Move rookMove)
261261
public bool CanPlay(Move move)
262262
{
263263
bool found = false;
264-
CollectMoves(m => found |= (m == move), move.FromSquare);
264+
CollectMoves(move.FromSquare, m => found |= (m == move));
265265
return found;
266266
}
267267

268268
public void CollectMoves(Action<Move> moveHandler)
269269
{
270270
for (int square = 0; square < 64; square++)
271-
CollectMoves(moveHandler, square);
271+
CollectMoves(square, moveHandler);
272272
}
273273

274274
public void CollectQuiets(Action<Move> moveHandler)
275275
{
276276
for (int square = 0; square < 64; square++)
277-
CollectQuiets(moveHandler, square);
277+
CollectQuiets(square, moveHandler);
278278
}
279279

280280
public void CollectCaptures(Action<Move> moveHandler)
281281
{
282282
for (int square = 0; square < 64; square++)
283-
CollectCaptures(moveHandler, square);
283+
CollectCaptures(square, moveHandler);
284284
}
285285

286-
public void CollectMoves(Action<Move> moveHandler, int square)
286+
public void CollectMoves(int square, Action<Move> moveHandler)
287287
{
288-
CollectQuiets(moveHandler, square);
289-
CollectCaptures(moveHandler, square);
288+
CollectQuiets(square, moveHandler);
289+
CollectCaptures(square, moveHandler);
290290
}
291291

292-
public void CollectQuiets(Action<Move> moveHandler, int square)
292+
public void CollectQuiets(int square, Action<Move> moveHandler)
293293
{
294294
if (IsActivePiece(_state[square]))
295-
AddQuiets(moveHandler, square);
295+
AddQuiets(square, moveHandler);
296296
}
297297

298-
public void CollectCaptures(Action<Move> moveHandler, int square)
298+
public void CollectCaptures(int square, Action<Move> moveHandler)
299299
{
300300
if (IsActivePiece(_state[square]))
301-
AddCaptures(moveHandler, square);
301+
AddCaptures(square, moveHandler);
302302
}
303303

304-
private void AddQuiets(Action<Move> moveHandler, int square)
304+
private void AddQuiets(int square, Action<Move> moveHandler)
305305
{
306306
switch (_state[square])
307307
{
@@ -339,7 +339,7 @@ private void AddQuiets(Action<Move> moveHandler, int square)
339339
}
340340
}
341341

342-
private void AddCaptures(Action<Move> moveHandler, int square)
342+
private void AddCaptures(int square, Action<Move> moveHandler)
343343
{
344344
switch (_state[square])
345345
{

MinimalChess/IterativeSearch.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ private int EvalPosition(Board position, int depth, SearchWindow window)
114114

115115
private int QEval(Board position, SearchWindow window)
116116
{
117+
if (_killSwitch.Triggered)
118+
return 0;
119+
117120
NodesVisited++;
118121
Color color = position.ActiveColor;
119122

@@ -145,7 +148,7 @@ private int QEval(Board position, SearchWindow window)
145148
return (int)color * Evaluation.LostValue;
146149

147150
//stalemate?
148-
if (expandedNodes == 0 && !AnyLegalMoves.HasMoves(position))
151+
if (expandedNodes == 0 && !LegalMoves.HasMoves(position))
149152
return 0;
150153

151154
//can't capture. We return the 'alpha' which may have been raised by "stand pat"

MinimalChess/MoveCollection.cs

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,34 @@ namespace MinimalChess
55
public class LegalMoves : List<Move>
66
{
77
private static Board _tempBoard = new Board();
8-
private Board _reference;
98

109
public LegalMoves(Board reference) : base(40)
1110
{
12-
_reference = reference;
13-
_reference.CollectMoves(Consider);
14-
_reference = null;
15-
}
16-
17-
public void Consider(Move move)
18-
{
19-
//only add if the move doesn't result in a check for active color
20-
_tempBoard.Copy(_reference);
21-
_tempBoard.Play(move);
22-
if (_tempBoard.IsChecked(_reference.ActiveColor))
23-
return;
24-
25-
Add(move);
26-
}
27-
}
28-
29-
public class AnyLegalMoves
30-
{
31-
private static Board _tempBoard = new Board();
32-
private Board _reference;
33-
34-
public bool CanMove { get; private set; }
35-
36-
public AnyLegalMoves(Board reference)
37-
{
38-
_reference = reference;
39-
_reference.CollectQuiets(Consider);
40-
if (!CanMove)
41-
_reference.CollectCaptures(Consider);
42-
_reference = null;
43-
}
44-
45-
public void Consider(Move move)
46-
{
47-
if (CanMove)//no need to look at any more moves if we got our answer already!
48-
return;
49-
50-
//only add if the move doesn't result in a check for active color
51-
_tempBoard.Copy(_reference);
52-
_tempBoard.Play(move);
53-
if (_tempBoard.IsChecked(_reference.ActiveColor))
54-
return;
55-
56-
CanMove = true;
11+
reference.CollectMoves(move =>
12+
{
13+
//only add if the move doesn't result in a check for active color
14+
_tempBoard.Copy(reference);
15+
_tempBoard.Play(move);
16+
if (!_tempBoard.IsChecked(reference.ActiveColor))
17+
Add(move);
18+
});
5719
}
5820

5921
public static bool HasMoves(Board position)
6022
{
61-
var moves = new AnyLegalMoves(position);
62-
return moves.CanMove;
23+
bool canMove = false;
24+
for (int i = 0; i < 64 && !canMove; i++)
25+
{
26+
position.CollectMoves(i, move =>
27+
{
28+
if (canMove) return;
29+
30+
_tempBoard.Copy(position);
31+
_tempBoard.Play(move);
32+
canMove = !_tempBoard.IsChecked(position.ActiveColor);
33+
});
34+
}
35+
return canMove;
6336
}
6437
}
6538

MinimalChessBoard/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private static void ApplyMoves(Board board, string[] moves)
193193
private static long Perft(Board board, int depth)
194194
{
195195
if (depth <= 0)
196-
return 1;
196+
return 0;
197197

198198
var moves = new LegalMoves(board);
199199
if (depth == 1) //no need to apply the moves before counting them

MinimalChessEngine/Engine.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ private void Search()
153153

154154
//aborted?
155155
if (_search.Aborted)
156-
{
157-
Uci.Log($"Wasted {_time.ElapsedInterval}ms partially searching ply {_search.Depth}!");
158156
break;
159-
}
160157

161158
//collect PV
162159
Collect();

MinimalChessEngine/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using MinimalChess;
22
using System;
3+
using System.Runtime;
34
using System.Threading.Tasks;
45

56
namespace MinimalChessEngine
67
{
78
public static class Program
89
{
9-
const string NAME_VERSION = "MinimalChess 0.3.6.3 Chili [chilipepper003]";
10+
const string NAME_VERSION = "MinimalChess 0.4";
1011

1112
static Engine _engine = new Engine();
1213
static async Task Main(string[] args)
@@ -22,6 +23,7 @@ static async Task Main(string[] args)
2223

2324
private static void Start()
2425
{
26+
GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
2527
_engine.Start();
2628
}
2729

MinimalChessEngine/Properties/PublishProfiles/Windows x64.pubxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
88
<Configuration>Release</Configuration>
99
<Platform>Any CPU</Platform>
1010
<TargetFramework>netcoreapp3.1</TargetFramework>
11-
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.3.6.3 Windows</PublishDir>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.4 Windows</PublishDir>
1212
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>True</PublishSingleFile>

MinimalChessEngine/TimeControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace MinimalChessEngine
44
{
55
class TimeControl
66
{
7-
const int TIME_MARGIN = 15;
7+
const int TIME_MARGIN = 20;
88
const int BRANCHING_FACTOR_ESTIMATE = 5;
99

1010
private int _movesToGo;

0 commit comments

Comments
 (0)