<?php
// numbers_guessing_game.php
// August, 2023
// RJM Programming
// Using experimental_drag_and_drop.htm for Chess or Draughts or Mak-yek game

$mappingsa=[];
$newurl='./chess_game.php';
if (isset($_GET['version'])) {
   if (substr(urldecode($_GET['version']) . ' ',0,1) == '_' || substr(urldecode($_GET['version']) . ' ',0,1) == '-') {
     if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'chess_game.php' . explode('GETME', urldecode($_GET['version']))[0] . 'GETME')) {
     $newurl='' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'chess_game_new_' . explode('GETME', urldecode($_GET['version']))[0] . '_get_me.php';
     if (!file_exists($newurl)) {
     exec('cp ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'chess_game.php' . explode('GETME', urldecode($_GET['version']))[0] . 'GETME ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'chess_game_new_' . explode('GETME', urldecode($_GET['version']))[0] . '_get_me.php');   
     }
     $newurl='./' . 'chess_game_new_' . explode('GETME', urldecode($_GET['version']))[0] . '_get_me.php';
     }
   } 
   if ($newurl != './chess_game.php' && strpos($_SERVER['REQUEST_URI'], 'chess_game_new_' . explode('GETME', urldecode($_GET['version']))[0] . '_get_me.php') === false) {
   //echo $_SERVER['REQUEST_URI'] . ' ... ' . $newurl . ' ... ' . str_replace('??', '?',('?' . $_SERVER['QUERY_STRING']));
   //exit;
   header('Location: ' . $newurl . str_replace('??', '?',('?' . $_SERVER['QUERY_STRING']))  );
   exit;
   }
}

function lmc($inapu) {
  global $mappingsa;
  $outapu=$inapu;
  for ($ijh=0; $ijh<sizeof($mappingsa); $ijh++) {
    $pts=explode(' data-square=' . explode('_', $mappingsa[$ijh])[1] . ' ', $outapu);
    if (sizeof($pts) > 1) {
      $outpt=explode('</', $pts[1])[0];
      $newpt=$outpt;
      if (strpos($outpt, ' data-piece="') !== false) {
       $ops=explode('"', explode(' data-piece="', $outpt)[1])[0];
       $oih=explode('>', $outpt)[-1 + sizeof(explode('>', $outpt))];
       if (substr($ops, 0, 1) == 'w') {
          $newpt=str_replace(' data-piece="' . $ops . '"', ' data-piece="' . explode('_', $mappingsa[$ijh])[0] . '"', $newpt);
          $newpt=str_replace('>' . $oih, '>&#9813;', $newpt);
       } else if (substr($ops, 0, 1) == 'b') {
          $newpt=str_replace(' data-piece="' . $ops . '"', ' data-piece="' . explode('_', $mappingsa[$ijh])[0] . '"', $newpt);
          $newpt=str_replace('>' . $oih, '>&#9819;', $newpt);
       }
      } else if (strpos($outpt, ' data-piece=') !== false) {
       $ops=explode('>', explode(' ', explode(' data-piece=', $outpt)[1])[0])[0];
       $oih=explode('>', $outpt)[-1 + sizeof(explode('>', $outpt))];
       if (substr($ops, 0, 1) == 'w') {
          $newpt=str_replace(' data-piece=' . $ops . ' ', ' data-piece=' . explode('_', $mappingsa[$ijh])[0] . ' ', $newpt);
          $newpt=str_replace('>' . $oih, '>&#9813;', $newpt);
       } else if (substr($ops, 0, 1) == 'b') {
          $newpt=str_replace(' data-piece=' . $ops . ' ', ' data-piece=' . explode('_', $mappingsa[$ijh])[0] . ' ', $newpt);
          $newpt=str_replace('>' . $oih, '>&#9819;', $newpt);
       }
      } else if (strpos($outpt, ' data-answer="') !== false) {
       $ops=explode('"', explode(' data-answer="', $outpt)[1])[0];
       //$oih=explode('>', $outpt)[-1 + sizeof(explode('>', $outpt))];
       if (substr($mappingsa[$ijh], 0, 1) == 'w') {
          $newpt=str_replace(' data-answer="' . $ops . '"', ' data-answer="' . explode('_', $mappingsa[$ijh])[0] . '-9813"', $newpt);
          //$newpt=str_replace('>' . $oih, '>&#9815;', $newpt);
       } else if (substr($mappingsa[$ijh], 0, 1) == 'b') {
          $newpt=str_replace(' data-answer="' . $ops . '"', ' data-answer="' . explode('_', $mappingsa[$ijh])[0] . '-9819"', $newpt);
          //$newpt=str_replace('>' . $oih, '>&#9821;', $newpt);
       }
      } 
      if ($newpt != $outpt) {
         $outapu=str_replace($outpt, $newpt, $outapu);
      }
    }
  }
  return $outapu;
}

function apu($inapu, $sqn) {
  global $mappingsa, $workwith, $mwith;
  if ($sqn == '') {
     $sqn=explode('w', explode('b', explode($inapu . '_', ($workwith . $mwith))[1])[0])[0];
  }
  if (strtoupper(substr($inapu,-1,1)) == (substr($inapu,-1,1)) && strtolower(substr($inapu,-1,1)) != (substr($inapu,-1,1))) {
    array_push($mappingsa, $inapu . '_' . $sqn);
  }
  return $inapu;
}

