option:color=" " nogoodprint

!!option=noorder (plusieurs rponses dans n'importe quel ordre) non document car n'est peut-tre
!! pas trs utile : ne peut pas servir pour numrer les dplacements possibles d'une seule pice.
!!option=nogoodprint : does not give the good answer in the embed part

ans_lettre=a,b,c,d,e,f,g,h
!set ans_color=!getopt color in $(replyoption$i)
!default ans_color=[#CA906E,#F0EAB9,black]
ans_rep=!rows2lines $(reply$i)
ans_rep_cnt=!linecnt $ans_rep

ans_Rep$i=
ans_rep$i=
!for j=1 to $ans_rep_cnt
  z=!line $j of $ans_rep
  z=!replace internal - by , -, in $z
  d_y=$[($(z[1])-1)%8 +1]
  d_xx=$[($(z[1])-$d_y)/8+1]
  d_x=!item $d_xx of $ans_lettre
  a_y=$[($(z[3])-1)%8 +1]
  a_xx=$[($(z[3])-$a_y)/8+1]
  a_x=!item $a_xx of $ans_lettre
  zz=$(z[2])
  !default zz=-
  ans_rep$i=!append line $d_x$d_y$(zz)$a_x$a_y to $(ans_rep$i)
  ans_Rep$i=!append line $d_x$d_y,$a_x$a_y to $(ans_Rep$i)
!next
!set ans_rep$i=!lines2rows $(ans_rep$i)
!set ans_Rep$i=!lines2rows $(ans_Rep$i)
!set g_answer=!lines2rows $(replygood$i)
!set g_answer=!rows2lines $g_answer
!set ans_initial=!line 1 of $g_answer
!set ans_initial=!declosing $ans_initial
!set g_answer=!line 2 of $g_answer
!set g_answer=!declosing $g_answer
!!pas d'analyse de la rponse
!if $g_answer=$empty
  replygood$i=?analyze
!endif
score=0
tmp=!lines2rows $(ans_Rep$i)
tmp_order=!rows2lines $(ans_Rep$i)
tmp_order=!replace internal , by | in $tmp_order
tmp_order=!lines2items $tmp_order
tmp_order=!nospace $tmp_order
g_ans=!replace internal | by $\
$ in $g_answer
g_ans=!translate internal -+x# to ,,,,  in $g_ans
g_prom=!replace internal = by XX in $g_mv
!if XX isin $g_prom
  g_ans=!translate =[A-Z] to $ $ in $g_ans
!else
  !reset g_prom
!endif
g_ans=!translate [A-Z] to $ $ in $g_ans
g_ans_cnt=!linecnt $g_ans
!reset G_answer
on cherche la meilleure rponse
test=0
sol=1
!for l_ = 1 to $g_ans_cnt
  u=!line $l_ of $g_ans
  test$l_=0
  ans_cnt$l_=!itemcnt $(u[;1])
  !if noorder notwordof $(replyoption$i)
    !for p=1 to $(ans_cnt$l_)
      u_= $(u[$p;])
      !for s = 1 to 2
        v_ = $(u_[$s])
        !if $v_ issametext $(tmp[$p;$s])
        !advance test$l_
        !endif
      !next s
    !next p
  !else noorder
    uorder=!rows2lines $u
    uorder=!replace internal , by | in $uorder
    uorder=!lines2items $uorder
    uorder=!nospace $uorder
    inter=!listintersect $uorder and $tmp_order
    diff1=!listcomplement $uorder in $tmp_order
    diff2=!listcomplement $tmp_order in $uorder
    diff1_cnt=!itemcnt $diff1
    diff2_cnt=!itemcnt $diff2
    test$l_=!itemcnt $inter_cnt
    test$l_=$[2*$(test$l_)]
  !endif
  !if $(test$l_) > $test
    test=$(test$l_)
    sol=$l_
  !endif
!next l_

!!sol est la solution la plus proche d'une bonne solution
G_answer=!line $sol of $g_answer

!!!for p=1 to $(ans_cnt$sol)
!!  !for s=1 to 2
!!    uu=!text expand $(G_answer[$p;$s]) using 01
!!    uu=!words2items $uu
!!    u1=!positionof item $(uu[1]) in $ans_lettre
!!    Feed_g=!append item $(u1)_$(uu[2]) to $Feed_g
!!  !next
!!!next
!if noorder notwordof $(replyoption$i)
  score=$[$(test$sol)/(2*$(ans_cnt$sol))]
!else
  score=$[1-$test/$(ans_cnt$sol)]
!endif

!if $lang=fr
  !set name_init=Position initiale
  !set name_answer= Votre proposition de coup
  !set name_goodanswer=Solution
!else
  !set name_init=Initial position
  !set name_answer=Your proposed move
  !set name_goodanswer=The correct move
!endif

G_answer=!lines2rows $G_answer
!readproc slib/games/chessboard [$ans_initial],$ans_color,id=ini$i caption="$name_init"
ans_init=$slib_out
!readproc slib/games/chessmv [$ans_initial],[$(ans_Rep$i)]
!set r_final=$slib_out
!readproc slib/games/chessboard $(r_final[-1;1]),$ans_color,id=rep$i caption="$name_answer"
reply_$i=$slib_out
!if $g_answer !=$empty
  !readproc slib/games/chessmv [$ans_initial],[$G_answer]
  !set g_final=$slib_out
  !if nogoodprint notwordof $(replyoption$i) and $presentgood!=0
    !readproc slib/games/chessboard $(g_final[-1;1]),$ans_color,caption="$name_goodanswer"
    replyGood$i=<div class="chess_answer oef_indneutral">$slib_out</div>
  !endif
!endif

m_r=$(ans_rep$i)
reply__$i=$m_r
!reset m_reply$i
r_final_=$(r_final)
!for j=1 to $ans_rep_cnt
  m_reply$i=!append line $(m_r[$j;]),$(r_final_[$j;2]),$(r_final_[$j;1]) to $(m_reply$i)
!next
m_reply$i=!lines2rows $(m_reply$i)
m_reply$i=!singlespace $(m_reply$i)
m_reply$i=!words2items $(m_reply$i)
!if $g_answer=$empty
  !for j=1 to 2*$ans_rep_cnt
    reply_$i=!replace internal <td id="cell_rep$(i)_$(ans_Rep$i[$j])" by <td id="cell_rep$(i)_$(ans_Rep$i[$j])" class="oef_indneutral" in $(reply_$i)
  !next
  reply_$i=<div class="flex_box oef_indneutral"><div class="chess_initial">$ans_init</div>\
  <div class="chess_answer">$(reply_$i)</div></div>
  !exit
!endif

!if $score=1
  diareply$i=good
  !advance freegot
!else
  diareply$i=bad
!endif
!if $score=1
  reply_$i=<div class="flex_box"><div class="chess_initial oef_indneutral">$ans_init</div>\
  <div class="chess_answer">$(reply_$i)</div></div>
  reply_$i=!replace internal <table by <table class="oef_indneutral" in $(reply_$i)
!else
  reply_$i=<div class="flex_box"><div class="chess_initial oef_indneutral">$ans_init</div>\
    <div class="chess_answer oef_indneutral">$(reply_$i)</div>\
    $(replyGood$i)</div>
!endif

!for j=1 to 2*$ans_rep_cnt
  !if $(ans_Rep$i[$j]) isin $G_answer
    reply_$i=!replace internal <td id="cell_rep$(i)_$(ans_Rep$i[$j])" class=" by \
      <td id="cell_rep$(i)_$(ans_Rep$i[$j])" class="chess_answer oef_indgood$ $ in $(reply_$i)
  !else
    reply_$i=!replace internal <td id="cell_rep$(i)_$(ans_Rep$i[$j])" class=" by \
      <td id="cell_rep$(i)_$(ans_Rep$i[$j])" class="chess_answer oef_indbad$ $ in $(reply_$i)
  !endif
!next

!!!for j=1 to 2*$(ans_cnt$sol)
!!  reply_$i=!replace internal <td id="cell_rep$(i)_$(Feed_g[$j])" by \
!!    <td id="cell_rep$(i)_$(Feed_g[$j])" class="chess_answer oef_indbad" in $(reply_$i)
!!!next

g_reply_=$(g_final[;2])
g_reply__=

!for u=1 to $(ans_cnt$sol)
  c=!declosing $(g_reply_[$u])
  c_nt=!itemcnt $c
  !if $c_nt=2
    g_reply__=!append line $(G_answer[$u;1])x$(G_answer[$u;2]) to $g_reply__
  !else
    g_reply__=!append line $(G_answer[$u;1])-$(G_answer[$u;2]) to $g_reply__
  !endif
!next
g_reply__=!lines2rows $(g_reply__)

!!!if $score<1 and $presentgood!=0
!!  reply__$i=$(reply__$i); $ch_good1: <span class="tt">$g_reply__</span>.
!!!endif
