Discussion of chess software programming and technical issues.
Moderators: hgm , Rebel , chrisw
Henk
Posts: 7217 Joined: Mon May 27, 2013 10:31 am
Post
by Henk » Thu Jan 24, 2019 4:44 pm
This code works for iterating over squares in C#. Just use foreach.
Code: Select all
fieldIterator.Reset(pos.Board.Pawns);
foreach(var field in fieldIterator)
{
}
public class FieldIterator : IEnumerator<IField>
{
ulong iniBits;
UInt64 bits;
ulong bit;
IBoardSquares board;
public IField Current
{
get
{
return board[bit];
}
}
object IEnumerator.Current
{
get
{
return bit;
}
}
BitBoardIndex bbIndex = BitBoardIndex.Instance;
public FieldIterator( IBoardSquares board)
{
this.board = board;
}
public FieldIterator(UInt64 target, IBoardSquares board)
{
iniBits = target;
bits = iniBits;
this.board = board;
}
public void Reset(UInt64 target)
{
iniBits = target;
bits = iniBits;
}
public void Dispose()
{
}
public bool MoveNext()
{
bit = bits & (~bits + 1);
bits &= bits - 1;
return bit != 0;
}
public void Reset()
{
bits = iniBits;
}
public FieldIterator GetEnumerator()
{
return this;
}
}
Henk
Posts: 7217 Joined: Mon May 27, 2013 10:31 am
Post
by Henk » Thu Jan 24, 2019 5:29 pm
Don't know yet if you can make it slower using lambda expressions.
Henk
Posts: 7217 Joined: Mon May 27, 2013 10:31 am
Post
by Henk » Fri Jan 25, 2019 4:46 pm
Easy to make it even more slow. You have to collect the squares into a collection before you can apply lambda expressions.
For instance:
Code: Select all
var target = pos.Board.GetBits(Kind.Rook_Kind, colorSign);
fieldIter.Reset(target);
var rookFieldList = new List<IField>(fieldIter.Fields());
if (testCastledShort)
{
return !rookFieldList.Exists(rookField => rookField.ColNr > kingColNr);
}
class FieldIterator
{
..
public IEnumerable<IField> Fields()
{
foreach (var field in this)
{
yield return field;
}
}
}
Henk
Posts: 7217 Joined: Mon May 27, 2013 10:31 am
Post
by Henk » Sat Jan 26, 2019 10:37 am
I think this is another less is slow improvement. Idea is to use LINQ.
Code: Select all
fieldIter.Reset(pos.Board.GetBits(Rook_Kind, colorSign));
if (testCastledShort)
{
return !(from u in fieldIter.Fields() let v = kingColNr where u.ColNr > v select u.ColNr).Any();
}
Henk
Posts: 7217 Joined: Mon May 27, 2013 10:31 am
Post
by Henk » Wed Feb 06, 2019 12:51 pm
LINQ. Slow but compact. Computing space here:
Code: Select all
double result = (from field in fieldIterator.Fields()
let occupier = field.Occupier
let space = field.Space(occupier.Colour)
select occupier.ColorSign2 * space).Sum();
Henk
Posts: 7217 Joined: Mon May 27, 2013 10:31 am
Post
by Henk » Fri Feb 15, 2019 2:21 pm
KiwiPete(5) takes one minute on my computer. Don't know how fast or slow it had been in the past.
[d] r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
xr_a_y
Posts: 1871 Joined: Sat Nov 25, 2017 2:28 pm
Location: France
Post
by xr_a_y » Fri Feb 15, 2019 2:25 pm
Code: Select all
# +-+-+-+-+-+-+-+-+
# |r| | | |k| | |r|
# +-+-+-+-+-+-+-+-+
# |p| |p|p|q|p|b| |
# +-+-+-+-+-+-+-+-+
# |b|n| | |p|n|p| |
# +-+-+-+-+-+-+-+-+
# | | | |P|N| | | |
# +-+-+-+-+-+-+-+-+
# | |p| | |P| | | |
# +-+-+-+-+-+-+-+-+
# | | |N| | |Q| |p|
# +-+-+-+-+-+-+-+-+
# |P|P|P|B|B|P|P|P|
# +-+-+-+-+-+-+-+-+
# |R| | | |K| | |R|
# +-+-+-+-+-+-+-+-+
# wk e1
# bk e8
# Turn white
# Phase 1
# Static score 84
# Hash 17569928883477024425
# FEN r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
# Info 2019-02-15 14:24:36-678: pseudoNodes 195934515
# Info 2019-02-15 14:24:36-678: validNodes 193690690
# Info 2019-02-15 14:24:36-678: Starting worker 0
# Info 2019-02-15 14:24:36-678: Waiting for workers to join...
# Info 2019-02-15 14:24:36-678: ... ok threadPool deleted
real 0m14,421s
user 0m14,172s
sys 0m0,240s