function initialgame($intg) {
   return $intg;
   $outtg=$intg;
   $ths=[];
   $thas=explode($intg, '</th>');
   for ($i=0; $i<sizeof($thas); $i++) {
      if (1 == 1) {
      $thisth='<span' . str_replace('th','span',explode('<th', $thas[$i])[-1 + sizeof(explode('<th', $thas[$i]))]) . '</span>';
      } else {
      $thisth='<th' . explode('<th', $thas[$i])[-1 + sizeof(explode('<th', $thas[$i]))] . '</th>';
      }
      array_push($ths, $thisth);
      $outtg=str_replace($thisth, '', $outtg);
   }
   $thas=explode($outtg, '</td>');
   for ($i=(-1 + sizeof($ths)); $i>=0; $i--) {
     $outtg=str_replace($thas[$i], $thas[$i] . $ths[$i], $outtg);
   }
   return $outtg;
}

$across=8;
$theans=rand(1,64);
$theclue="switch ($nclue) {
    case 0:
 $clue = '&#9820;';
        break;
    case 1:
 $clue = '&#9820;';
        break;
    case 2:
 $clue = '&#9820;';
        break;
    case 3:
 $clue = '&#9820;';
        break;
    case 4:
 $clue = '&#9820;';
        break;
    case 5:
 $clue = '&#9820;';
        break;
    case 6:
 $clue = '&#9820;';
        break;
    case 7:
 $clue = '&#9820;';
        break;
    case 8:
 $clue = '&#9820;';
        break;
    case 9:
 $clue = '&#9820;';
        break;
    case 10:
 $clue = '&#9820;';
        break;
    case 11:
 $clue = '&#9820;';
        break;
    case 12:
 $clue = '&#9820;';
        break;
    case 13:
 $clue = '&#9820;';
        break;
    case 14:
 $clue = '&#9820;';
        break;
    case 15:
 $clue = '&#9820;';
        break;
    case 16:
 $clue = '&#9820;';
        break;
    case 17:
 $clue = '&#9820;';
        break;
    case 18:
 $clue = '&#9820;';
        break;
    case 19:
 $clue = '&#9820;';
        break;
    case 20:
 $clue = '&#9820;';
        break;
    case 21:
 $clue = '&#9820;';
        break;
    case 22:
 $clue = '&#9820;';
        break;
    case 23:
 $clue = '&#9820;';
        break;
    case 24:
 $clue = '&#9820;';
        break;
    case 25:
 $clue = '&#9820;';
        break;
    case 26:
 $clue = '&#9820;';
        break;
    case 27:
 $clue = '&#9820;';
        break;
    case 28:
 $clue = '&#9820;';
        break;
    case 29:
 $clue = '&#9820;';
        break;
    case 30:
 $clue = '&#9820;';
        break;
    case 31:
 $clue = '&#9820;';
        break;
    case 32:
 $clue = '&#9820;';
        break;
    case 33:
 $clue = '&#9820;';
        break;
    case 34:
 $clue = '&#9820;';
        break;
    case 35:
 $clue = '&#9820;';
        break;
    case 36:
 $clue = '&#9820;';
        break;
    case 37:
 $clue = '&#9820;';
        break;
    case 38:
 $clue = '&#9820;';
        break;
    case 39:
 $clue = '&#9820;';
        break;
    case 40:
 $clue = '&#9820;';
        break;
    case 41:
 $clue = '&#9820;';
        break;
    case 42:
 $clue = '&#9820;';
        break;
    case 43:
 $clue = '&#9820;';
        break;
    case 44:
 $clue = '&#9820;';
        break;
    case 45:
 $clue = '&#9820;';
        break;
    case 46:
 $clue = '&#9820;';
        break;
    case 47:
 $clue = '&#9820;';
        break;
    case 48:
 $clue = '&#9820;';
        break;
    case 49:
 $clue = '&#9820;';
        break;
    case 50:
 $clue = '&#9820;';
        break;
    case 51:
 $clue = '&#9820;';
        break;
    case 52:
 $clue = '&#9820;';
        break;
    case 53:
 $clue = '&#9820;';
        break;
    case 54:
 $clue = '&#9820;';
        break;
    case 55:
 $clue = '&#9820;';
        break;
    case 56:
 $clue = '&#9820;';
        break;
    case 57:
 $clue = '&#9820;';
        break;
    case 58:
 $clue = '&#9820;';
        break;
    case 59:
 $clue = '&#9820;';
        break;
    case 60:
 $clue = '&#9820;';
        break;
    case 61:
 $clue = '&#9820;';
        break;
    case 62:
 $clue = '&#9820;';
        break;
    case 63:
 $clue = '&#9820;';
        break;
    case 64:
 $clue = '&#9820;';
        break;
    }
";
//$theclue=file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/Games/Battleshipsandcruisers/index2.php');
$ansa=explode('case ' . $theans . ':', $theclue);
$thecluewording=explode("'", explode("'", $ansa[1])[1])[0];

 $scorebit='';
 if (isset($_GET['score']) && isset($_GET['secs'])) {
   $scorebit="Score " . $_GET['score'] . '/' . $_GET['secs'];
 }

 $wikistuff=str_replace(' ','_',$thecluewording) . ' ' . $thecluewording;
 $wikistuff="1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
