https://www.chessprogramming.org/index.php?title=CPW-Engine_utils&feed=atom&action=history
CPW-Engine utils - Revision history
2024-03-28T20:21:28Z
Revision history for this page on the wiki
MediaWiki 1.30.1
https://www.chessprogramming.org/index.php?title=CPW-Engine_utils&diff=9325&oldid=prev
GerdIsenberg: Created page with "'''Home * Engines * CPW-Engine * Utils''' <pre> #include "stdafx.h" #include "0x88_math.h" #include "transposition.h" enum etask { TASK_NOTHING,..."
2018-12-18T15:05:01Z
<p>Created page with "'''<a href="/Main_Page" title="Main Page">Home</a> * <a href="/Engines" title="Engines">Engines</a> * <a href="/CPW-Engine" title="CPW-Engine">CPW-Engine</a> * Utils''' <pre> #include "stdafx.h" #include "0x88_math.h" #include "transposition.h" enum etask { TASK_NOTHING,..."</p>
<p><b>New page</b></p><div>'''[[Main Page|Home]] * [[Engines]] * [[CPW-Engine]] * Utils'''<br />
<br />
<pre><br />
<br />
#include "stdafx.h"<br />
#include "0x88_math.h"<br />
#include "transposition.h"<br />
<br />
<br />
enum etask {<br />
TASK_NOTHING,<br />
TASK_SEARCH,<br />
TASK_PONDER<br />
} extern task;<br />
<br />
extern bool time_over;<br />
<br />
unsigned int gettime();<br />
<br />
int util_pv(char * pv) {<br />
<br />
sboard rootb = b;<br />
<br />
char best;<br />
smove m[256];<br />
int mcount = 0;<br />
<br />
for (U8 depth = 1; depth <= sd.depth; depth++) {<br />
<br />
best = -1;<br />
tt_probe(0, 0, 0, &best);<br />
<br />
if (best == -1) break;<br />
<br />
mcount = movegen(m, 0xFF);<br />
<br />
for (int i = 0; i<mcount; i++) {<br />
if (m[i].id == best) {<br />
move_make(m[i]);<br />
<br />
pv = algebraic_writemove(m[i], pv);<br />
pv[0] = ' ';<br />
pv++;<br />
<br />
break;<br />
}<br />
}<br />
}<br />
<br />
pv[0] = 0;<br />
b = rootb;<br />
return 0;<br />
}<br />
<br />
void perft_start(char * command) {<br />
<br />
unsigned int starttime = gettime();<br />
<br />
int depth;<br />
sscanf(command + 6, "%d", &depth);<br />
<br />
printf("Performance Test\n");<br />
for (U8 d = 1; d <= depth; d++) {<br />
printf("%d:\t%d\t%d\n", d, gettime() - starttime, perft(d));<br />
}<br />
}<br />
<br />
U64 perft(U8 depth) {<br />
<br />
U64 nodes = 0;<br />
<br />
if (depth == 0) return 1;<br />
<br />
smove m[256];<br />
int mcount = movegen(m, 0xFF);<br />
<br />
for (int i = 0; i < mcount; i++) {<br />
move_make(m[i]);<br />
<br />
if (!isAttacked(b.stm, b.KingLoc[!b.stm]))<br />
nodes += perft(depth - 1);<br />
<br />
move_unmake(m[i]);<br />
}<br />
<br />
return nodes;<br />
}<br />
<br />
void util_bench(char * command) {<br />
<br />
unsigned int starttime = gettime();<br />
<br />
int depth;<br />
sscanf(command + 6, "%d", &depth);<br />
<br />
time_over = false;<br />
<br />
sd.myside = b.stm;<br />
sd.starttime = gettime();<br />
sd.depth = 0;<br />
sd.nodes = 0;<br />
<br />
task = TASK_SEARCH;<br />
<br />
chronos.depth = depth;<br />
chronos.flags = FDEPTH;<br />
<br />
search_run();<br />
<br />
task = TASK_NOTHING;<br />
<br />
if (gettime() - starttime < 1000) starttime = gettime() - 1000;<br />
<br />
printf("Nodes:\t%d\nTime:\t%d ms\nNPS:\t%d\n", (int)sd.nodes, gettime() - starttime, (int)sd.nodes / ((gettime() - starttime) / 1000));<br />
<br />
return;<br />
}<br />
<br />
smove strToMove(char * a) {<br />
smove m;<br />
<br />
m.from = convert_a_0x88(a);<br />
m.to = convert_a_0x88(a + 2);<br />
<br />
m.piece_from = b.pieces[m.from];<br />
m.piece_to = b.pieces[m.from];<br />
m.piece_cap = b.pieces[m.to];<br />
<br />
m.flags = 0;<br />
m.castle = 0;<br />
m.ep = -1;<br />
m.ply = 0;<br />
m.score = 0;<br />
<br />
/* default promotion to queen */<br />
<br />
if ((m.piece_to == PAWN) &&<br />
(ROW(m.to) == ROW_1 || ROW(m.to) == ROW_8))<br />
m.piece_to = QUEEN;<br />
<br />
<br />
switch (a[4]) {<br />
case 'q': m.piece_to = QUEEN; a++; break;<br />
case 'r': m.piece_to = ROOK; a++; break;<br />
case 'b': m.piece_to = BISHOP; a++; break;<br />
case 'n': m.piece_to = KNIGHT; a++; break;<br />
}<br />
<br />
//castling<br />
if ((m.piece_from == KING) &&<br />
((m.from == E1 && (m.to == G1 || m.to == C1)) ||<br />
(m.from == E8 && (m.to == G8 || m.to == C8)))) {<br />
m.flags = MFLAG_CASTLE;<br />
}<br />
<br />
/* ep<br />
if the moving-piece is a Pawn, the square it moves to is empty and<br />
it was a diagonal move it has to be an en-passant capture.<br />
*/<br />
if ((m.piece_from == PAWN) &&<br />
(m.piece_cap == PIECE_EMPTY) &&<br />
((abs(m.from - m.to) == 15) || (abs(m.from - m.to) == 17))) {<br />
m.flags = MFLAG_EPCAPTURE;<br />
}<br />
<br />
if ((m.piece_from == PAWN) && (abs(m.from - m.to) == 32)) {<br />
m.flags |= MFLAG_EP;<br />
}<br />
<br />
return m;<br />
}<br />
<br />
int algebraic_moves(char * a) {<br />
<br />
smove m;<br />
int found_match = 0;<br />
<br />
while (a[0]) {<br />
<br />
if (!((a[0] >= 'a') && (a[0] <= 'h'))) {<br />
a++;<br />
continue;<br />
}<br />
<br />
m = strToMove(a);<br />
<br />
found_match = move_isLegal(m);<br />
<br />
if (found_match) {<br />
<br />
move_make(m);<br />
<br />
if ((m.piece_from == PAWN) ||<br />
(move_iscapt(m)) ||<br />
(m.flags == MFLAG_CASTLE))<br />
b.rep_index = 0;<br />
<br />
}<br />
else {<br />
<br />
break;<br />
<br />
}<br />
<br />
a += 4;<br />
if (a[0] == 0) break;<br />
if (a[0] != ' ') a++;<br />
<br />
}<br />
<br />
return found_match;<br />
}<br />
<br />
<br />
//returns new pointer<br />
char * algebraic_writemove(smove m, char * a) {<br />
char parray[5] = { 0,'q','r','b','n' };<br />
<br />
convert_0x88_a(m.from, a);<br />
convert_0x88_a(m.to, a + 2);<br />
a += 4;<br />
if (m.piece_to != m.piece_from) {<br />
a[0] = parray[m.piece_to];<br />
a++;<br />
}<br />
a[0] = 0;<br />
return a;<br />
}<br />
<br />
void convert_0x88_a(S8 sq, char * a) {<br />
a[0] = COL(sq) + 'a';<br />
a[1] = ROW(sq) + '1';<br />
a[2] = 0;<br />
}<br />
<br />
U8 convert_a_0x88(char * a) {<br />
S8 sq;<br />
sq = a[0] - 'a';<br />
sq += (a[1] - '1') * 16;<br />
<br />
return sq;<br />
}<br />
</pre><br />
'''[[CPW-Engine|Up one Level]]'''</div>
GerdIsenberg