@ -58,7 +58,7 @@ my $sth7 = $dbh->prepare(q/SELECT id FROM black WHERE b0 = ? and b1 = ? and k0 =
#my $sth8 = $dbh->prepare(q/SELECT black.b0, black.b1, black.k0, black.n0, black.n1, black.p0, black.p1, black.p2, black.p3, black.p4, black.p5, black.p6, black.p7, black.q0, black.r0, black.r1, white.b0, white.b1, white.k0, white.n0, white.n1, white.p0, white.p1, white.p2, white.p3, white.p4, white.p5, white.p6, white.p7, white.q0, white.r0, white.r1 FROM black, white WHERE (black.id, white.id) IN (SELECT bid, wid FROM sessions WHERE session = ?)/);
#my $sth8 = $dbh->prepare(q/SELECT black.b0, black.b1, black.k0, black.n0, black.n1, black.p0, black.p1, black.p2, black.p3, black.p4, black.p5, black.p6, black.p7, black.q0, black.r0, black.r1, white.b0, white.b1, white.k0, white.n0, white.n1, white.p0, white.p1, white.p2, white.p3, white.p4, white.p5, white.p6, white.p7, white.q0, white.r0, white.r1 FROM black, white WHERE (black.id, white.id) IN (SELECT bid, wid FROM sessions WHERE session = ?)/);
my$sth8=$dbh->prepare(q/SELECT b0, b1, k0, n0, n1, p0, p1, p2, p3, p4, p5, p6, p7, q0, r0, r1 FROM white WHERE id = ?/);
my$sth8=$dbh->prepare(q/SELECT b0, b1, k0, n0, n1, p0, p1, p2, p3, p4, p5, p6, p7, q0, r0, r1 FROM white WHERE id = ?/);
my$sth9=$dbh->prepare(q/SELECT b0, b1, k0, n0, n1, p0, p1, p2, p3, p4, p5, p6, p7, q0, r0, r1 FROM black WHERE id = ?/);
my$sth9=$dbh->prepare(q/SELECT b0, b1, k0, n0, n1, p0, p1, p2, p3, p4, p5, p6, p7, q0, r0, r1 FROM black WHERE id = ?/);
my$sth10=$dbh->prepare(q/INSERT INTO sessions (session, bid, wid) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT sessions_pkey DO UPDATE SET (session, bid, wid) = ($1, $2, $3)/);
my$sth10=$dbh->prepare(q/INSERT INTO sessions (session, bid, wid) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT sessions_pkey DO UPDATE SET (bid, wid) = ($2, $3)/);
my$sth11=$dbh->prepare(q/SELECT bid, wid FROM sessions WHERE session = ?/);
my$sth11=$dbh->prepare(q/SELECT bid, wid FROM sessions WHERE session = ?/);
@ -170,6 +170,7 @@ sub INIT_KEYS { # For sorted lookup of piece source.
subMOVEMENT_TREE{# Assume relative movement to support abritrary depth (0 < $depth < Inf).
subMOVEMENT_TREE{# Assume relative movement to support abritrary depth (0 < $depth < Inf).
my($player,$depth,$i_al,$i)=(@_[0,1],$_[5]//$_[0]*16-1,$_[2]//-4);# Support for travelling to different tree depths every player turn (could be used to create, e.g., adaptive AI).
my($player,$depth,$i_al,$i)=(@_[0,1],$_[5]//$_[0]*16-1,$_[2]//-4);# Support for travelling to different tree depths every player turn (could be used to create, e.g., adaptive AI).
my(@xid,$prio,%key_list_by_value,@selection);
$i+=4;
$i+=4;
ALLY_SOURCE:foreachmy$asrc(&KEYS($player)){
ALLY_SOURCE:foreachmy$asrc(&KEYS($player)){
my$dpval=$_[3]//0;# Default piece value.
my$dpval=$_[3]//0;# Default piece value.
@ -179,10 +180,22 @@ sub MOVEMENT_TREE { # Assume relative movement to support abritrary depth (0 < $
for(my$c=$#{$tree[$i]{$asrc}};$c>=1;$c--){# This should make the subroutine multiple times faster if there's no previous untravelled branch.
for(my$c=$#{$tree[$i]{$asrc}};$c>=1;$c--){# This should make the subroutine multiple times faster if there's no previous untravelled branch.
@ -196,9 +209,9 @@ sub MOVEMENT_TREE { # Assume relative movement to support abritrary depth (0 < $
$keys[$i_al]=$adest;
$keys[$i_al]=$adest;
{nowarnings;$selection=join',',@keys};# CSV.
{nowarnings;$selection=join',',@keys};# CSV.
$sth1->execute(@keys);
$sth1->execute(@keys);
my$ditet=$sth1->fetchall_arrayref();
my@ditet=$sth1->fetchrow_array();
if($#$ditet!=-1anddefined(${@$ditet[0]}[2/($player+1)+1])){# Already calculated the context for this $adest (and since @keys has a fixed order, for this $piece): Skip to save precious time.
if($#ditet!=-1anddefined($ditet[2/($player+1)+1])){# Already calculated the context for this $adest (and since @keys has a fixed order, for this $piece): Skip to save precious time.
}elsif($user_mode!=$player){# The other side has checkmated you (the AI). The AI should know when to give up (even if it weren't for efficiency reasons).
# } elsif ($user_mode != $player) { # The other side has checkmated you (the AI). The AI should know when to give up (even if it weren't for efficiency reasons).
if($user_mode==-1){# So, instead of performing winning moves (which will always be done at this tree depth), we have enough data here to revert back to some previous (hence valid) board state (with untravelled nodes). Rather than starting from scratch every time, this might be preferable for AI training. The importance of the first few chess moves shouldn't be undermined, however. Therefore, include some appropriate randomization.
# if ($user_mode == -1) { # So, instead of performing winning moves (which will always be done at this tree depth), we have enough data here to revert back to some previous (hence valid) board state (with untravelled nodes). Rather than starting from scratch every time, this might be preferable for AI training. The importance of the first few chess moves shouldn't be undermined, however. Therefore, include some appropriate randomization.
#TODO: Usual DB writing followed by POC.
# #TODO: Usual DB writing followed by POC.
nextTURN;
# next TURN;
}else{
# } else {
nextTURN;
# next TURN;
}
# }
}
# }
my$matches=grep{!/^[01][.]k[.]0_/}keys%tmpalg;# When only the king can move...
my$matches=grep{!/^[01][.]k[.]0_/}keys%tmpalg;# When only the king can move...