41 41
42 42
43 43
44 44
45 45
46 46
47 47
48 48
49 49
50 50
51 51
52 52
53 53
54 54
55 55
56 56
57 57
58 58
59 59
60 60
61 61
62 62
63 63
64 64
";

  $lastotherde='';
  $wikistuffs=explode("\n", $wikistuff);
  $wikidesignations=[];
  $wikidescriptions=[];
  $crandlist='';
  $randlist=[];
  for ($i=0; $i<sizeof($wikistuffs); $i++) {
    $thisde=explode(' ', $wikistuffs[$i])[0];
    $otherde=substr($wikistuffs[$i], (strlen($thisde) + 1));
    if (strpos($otherde, ') ') !== false) {
      $otherde=explode(') ', $otherde)[1];
    }
    //echo $otherde;
    //exit;
    if (trim($thisde) != '') {
    array_push($wikidesignations, $thisde);
    array_push($wikidescriptions, trim($otherde));
    $lastotherde=$otherde;
    }
  }
  
  for ($i=0; $i<1; $i++) {
    $j=rand(0, (-1 + sizeof($wikidesignations)));
    if ($crandlist == '') {
      $crandlist='' . $j;
    } else if (strpos((',' . $crandlist . ','), (',' . $j . ',')) !== false) {
      while (strpos((',' . $crandlist . ','), (',' . $j . ',')) !== false) {
        $j=rand(0, (-1 + sizeof($wikidesignations)));
      }
      $crandlist.=',' . $j;
    } else {
      $crandlist.=',' . $j;
    }
  }
  
  $randlist=explode(',', $crandlist);
  $randmode=0; //rand(0,1);
  $correctans=(-1 + $theans); //rand(0,98);
  
  $callbackurl='./chess_game.php';
  $drux='http://' . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "" . explode("#", explode("?", str_replace('/chess_game.php','/chess_game.php',$_SERVER['REQUEST_URI']))[0])[0];
  $templategame=file_get_contents('./experimental_drag_and_drop.htm');
  $finnards='';
  if (isset($_GET['traditional'])) {
   $suffards='';
   $mwith='';
   $workwith=str_replace('+',' ',urldecode($_GET['traditional']));
   if (isset($_GET['move'])) {
    $mwith=str_replace('+',' ',urldecode($_GET['move']));
    $suffards="&move=" . $mwith;
    $suffs=explode('_', $mwith);
    if (sizeof($suffs) >= 3) {
     if (strpos($workwith, $suffs[0] . '_' . $suffs[1]) === false) {
       $mwith=$suffs[0] . '_' . $suffs[1];
     } else {
       $mwith='';
     }
    }
   } 
   $ps=explode('_', ($workwith . $mwith));
   $finnards=$drux . '?traditional=';
   if (sizeof($ps) == 1) {
   if ($workwith == 'mayhem') {
   $finnards.='mayhem';
   if (isset($_GET['bname'])) {
     $finnards.='&bname=' . $_GET['bname'];
   if (!isset($_GET['wname'])) {
     $finnards.='&wname=';
   }
   }
   if (isset($_GET['wname'])) {
   if (!isset($_GET['bname'])) {
     $finnards.='&bname=';
   }
     $finnards.='&wname=' . $_GET['wname'];
   }
   $suffards='&move=';
   } else {
   if ($workwith == 'draughts' || isset($_GET['draughts']) || $workwith == 'makyek' || isset($_GET['makyek'])) {
   if (isset($_GET['draughts'])) { $suffards='&draughts='; }
   if (isset($_GET['makyek'])) { $suffards='&makyek='; }
   if ($workwith == 'draughts' || isset($_GET['draughts'])) {
   $cpa=['bB1','wB1','bB2','wB2','bB3','wB3','bB4','wB4','bB5','wB5','bB6','wB6','bB7','wB7','bB8','wB8','bB9','wB9','bBv','wBv','bBu','wBu','bBt','wBt'];
   $staidmap=[1,56,3,58,5,60,7,62,8,49,10,51,12,53,14,55,17,40,19,42,21,44,23,46];
   } else if ($workwith == 'makyek' || isset($_GET['makyek'])) {
   $cpa=['bR1','wR1','bR2','wR2','bR3','wR3','bR4','wR4','bR5','wR5','bR6','wR6','bR7','wR7','bR8','wR8','bR9','wR9','bRr','wRr','bRs','wRs','bRt','wRt','bRu','wRu','bRv','wRv','bRx','wRx','bRy','wRy'];
   $staidmap=[0,56,1,57,2,58,3,59,4,60,5,61,6,62,7,63,    16,40,17,41,18,42,19,43,20,44,21,45,22,46,23,47];
   }
   } else {
   $cpa=['bK','wK','bQ','wQ','bR1','wR1','bR2','wR2','bB1','wB1','bB2','wB2','bK1','wK1','bK2','wK2','bP1','wP1','bP2','wP2','bP3','wP3','bP4','wP4','bP5','wP5','bP6','wP6','bP7','wP7','bP8','wP8'];
   $staidmap=[4,60,3,59,0,56,7,63, 2,58,5,61, 1,57,6,62, 8,48,9,49,10,50,11,51,12,52,13,53,14,54,15,55];
   }
   for ($ips=0; $ips<sizeof($cpa); $ips++) {
     $finnards.=$cpa[$ips] . '_' . (1 + $staidmap[$ips]);
   }
   if (isset($_GET['bname'])) {
     $finnards.='&bname=' . $_GET['bname'];
   if (!isset($_GET['wname'])) {
     $finnards.='&wname=';
   }
   }
   if (isset($_GET['wname'])) {
   if (!isset($_GET['bname'])) {
     $finnards.='&bname=';
   }
     $finnards.='&wname=' . $_GET['wname'];
   }
   $suffards='&move=';
   }
   } else {
   $finnards.=apu($ps[0], explode('b',explode('w', $ps[0])[0])[0]) . '_';
   for ($ips=1; $ips<sizeof($ps); $ips++) {
     $sqis=explode('b',explode('w', $ps[$ips])[0])[0];
     apu($ps[$ips], $sqis);
     $finnards.=$sqis;
     if ($ps[$ips] != $sqis) {  $finnards.=explode($sqis, $ps[$ips])[1] . '_'; }
   }
   //echo $finnards;
   }
   if (isset($_GET['bname'])) {
     $finnards.='&bname=' . $_GET['bname'];
   if (!isset($_GET['wname'])) {
     $finnards.='&wname=';
   }
   }
   if (isset($_GET['wname'])) {
   if (!isset($_GET['bname'])) {
     $finnards.='&bname=';
   }
     $finnards.='&wname=' . $_GET['wname'];
   }
   $finnards.=$suffards;
   $sparef=$finnards;
   if ($workwith == 'draughts' || isset($_GET['draughts'])) {
   $finnards='mailto:?subject=Draughts%20Game&body=' . urlencode($sparef);
   } else if ($workwith == 'makyek' || isset($_GET['makyek'])) {
   $finnards='mailto:?subject=Mak-yek%20Game&body=' . urlencode($sparef);
   } else {
   $finnards='mailto:?subject=Chess%20Game&body=' . urlencode($sparef);
   }
  }
  
  if (4 == 4) {
  $arow='';
  $rownum=4;
  $tdnum=64;
  for ($ijk=10; $ijk<=$tdnum; $ijk++) {
    if (($ijk % 3) == 1) {
      $rownum=(1 + ((-1 + $ijk) / 3));
      $arow='<tr id=tr' . $rownum . '><td id=td' . $rownum . '1>' . $ijk . '</td></tr>';
    } else if (($ijk % 3) == 0) {
      $arow=str_replace('</tr>', '<td id=td' . $rownum . '3>' . $ijk . '</td></tr>', $arow);
      $templategame=str_replace('</table>', $arow . '</table>', $templategame);
      $arow='';
    } else {
      $arow=str_replace('</tr>', '<td id=td' . $rownum . '2>' . $ijk . '</td></tr>', $arow);
    }
  }
  $templategame=str_replace('</table>', $arow . '</table>', $templategame);
    //file_put_contents('xx.html', $templategame);
  
  $curclass='white';
  if ($across != 3 && 1 == 1) {
    $irws=1;
    $usedtobesofartd='';
    $sofartd='';
    $two=2;
    $wasih="<table" . explode("</table>", explode("<table", $templategame)[1])[0] . "</table>";
    $origih=$wasih;
    $rowas=explode("</tr>", $wasih);
    for ($irws=1; $irws<sizeof($rowas); $irws++) {
       if (strlen($rowas[$irws]) > 3 && strpos(substr(str_replace("\n","",$rowas[$irws]),0,2),'</') === false) {
          $wasih=str_replace($rowas[(-1 + $irws)] . '</tr>', $rowas[(-1 + $irws)] . '<!-' . '-/tr>', $wasih);
       }
    }
    $rowas=explode("<tr", $wasih);
    for ($irws=2; $irws<sizeof($rowas); $irws++) {
       if (strlen($rowas[$irws]) > 3) {
          $wasih=str_replace('<tr' . $rowas[$irws], '<tr' . str_replace(explode('>', $rowas[$irws])[0] . '>', '-' . '->', $rowas[$irws]), $wasih);
       }
    }
    //file_put_contents('xxxx.html', str_replace($origih, $wasih, $templategame));
    $usedtoberowas=explode("</td>", $wasih);
    $rowas=explode("</td>", $wasih);
    for ($irws=0; $irws<sizeof($rowas); $irws++) {
       //$rowas[$irws]=str_replace('<td', '<td class=' . $curclass . ' data-square=' . (1 + $irws) . '', $rowas[$irws]);
       //if (strpos($rowas[$irws], '>') === false) {
       $rowas[$irws]=str_replace('<td', '<td data-square=' . (1 + $irws) . '', $rowas[$irws]);
       //} else {
       //$fb=explode('>', $rowas[$irws])[-1 + sizeof(explode('>', $rowas[$irws]))];
       //$rowas[$irws]=str_replace($fb, $fb . ' data-square=' . (1 + $irws) . '', $rowas[$irws]);
       //}
       if ($curclass == 'white' && strpos($rowas[$irws], '<tr') === false) {
         $curclass='black';
       } else if ($curclass == 'white' && strpos($rowas[$irws], '<tr') !== false) {
         $curclass='white';
       } else if (strpos($rowas[$irws], '</tr>') === false) {
         $curclass='white';
       } else {
         $curclass='black';
       }
       if (((1 + $irws) % $across) == 0) {
          $usedtobesofartd.=$usedtoberowas[$irws] . '</td>';
          $sofartd.=$rowas[$irws] . '</td>';
          $wasih=str_replace($usedtobesofartd, $sofartd . '</tr><tr id=tr' . $two . '>', $wasih);    
          $usedtobesofartd='';
          $sofartd='';
          $two++;
       } else {
          $usedtobesofartd.=$usedtoberowas[$irws] . '</td>';
          $sofartd.=$rowas[$irws] . '</td>';
       }
    }
    for ($irws=9; $irws<=16; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=1; $irws<=8; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" style="background-color: rgba(255,255,255,0.6);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=25; $irws<=32; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=17; $irws<=24; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=41; $irws<=48; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=33; $irws<=40; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=57; $irws<=64; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    for ($irws=49; $irws<=56; $irws+=2) {
      $wasih=str_replace(' data-square=' . $irws . ' ', ' class=white style="background-color: rgba(255,255,255,0.5);" data-square=' . $irws . ' ', $wasih);
      $wasih=str_replace(' data-square=' . (1 + $irws) . ' ', ' class=black style="background-color: rgba(0,0,0,0.6);" data-square=' . (1 + $irws) . ' ', $wasih);
    }
    //file_put_contents('x.html', str_replace($origih, $wasih, $templategame));
    $templategame=str_replace($origih, $wasih, $templategame);
  }  
  
  }
    //file_put_contents('xxx.html', $templategame);
  $namesdecided='';
  
  $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
  $templategame=str_replace('</body>', '<input type=text value="" style="position:absolute;top:-900px;left:-900px;"></input></body>', $templategame);
  if (!isset($_GET['traditional'])) {
  $templategame=str_replace('</h4>', '&nbsp;<details style=display:inline-block; open><summary style=margin-top:-18px;></summary><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" style="display:none;background-color:#f9f9f9;padding-left:3px;padding-right:3px;"  contenteditable=true id=pblack>Black</p><p onclick="document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility=(' . "'' + " . 'document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility).toLowerCase().replace(' . "'" . 'visible' . "','" . 'HIDDEN' . "'" . ').replace(' . "'" . 'hidden' . "','" . 'visible' . "'" . ');" title="Click here to toggle commentary visibilty." style=display:none; id=pvs>&nbsp;versus&nbsp;</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" style="display:none;background-color:#f9f9f9;padding-left:3px;padding-right:3px;"  contenteditable=true id=pwhite>White</p> </details><input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  } else if (strpos($_GET['traditional'], 'mayhem') !== false) {
  $templategame=str_replace('</h4>', '&nbsp;<details style=display:inline-block; open><summary style=margin-top:-18px;></summary><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" style="display:none;background-color:#f9f9f9;padding-left:3px;padding-right:3px;"  contenteditable=true id=pblack>Black</p><p style=display:none; id=pvs>&nbsp;versus&nbsp;</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" style="display:none;background-color:#f9f9f9;padding-left:3px;padding-right:3px;"  contenteditable=true id=pwhite>White</p> <input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  } else if (isset($_GET['bname']) && isset($_GET['wname'])) {
  $namesdecided=' data-decided="bw" ';
  $templategame=str_replace('</h4>', '&nbsp;<p style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pblack>' . str_replace('+',' ',urldecode($_GET['bname'])) . '</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" onclick="document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility=(' . "'' + " . 'document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility).toLowerCase().replace(' . "'" . 'visible' . "','" . 'HIDDEN' . "'" . ').replace(' . "'" . 'hidden' . "','" . 'visible' . "'" . ');" title="Click here to toggle commentary visibilty." style=display:inline-block; id=pvs>&nbsp;versus&nbsp;</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pwhite>' . str_replace('+',' ',urldecode($_GET['wname'])) . '</p> </details><input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  } else if (isset($_GET['wname'])) {
  $namesdecided=' data-decided="w" ';
  $templategame=str_replace('</h4>', '&nbsp;<details style=display:inline-block; open><summary style=margin-top:-18px;></summary><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" onblur="if (this.innerHTML != ' . "'" . 'Black' . "'" . ') {  location.href=document.URL + ' . "'" . '&bname=' . "' + encodeURIComponent(this.innerHTML); " . ' document.getElementById(' . "'" . 'pstatus' . "'" . ').setAttribute(' . "'" . 'data-decided' . "','" . 'bw' . "');  }" . '" style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pblack>Black</p><p onclick="document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility=(' . "'' + " . 'document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility).toLowerCase().replace(' . "'" . 'visible' . "','" . 'HIDDEN' . "'" . ').replace(' . "'" . 'hidden' . "','" . 'visible' . "'" . ');" title="Click here to toggle commentary visibilty." style=display:inline-block; id=pvs>&nbsp;versus&nbsp;</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" style="display:none;background-color:#f9f9f9;padding-left:3px;padding-right:3px;"  contenteditable=true id=pblack>Black</p><p onclick="document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility=(' . "'' + " . 'document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility).toLowerCase().replace(' . "'" . 'visible' . "','" . 'HIDDEN' . "'" . ').replace(' . "'" . 'hidden' . "','" . 'visible' . "'" . ');" title="Click here to toggle commentary visibilty." style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pwhite>' . str_replace('+',' ',urldecode($_GET['wname'])) . '</p> <input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  } else if (isset($_GET['bname'])) {
  $namesdecided=' data-decided="b" ';
  $templategame=str_replace('</h4>', '&nbsp;<p style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pblack>' . str_replace('+',' ',urldecode($_GET['bname'])) . '</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" onclick="document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility=(' . "'' + " . 'document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility).toLowerCase().replace(' . "'" . 'visible' . "','" . 'HIDDEN' . "'" . ').replace(' . "'" . 'hidden' . "','" . 'visible' . "'" . ');" title="Click here to toggle commentary visibilty." style=display:inline-block; id=pvs>&nbsp;versus&nbsp;</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" onblur="if (this.innerHTML != ' . "'" . 'White' . "'" . ') {  location.href=document.URL + ' . "'" . '&wname=' . "' + encodeURIComponent(this.innerHTML); " . ' document.getElementById(' . "'" . 'pstatus' . "'" . ').setAttribute(' . "'" . 'data-decided' . "','" . 'bw' . "');  }" . '" style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pwhite>White</p> </details><input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  } else {
  $templategame=str_replace('</h4>', '&nbsp;<details style=display:inline-block; open><summary style=margin-top:-18px;></summary><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" onblur="if (this.innerHTML != ' . "'" . 'Black' . "'" . ') { location.href=document.URL + ' . "'" . '&bname=' . "' + encodeURIComponent(this.innerHTML);  }" . '" style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pblack>Black</p><p style=display:none; contenteditable=true id=pblack>Black</p><p onclick="document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility=(' . "'' + " . 'document.getElementById(' . "'" . 'pstatus' . "'" . ').style.visibility).toLowerCase().replace(' . "'" . 'visible' . "','" . 'HIDDEN' . "'" . ').replace(' . "'" . 'hidden' . "','" . 'visible' . "'" . ');" title="Click here to toggle commentary visibilty." style=display:inline-block; id=pvs>&nbsp;versus&nbsp;</p><p title="Example for email could be R. Metcalfe [rmetcalfe15@gmail.com] or for SMS inside [] would all be numbers" onblur="if (this.innerHTML != ' . "'" . 'White' . "'" . ') { location.href=document.URL + ' . "'" . '&wname=' . "' + encodeURIComponent(this.innerHTML);  }" . '" style="display:inline-block;background-color:#f0f0f0;padding-left:7px;padding-right:7px;border:1px dotted rgb(241,241,241);"  contenteditable=true id=pwhite>White</p> </details><input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  }
  if ($workwith == 'draughts' || isset($_GET['draughts'])) {
  $templategame=str_replace('>Game</button>', ' style=background-color:orange;>Draughts Game <br>Practice</button>&nbsp;<p' . $namesdecided . ' style=display:inline-block;font-size:14px;visibility:visible; id=pstatus></p>', $templategame);
  } else if ($workwith == 'makyek' || isset($_GET['makyek'])) {
  $templategame=str_replace('>Game</button>', ' style=background-color:orange;>Mak-yek Game <br>Practice</button>&nbsp;<p' . $namesdecided . ' style=display:inline-block;font-size:14px;visibility:visible; id=pstatus></p>', $templategame);
  } else {
  $templategame=str_replace('>Game</button>', ' style=background-color:lime;>Chess Game <br>Practice</button>&nbsp;<p' . $namesdecided . ' style=display:inline-block;font-size:14px;visibility:visible; id=pstatus></p>', $templategame);
  }
  $templategame=str_replace('></h3>', '>' . $scorebit . '</h3>', $templategame);
  $thepsource=explode('f0;">', explode("</p>", $templategame)[0])[-1 + sizeof(explode('f0;">', explode("</p>", $templategame)[0]))];

  //$templategame=str_replace(', 2023<', ', 2023 ... thanks to <a target=_blank href=https://en.wikipedia.org/wiki/Street_suffix>https://en.wikipedia.org/wiki/Street_suffix</a><', $templategame);
  $templategame=str_replace('>Reset example<', ' style="display:none;">Reset example<', $templategame);
  
  if (1 == 2) {
  $templategame=str_replace('</b' . 'ody>', "<input type=hidden value=tab' . 'lece' . 'llb' . 'c></input></b" . "ody>", $templategame);
  } else {
  if ($workwith == 'draughts' || isset($_GET['draughts'])) {
  $templategame=str_replace('<h3', '<h3 style=display:none;', str_replace('</b' . 'ody>', "<style> #source { font-size: 36px; color:black; background-color: transparent; }  td span {  text-align: center;  } td + th { margin-left:60px; }  td {  text-align: center;  }  .source {  text-align: center;  }  #mytable { width: 320px; height: 320px; font-size: 36px;  text-shadow:-1px 1px 1px #2d95ff; text-align: center;  } #target { font-size: 36px;  }  #mytable td { height:40px; width:40px; }  .white { background-color: rgba(255,255,255,0.5);  text-align: center;  }  .black {   background-color: rgba(0,0,0,0.6); text-align: center;  }     </style></b" . "ody>", $templategame));
  } else if ($workwith == 'makyek' || isset($_GET['makyek'])) {
  $templategame=str_replace('<h3', '<h3 style=display:none;', str_replace('</b' . 'ody>', "<style> #source { font-size: 36px; color:black; background-color: transparent; }  td span {  text-align: center;  } td + th { margin-left:60px; }  td {  text-align: center;  }  .source {  text-align: center;  }  #mytable { width: 320px; height: 320px; font-size: 36px;  text-shadow:-1px 1px 1px #952dff; text-align: center;  } #target { font-size: 36px;  }  #mytable td { height:40px; width:40px; }  .white { background-color: rgba(255,255,255,0.5);  text-align: center;  }  .black {   background-color: rgba(0,0,0,0.6); text-align: center;  }     </style></b" . "ody>", $templategame));
  } else {
  $templategame=str_replace('<h3', '<h3 style=display:none;', str_replace('</b' . 'ody>', "<style> #source { font-size: 36px; color:black; background-color: transparent; }  td span {  text-align: center;  } td + th { margin-left:60px; }  td {  text-align: center;  }  .source {  text-align: center;  }  #mytable { width: 320px; height: 320px; font-size: 36px;  text-shadow:-1px 1px 1px #ff2d95; text-align: center;  } #target { font-size: 36px;  }  #mytable td { height:40px; width:40px; }  .white { background-color: rgba(255,255,255,0.5);  text-align: center;  }  .black {   background-color: rgba(0,0,0,0.6); text-align: center;  }     </style></b" . "ody>", $templategame));
  }
  $dzfrom='Drop Zone';
  $dzto='';
  if (!isset($_GET['traditional'])) {
    $templategame=str_replace($dzfrom, $dzto, $templategame);
    $dzfrom='Experimental Drag and Drop';
    //$dzto=$dzfrom . '&nbsp;<span id=divdz style="margin: 0 0 0 0;"><button style=background-color:rgba(255,0,0,0.5); id=chessinit onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=y'" . ").replace('.php&','.php?'" . ');">Traditional Start to Game</button>&nbsp;<button style=background-color:rgba(255,0,255,0.5); id=chessinitm onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=mayhem'" . ").replace('.php&','.php?'" . ');">Weird Start to Game</button><font size=1 id=fnodz>&nbsp;...&nbsp;no checks on move lagaility yet ...</font></span>';
    $dzto=str_replace(' and ',' <a style=text-decoration:none;cursor:pointer; title=Menu onclick="location.href=document.URL.split(String.fromCharCode(63))[0].split(String.fromCharCode(35))[0];">&amp;</a> ',$dzfrom) . '&nbsp;<span id=divdz style="margin: 0 0 0 0;"><button style=background-color:rgba(255,0,0,0.5); id=chessinit onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=y'" . ").replace('.php&','.php?'" . ');">Traditional Start to Chess Game</button>&nbsp;<button style=background-color:rgba(0,255,255,0.5); id=chessinitd onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=draughts'" . ").replace('.php&','.php?'" . ');">Draughts Game</button>&nbsp;<button style=background-color:rgba(0,127,127,0.5); id=chessinity onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=makyek'" . ").replace('.php&','.php?'" . ');">Mak-yek Game</button>&nbsp;<button style=background-color:rgba(255,0,255,0.5); id=chessinitm onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=mayhem'" . ").replace('.php&','.php?'" . ');">Weird Start to Chess Game</button>&nbsp;<button style=background-color:rgba(255,255,0,0.5); id=chessinitmid onclick="location.href=(document.URL.split(String.fromCharCode(35))[0] + ' . "'&traditional=' + encodeURIComponent(prompt('Pieces have designations bK wK bQ wQ bR1 wR1 bR2 wR2 bB1 wB1 bB2 wB2 bK1 wK1 bK2 wK2 bP1 wP1 bP2 wP2 bP3 wP3 bP4 wP4 bP5 wP5 bP6 wP6 bP7 wP7 bP8 wP8 ... so example answer bK_25wK_35bQ_32wQ_37&move=wQ_37_39 places the Kings and Queens to squares in the range 1 to 64 (starting top left of board) and the optional but advisable move says move white Queen from square 37 to square 39 ... as an albeit pretty stupid (on so many levels) example!','')).replace(encodeURIComponent(String.fromCharCode(38)),String.fromCharCode(38)).replace(encodeURIComponent(String.fromCharCode(61)),String.fromCharCode(61))" . ").replace('.php&','.php?'" . ');">Midway Start to Chess Game</button><a style=display:none; id=fshare href="' . $finnards . '">Email or SMS</a><font id=fnodz>&nbsp;</font></span>';
  } else {
    $templategame=str_replace("https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/setData","",$templategame);
    $templategame=str_replace("Thanks to ","",$templategame);
    $templategame=str_replace($dzfrom, $dzto, $templategame);
    $dzfrom='Experimental Drag and Drop';
    $dzto=str_replace(' and ',' <a style=text-decoration:none;cursor:pointer; title=Menu onclick="location.href=document.URL.split(String.fromCharCode(63))[0].split(String.fromCharCode(35))[0];">&amp;</a> ',$dzfrom) . '<a style=display:none; id=fshare href="' . $finnards . '">Email or SMS</a><span id=fnodz></span>';
  }
  if ($workwith == 'draughts' || isset($_GET['draughts'])) {
  $templategame=str_replace($dzfrom, $dzto, str_replace(') : 3;', ') : 8; // 8', str_replace(') : 9;', ') : 64; // 64', str_replace(')) : 1;', ')) : 0; // 32', str_replace('] ? true : false;', '] ? true : false; ', str_replace(") : '';", ") : '&#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815; &#9821; &#9815;';", $templategame))))));
  } else if ($workwith == 'makyek' || isset($_GET['makyek'])) {
  $templategame=str_replace($dzfrom, $dzto, str_replace(') : 3;', ') : 8; // 8', str_replace(') : 9;', ') : 64; // 64', str_replace(')) : 1;', ')) : 0; // 32', str_replace('] ? true : false;', '] ? true : false; ', str_replace(") : '';", ") : '&#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814; &#9820; &#9814;';", $templategame))))));
  } else {
  $templategame=str_replace($dzfrom, $dzto, str_replace(') : 3;', ') : 8; // 8', str_replace(') : 9;', ') : 64; // 64', str_replace(')) : 1;', ')) : 0; // 32', str_replace('] ? true : false;', '] ? true : false; ', str_replace(") : '';", ") : '&#9818; &#9812; &#9819; &#9813; &#9820;<span>&#8288;</span> &#9814;<span>&#8288;</span> &#9820; &#9814; &#9821;<span>&#8288;</span> &#9815;<span>&#8288;</span> &#9821; &#9815; &#9822; &#9816;<span>&#8288;</span> &#9822;<span>&#8288;</span> &#9816; &#9823; &#9817; &#9823; &#9817;<span>&#8288;</span> &#9823;<span>&#8288;</span> &#9817;<span>&#8288;&#8288;</span> &#9823;<span>&#8288;&#8288;</span> &#9817;<span>&#8288;&#8288;&#8288;</span> &#9823;<span>&#8288;&#8288;&#8288;</span> &#9817;<span>&#8288;&#8288;&#8288;&#8288;</span> &#9823;<span>&#8288;&#8288;&#8288;&#8288;</span> &#9817;<span>&#8288;&#8288;&#8288;&#8288;&#8288;</span> &#9823;<span>&#8288;&#8288;&#8288;&#8288;&#8288;&#8288;</span> &#9817;<span>&#8288;&#8288;&#8288;&#8288;&#8288;&#8288;</span> &#9823;<span>&#8288;&#8288;&#8288;&#8288;&#8288;&#8288;&#8288;</span> &#9817;<span>&#8288;&#8288;&#8288;&#8288;&#8288;&#8288;&#8288;</span>';", $templategame))))));
  }
  }
  
  //echo $crandlist . "\n" . $thepsource . "\n" . $correctans . "\n";
  //exit;
  //echo $crandlist . "\n";
  //echo "" . $randlist[$correctans] . "\n" . $wikidescriptions[$randlist[$correctans]] . "\n" . $wikidesignations[$randlist[$correctans]];
  //exit;
  
  $templategame=str_replace("<head>", "<head><meta id='myviewport' name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.1, maximum-scale=8, user-scalable=yes'  >", $templategame);
  
  if ($randmode == 0) {  // designation up top and descriptions in drop zone  
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' style=text-align:center; data-answer="0' . $theans . '">' . $theans . '<', $templategame);
    $templategame=str_replace('">' . $thepsource . '<', 'text-align:center;" data-answer="0' . $theans. '">' . $thecluewording . '<', $templategame);
    for ($i=1; $i<=99; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' style=text-align:center; data-answer="0">' . $i . '</td', $templategame);
       }
    }
    //$templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    $templategame=lmc($templategame);
    if (isset($_GET['traditional'])) {
      $templategame=initialgame($templategame);
    }
    echo $templategame;
  } else { // description up top and designations in drop zone
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' style=text-align:center; data-answer="' . $wikidesignations[$randlist[$correctans]] . '">' .$wikidesignations[$randlist[$correctans]] . '<', $templategame);
    $templategame=str_replace('">' . $thepsource . '<', 'text-align:center;" data-answer="' . $wikidesignations[$randlist[$correctans]] . '">' . $wikidescriptions[$randlist[$correctans]] . '<', $templategame);
    for ($i=1; $i<=99; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' style=text-align:center; data-answer="">' . mapittwo($wikidesignations[$randlist[-1 + $i]]) . '</td', $templategame);
       }
    }
    //$templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    if (isset($_GET['traditional'])) {
      $templategame=initialgame($templategame);
    }
    echo $templategame;
  }
  

?>
