Mobility
Moderators: hgm, Rebel, chrisw
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
Re: Mobility
so queen mobility is based on rooks and bishops? sorry, but it is confusing.mcostalba wrote:I prefer your version, is more reaadable and probably faster but....anyhow....outAtime wrote:Im gonna test this :
Code: Select all
int b_ofs[] = { 11, -11, 13, -13, 0 }; int r_ofs[] = { 12, -12, 1, -1, 0 }; int piece_mobility(int sq, int offsets[]) { int p, *d = offsets, s = 0; for ( ; *d; d++) for (p = sq + *d; board[p] == npiece; p += *d) s++; return s; } int bishop_mobility(int sq){ return piece_mobility(sq, b_ofs); } int rook_mobility(int sq) { return piece_mobility(sq, r_ofs); } int queen_mobility(int sq) { return piece_mobility(sq, b_ofs) + piece_mobility(sq, r_ofs); }
outAtime
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
Re: Mobility
That newest mobility code idea returns a different score than the previous bishop_mobility... maybe something is missing? It is certainly nice and compact.
outAtime
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
Re: Mobility
Any idea how to define "opponent" without being clunky (many lines of code) ? I'd like to try this but keep imagining I'll need to have a white_knight_mobility and a black_knight_mobility so I can use something like black_opponent and white_opponent where e.g.Harald wrote:
Loops and rays for a knight?
Try this:HaraldCode: Select all
/* use piece offsets to get mobility score */ int offsets = [-10, 10, -14, 14, -23, 23, -25, 25]; int knight_mobility(int squaren) { int n; int g = 0; for (n = 0; n < 8; ++n) if (board[squaren + offsets[n]] == npiece) // or == opponent g++; return g; }
Code: Select all
int white_opponent = bpawn || bknight || bbishop || brook || bqueen || bking;
outAtime
-
- Posts: 318
- Joined: Thu Mar 09, 2006 1:07 am
Re: Mobility
Oh my god! Are you sure you want to learn programming C with a chess program?outAtime wrote:Any idea how to define "opponent" without being clunky (many lines of code) ? I'd like to try this but keep imagining I'll need to have a white_knight_mobility and a black_knight_mobility so I can use something like black_opponent and white_opponent where e.g.Harald wrote:
Loops and rays for a knight?
Try this:HaraldCode: Select all
/* use piece offsets to get mobility score */ int offsets = [-10, 10, -14, 14, -23, 23, -25, 25]; int knight_mobility(int squaren) { int n; int g = 0; for (n = 0; n < 8; ++n) if (board[squaren + offsets[n]] == npiece) // or == opponent g++; return g; }
Id really rather not have to duplicate so much code....because Id need to also have white_bishop_mobility etc... rooks and queens.. ughCode: Select all
int white_opponent = bpawn || bknight || bbishop || brook || bqueen || bking;
Perhaps learn programming C first, at least a little bit, and then return to chess.
On the other side I like to learn new programming languages with chess modules myself.
I have many comments:
Your code for white_opponewnt won't work. You are using the wrong or-operator or even a wrong data model.
If your chess board has its pieces as ints and each type (piece and color) has only one bit set, like
wpawn = 1, wknight = 2, wbishop = 4, ..., wking = 32, bpawn = 64, bknight = 128, ..., bking = 2048;
then this may work:
Code: Select all
int white_opponent = bpawn | bknight | bbishop | brook | bqueen | bking;
int noPiece=0, border = 99;
int wpawn = 1, wknight = 2, wbishop = 3, ..., wking = 6, bpawn = -1, bknight = -2, ..., bking = -6;
And then the white opponent can be found with
if (board[sq] < 0)
The black opponent can be found with
if (board[sq] > 0 && board[sq] != border)
|| is a logical operator for truth values with 0/false or 1/true as result.
It has a shortcut evaluation that stops when the result is clear, that is
after the first true value is found.
| is a binary operator that works on bits and the result collects all set bits
of the single parts. A result != 0 may be considered true in a second step.
If your bitwise definition of white opponent is working, then you only need
to calculate it once and make the variable global that will cost no time.
a)
#define WHITE_OPPONENT (BPAWN | BKNIGHT | ... | BKING)
or b)
const int white_opponent = (bpawn | ... | bking);
and then
if (board[sq] & WHITE_OPPONENT)
Some people prefer char board[] over int board[] because this will need less space.
Find out what is best for you. The bitwise approach won't work with this.
Think about a distinction between all opponents and valuable opponents (no pawns).
Other topic (seen in another posting):
If you change a line like
for (n = 0; n < 8; ++n)
to
for (n = 0; n < 8; n++)
that does no harm because most compilers will generate the same code.
But you should know the difference and know what you are doing.
In complex C++ classes with an own ++operator there may be a difference
and often ++X should be preferred over X++.
And last not least: Try to understand the code from helping people
before you copy and paste it.
Harald
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
Re: Mobility
Sorry I wasn't clear. I was trying to say that is what I _don't_ want to do. I'm looking for alternatives to this sort of definition.
outAtime
-
- Posts: 226
- Joined: Sun Mar 08, 2009 3:08 pm
- Location: Canada
Re: Mobility
Looks like my only choice for now... I can't find anything else without doing a huge re-write...This is probably wrong... although it does compile, lol.
where...
hate having to repeat code like this, but it's all I can figure out
Code: Select all
#define frame 0
#define wpawn 1
#define bpawn 2
#define wknight 3
#define bknight 4
#define wking 5
#define bking 6
#define wrook 7
#define brook 8
#define wqueen 9
#define bqueen 10
#define wbishop 11
#define bbishop 12
#define npiece 13
#define black_piece bpawn | bknight | bbishop | brook | bqueen | bking
#define white_piece wpawn | wknight | wbishop | wrook | wqueen | wking
#define bmob (npiece | white_piece)
#define wmob (npiece | black_piece)
Code: Select all
int white_bishop_mobility(int squareb)
{
int p, d, bfs, s = 0;
for (d = 0; d < 4; d++)
{
bfs = b_offsets[d];
for (p = squareb + bfs; board[p] == wmob; p += bfs)
s++;
}
return s;
}
Code: Select all
int black_bishop_mobility(int squareb)
{
int p, d, bfs, s = 0;
for (d = 0; d < 4; d++)
{
bfs = b_offsets[d];
for (p = squareb + bfs; board[p] == bmob; p += bfs)
s++;
}
return s;
}
outAtime