Skip to content

Commit 3be9f3e

Browse files
committed
Made PrincipalVariation simpler to use. It can now grow indefinitely and doesn't have a fixed capacity anymore!
The "position fen" UCI command now doesn't always expect 7 tokens Increased version to 0.3.1 PeSTO
1 parent 107ee10 commit 3be9f3e

File tree

4 files changed

+31
-48
lines changed

4 files changed

+31
-48
lines changed

MinimalChess/IterativeSearch.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class IterativeSearch
1212
public int Depth { get; private set; }
1313
public int Score { get; private set; }
1414
public Board Position => new Board(_root); //return copy, _root must not be modified during search!
15-
public Move[] PrincipalVariation => Depth > 0 ? _pv.GetLine(Depth) : null;
15+
public Move[] PrincipalVariation => _pv.GetLine(Depth);
1616
public bool Aborted => _killSwitch.Triggered;
17-
public bool GameOver => _pv.IsGameOver(Depth);
17+
public bool GameOver => PrincipalVariation?.Length < Depth;
1818

1919
Board _root = null;
2020
List<Move> _rootMoves = null;
@@ -25,14 +25,14 @@ public IterativeSearch(Board board)
2525
{
2626
_root = new Board(board);
2727
_rootMoves = new LegalMoves(board);
28-
_pv = new PrincipalVariation(20);
28+
_pv = new PrincipalVariation();
2929
}
3030

3131
public IterativeSearch(Board board, List<Move> rootMoves)
3232
{
3333
_root = new Board(board);
3434
_rootMoves = rootMoves;
35-
_pv = new PrincipalVariation(20);
35+
_pv = new PrincipalVariation();
3636
}
3737

3838
public void Search(int maxDepth)
@@ -105,7 +105,7 @@ private int EvalPosition(Board position, int depth, SearchWindow window)
105105
if (expandedNodes == 0) //no expansion happened from this node!
106106
{
107107
//having no legal moves can mean two things: (1) lost or (2) draw?
108-
_pv.Clear(depth);
108+
_pv[depth] = default;
109109
return position.IsChecked(position.ActiveColor) ? (int)color * PeSTO.LostValue : 0;
110110
}
111111

MinimalChess/PrincipalVariation.cs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ namespace MinimalChess
44
{
55
class PrincipalVariation
66
{
7-
readonly Move[] _moves;
8-
9-
public PrincipalVariation(int maxDepth)
10-
{
11-
int length = Index(maxDepth + 1);
12-
_moves = new Move[length];
13-
}
7+
Move[] _moves = new Move[0];
8+
int _depth = 0;
149

1510
private int Index(int depth)
1611
{
@@ -19,36 +14,28 @@ private int Index(int depth)
1914
return (d * d + d) / 2;
2015
}
2116

22-
public void Clear(int depth)
17+
public void Grow(int depth)
2318
{
24-
int iDepth = Index(depth);
25-
for (int i = 0; i < depth; i++)
26-
_moves[iDepth + i] = default;
19+
while (_depth < depth)
20+
Grow();
2721
}
2822

29-
public void Grow(int depth)
23+
private void Grow()
3024
{
31-
//TODO: if depth > maxdepth return or else we get a crash. but that's fine for now as it will probably a bug has caused an "endless" search and we want to find those!
32-
33-
Clear(depth);
34-
if (depth <= 1)
35-
return;
36-
37-
//copy previous line
38-
int start = Index(depth - 1);
39-
int nullMove = Array.IndexOf(_moves, default, start, depth);
40-
int count = nullMove - start;
41-
42-
Clear(depth);
43-
int target = Index(depth);
44-
for (int i = 0; i < count; i++)
45-
_moves[target + i] = _moves[start + i];
46-
47-
//copy to sub-pv's
48-
for (int i = 1; i < depth; i++)
25+
_depth++;
26+
int size = Index(_depth + 1);
27+
Move[] moves = new Move[size];
28+
//copy pv lines to new array
29+
int to = 0;
30+
for (int depth = 0; depth < _depth; depth++)
4931
{
50-
this[i] = _moves[target + depth - i];
32+
//copy the last d elements of the mainline
33+
for (int i = 0; i < depth; i++)
34+
moves[to++] = _moves[^(depth - i)];
35+
//leave one free
36+
to++;
5137
}
38+
_moves = moves;
5239
}
5340

5441
public Move[] GetLine(int depth)
@@ -58,17 +45,10 @@ public Move[] GetLine(int depth)
5845
int count = (nullMove == -1) ? depth : nullMove - start;
5946

6047
Move[] line = new Move[count];
61-
Array.Copy(_moves, Index(depth), line, 0, count);
48+
Array.Copy(_moves, start, line, 0, count);
6249
return line;
6350
}
6451

65-
public bool IsGameOver(int depth)
66-
{
67-
int start = Index(depth);
68-
int nullMove = Array.IndexOf(_moves, default, start, depth);
69-
return nullMove != -1;
70-
}
71-
7252
public Move this[int depth]
7353
{
7454
get
@@ -79,7 +59,7 @@ public Move this[int depth]
7959
{
8060
int a = Index(depth);
8161
_moves[a] = value;
82-
62+
//remember the continuation
8363
int b = Index(depth - 1);
8464
for (int i = 0; i < depth - 1; i++)
8565
_moves[a + i + 1] = _moves[b + i];

MinimalChessEngine/Program.cs

Lines changed: 5 additions & 2 deletions
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.3 PeSTO";
10+
const string NAME_VERSION = "MinimalChess 0.3.1 PeSTO";
1111

1212
static Engine _engine = new Engine();
1313

@@ -75,7 +75,10 @@ private static void UciPosition(string[] tokens)
7575
if (tokens[1] == "startpos")
7676
_engine.SetupPosition(new Board(Board.STARTING_POS_FEN));
7777
else if (tokens[1] == "fen") //rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
78-
_engine.SetupPosition(new Board($"{tokens[2]} {tokens[3]} {tokens[4]} {tokens[5]} {tokens[6]} {tokens[7]}"));
78+
{
79+
string fen = string.Join(' ', tokens[2..]);
80+
_engine.SetupPosition(new Board(fen));
81+
}
7982
else
8083
{
8184
Uci.Log("'position' parameters missing or not understood. Assuming 'startpos'.");

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 Windows</PublishDir>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.3.1 PeSTO Windows</PublishDir>
1212
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>True</PublishSingleFile>

0 commit comments

Comments
 (0)