Skip to content

Commit be7d687

Browse files
committed
MoveList now stores SortedMove instead of Move: a struct that stores a move together with a priority. This eliminates the cost of repeatedly computing MvvLva or history values for moves while sorting. It's not a big ELO win but it's a more obvious choice for handling the move sorting and thus a better fit for a engine that tries to keep things simple and stupid.
1 parent d77cb1a commit be7d687

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

MinimalChess/MoveCollection.cs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,61 +37,46 @@ public static bool HasMoves(Board position)
3737
}
3838
}
3939

40-
public class MoveList : List<Move>
40+
public struct SortedMove : IComparable<SortedMove>
41+
{
42+
public float Priority;
43+
public Move Move;
44+
public int CompareTo(SortedMove other) => other.Priority.CompareTo(Priority);
45+
public static implicit operator Move(SortedMove m) => m.Move;
46+
}
47+
48+
public class MoveList : List<SortedMove>
4149
{
4250
internal static MoveList Quiets(Board position)
4351
{
4452
MoveList quietMoves = new MoveList();
45-
position.CollectQuiets(quietMoves.Add);
53+
position.CollectQuiets(m => quietMoves.Add(m, 0));
4654
return quietMoves;
4755
}
4856

4957
internal static MoveList SortedCaptures(Board position)
5058
{
5159
MoveList captures = new MoveList();
52-
position.CollectCaptures(captures.Add);
53-
captures.SortMvvLva(position);
60+
position.CollectCaptures(m => captures.Add(m, ScoreMvvLva(m, position)));
61+
captures.Sort();
5462
return captures;
5563
}
5664

57-
internal static MoveList SortedQuiets(Board position, History history, float threshold)
58-
{
59-
MoveList quiets = new MoveList();
60-
position.CollectQuiets(move =>
61-
{
62-
float score = history.Value(position, move);
63-
//if score >= threshold insertion-sort else just add
64-
int index = score >= threshold ? quiets.FindIndex(m => history.Value(position, m) <= score) : -1;
65-
if (index >= 0)
66-
quiets.Insert(index, move);
67-
else
68-
quiets.Add(move);
69-
});
70-
return quiets;
71-
}
72-
7365
public static MoveList SortedQuiets(Board position, History history)
7466
{
7567
MoveList quiets = new MoveList();
76-
position.CollectQuiets(quiets.Add);
77-
quiets.SortHistory(position, history);
68+
position.CollectQuiets(m => quiets.Add(m, history.Value(position, m)));
69+
quiets.Sort();
7870
return quiets;
7971
}
8072

81-
private void SortMvvLva(Board context)
73+
private static int ScoreMvvLva(Move move, Board context)
8274
{
83-
int Score(Move move)
84-
{
85-
Piece victim = context[move.ToSquare];
86-
Piece attacker = context[move.FromSquare];
87-
return Pieces.MaxOrder * Pieces.Order(victim) - Pieces.Order(attacker);
88-
}
89-
Sort((a, b) => Score(b).CompareTo(Score(a)));
75+
Piece victim = context[move.ToSquare];
76+
Piece attacker = context[move.FromSquare];
77+
return Pieces.MaxOrder * Pieces.Order(victim) - Pieces.Order(attacker);
9078
}
9179

92-
private void SortHistory(Board context, History history)
93-
{
94-
Sort((a, b) => history.Value(context, b).CompareTo(history.Value(context, a)));
95-
}
80+
private void Add(Move move, float priority) => Add(new SortedMove { Move = move, Priority = priority });
9681
}
9782
}

0 commit comments

Comments
 (0)