<?php
// numbers_guessing_game.php
// August, 2023
// RJM Programming
// Using experimental_drag_and_drop.htm for Numbers Guessing game

$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 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';
  $dru='http://' . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "" . explode("#", explode("?", str_replace('/numbers_guessing_game.php','/experimental_drag_and_drop.htm',$_SERVER['REQUEST_URI']))[0])[0];
  //$templategame=file_get_contents($dru . '?tdnum=99');
  $templategame=file_get_contents('./experimental_drag_and_drop.htm');
  
  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);
  
  $templategame=str_replace('</h4>', '<input type=hidden id="callXback" value="' . $callbackurl . '"></input></h4>', $templategame);
  $templategame=str_replace('>Game<', '>Chess Game Practice<', $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 {
  $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>';
  } 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=$dzfrom . '<span id=fnodz></span>';
  }
  $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;
  
  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);
    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;
  }
  

?>
