<?php
// colouring_in_was_numbers_guessing_game.php
// April, 2024
// RJM Programming
// Using experimental_drag_and_drop.htm for Colouring In web application
// Note use of input type=color id=ddcolour 
 
$across=99;
if (isset($_GET['across'])) {  $across=str_replace('+',' ',urldecode($_GET['across']));  }
$fontweight=1;
if (isset($_GET['fontweight'])) {  $fontweight=str_replace('+',' ',urldecode($_GET['fontweight']));  }
$theans=rand(1,99);
$theclue="switch ($nclue) {
    case 0:
 $clue = 'Is the additive identity.';
        break;
    case 1:
 $clue = 'Is the multiplicative identity.';
        break;
    case 2:
 $clue = 'Is the only even prime.';
        break;
    case 3:
 $clue = 'Is the number of spatial dimensions we live in.';
        break;
    case 4:
 $clue = 'Is the smallest number of colors sufficient to color all planar maps.';
        break;
    case 5:
 $clue = 'Is the number of Platonic solids.';
        break;
    case 6:
 $clue = 'Is the smallest perfect number.';
        break;
    case 7:
 $clue = 'Is the smallest number of sides of a regular polygon that is not constructible by straightedge and compass.';
        break;
    case 8:
 $clue = 'Is the largest cube in the Fibonacci sequence.';
        break;
    case 9:
 $clue = 'Is the maximum number of cubes that are needed to sum to any positive integer.';
        break;
    case 10:
 $clue = 'Is the base of our number system.';
        break;
    case 11:
 $clue = 'Is the largest known multiplicative persistence.';
        break;
    case 12:
 $clue = 'Is the smallest abundant number.';
        break;
    case 13:
 $clue = 'Is the number of Archimedian solids.';
        break;
    case 14:
 $clue = 'Is the smallest even number n with no solutions to phi(m) = n.';
        break;
    case 15:
 $clue = 'Is the smallest composite number n with the property that there is only one group of order n.';
        break;
    case 16:
 $clue = 'Is the only number of the form xy = yx with x and y being different integers.';
        break;
    case 17:
 $clue = 'Is the number of wallpaper groups.';
        break;
    case 18:
 $clue = 'Is the only positive number that is twice the sum of its digits.';
        break;
    case 19:
 $clue = 'Is the maximum number of 4th powers needed to sum to any number.';
        break;
    case 20:
 $clue = 'Is the number of rooted trees with 6 vertices.';
        break;
    case 21:
 $clue = 'Is the smallest number of distinct squares needed to tile a square.';
        break;
    case 22:
 $clue = 'Is the number of partitions of 8.';
        break;
    case 23:
 $clue = 'Is the smallest number of integer-sided boxes that tile a box so that no two boxes share a common length.';
        break;
    case 24:
 $clue = 'Is the largest number divisible by all numbers less than its square root.';
        break;
    case 25:
 $clue = 'Is the smallest square that can be written as a sum of 2 squares.';
        break;
    case 26:
 $clue = 'Is the only positive number to be directly between a square and a cube.';
        break;
    case 27:
 $clue = 'Is the largest number that is the sum of the digits of its cube.';
        break;
    case 28:
 $clue = 'Is the 2nd perfect number.';
        break;
    case 29:
 $clue = 'Is the 7th Lucas number.';
        break;
    case 30:
 $clue = 'Is the largest number with the property that all smaller numbers relatively prime to it are prime.';
        break;
    case 31:
 $clue = 'Is a Mersenne prime.';
        break;
    case 32:
 $clue = 'Is the smallest non-trivial 5th power.';
        break;
    case 33:
 $clue = 'Is the largest number that is not a sum of distinct triangular numbers.';
        break;
    case 34:
 $clue = 'Is the smallest number with the property that it and its neighbors have the same number of divisors.';
        break;
    case 35:
 $clue = 'Is the number of hexominoes.';
        break;
    case 36:
 $clue = 'Is the smallest non-trivial number which is both square and triangular.';
        break;
    case 37:
 $clue = 'Is the maximum number of 5th powers needed to sum to any number.';
        break;
    case 38:
 $clue = 'Is the last Roman numeral when written lexicographically.';
        break;
    case 39:
 $clue = 'Is the smallest number which has 3 different partitions into 3 parts with the same product.';
        break;
    case 40:
 $clue = 'Is the only number whose letters are in alphabetical order.';
        break;
    case 41:
 $clue = 'Is a value of n so that x squared + x + n takes on prime values for x = 0, 1, 2, ... n-2.';
        break;
    case 42:
 $clue = 'Is the 5th Catalan number.';
        break;
    case 43:
 $clue = 'Is the number of sided 7-iamonds.';
        break;
    case 44:
 $clue = 'Is the number of derangements of 5 items.';
        break;
    case 45:
 $clue = 'Is a Kaprekar number.';
        break;
    case 46:
 $clue = 'Is the number of different arrangements (up to rotation and reflection) of 9 non-attacking queens on a 9×9 chessboard.';
        break;
    case 47:
 $clue = 'Is the largest number of cubes that cannot tile a cube.';
        break;
    case 48:
 $clue = 'Is the smallest number with 10 divisors.';
        break;
    case 49:
 $clue = 'Is the smallest number with the property that it and its neighbors are squareful.';
        break;
    case 50:
 $clue = 'Is the smallest number that can be written as the sum of 2 squares in 2 ways.';
        break;
    case 51:
 $clue = 'Is the 6th Motzkin number.';
        break;
    case 52:
 $clue = 'Is the 5th Bell number.';
        break;
    case 53:
 $clue = 'Is the only two digit number that is reversed in hexadecimal.';
        break;
    case 54:
 $clue = 'Is the smallest number that can be written as the sum of 3 squares in 3 ways.';
        break;
    case 55:
 $clue = 'Is the largest triangular number in the Fibonacci sequence.';
        break;
    case 56:
 $clue = 'Is the number of reduced 5 × 5 Latin squares.';
        break;
    case 57:
 $clue = 'Is 111 in base 7.';
        break;
    case 58:
 $clue = 'Is the number of commutative semigroups of order 4.';
        break;
    case 59:
 $clue = 'Is the number of stellations of an icosahedron.';
        break;
    case 60:
 $clue = 'Is the smallest number divisible by 1 through 6.';
        break;
    case 61:
 $clue = 'Is the 3rd secant number.';
        break;
    case 62:
 $clue = 'Is the smallest number that can be written as the sum of 3 distinct squares in 2 ways.';
        break;
    case 63:
 $clue = 'Is the number of partially ordered sets of 5 elements.';
        break;
    case 64:
 $clue = 'Is the smallest number with 7 divisors.';
        break;
    case 65:
 $clue = 'Is the smallest number that becomes square if its reverse is either added to or subtracted from it.';
        break;
    case 66:
 $clue = 'Is the number of 8-iamonds.';
        break;
    case 67:
 $clue = 'Is the smallest number which is palindromic in bases 5 and 6.';
        break;
    case 68:
 $clue = 'Is the 2-digit string that appears latest in the decimal expansion of π.';
        break;
    case 69:
 $clue = 'Is a value of n where n squared and n cubed together contain each digit once.';
        break;
    case 70:
 $clue = 'Is the smallest weird number.';
        break;
    case 71:
 $clue = 'Is the number that divides the sum of the primes less than it.';
        break;
    case 72:
 $clue = 'Is the maximum number of spheres that can touch another sphere in a lattice packing in 6 dimensions.';
        break;
    case 73:
 $clue = 'Is the smallest multi-digit number which is one less than twice its reverse.';
        break;
    case 74:
 $clue = 'Is the number of different non-Hamiltonian polyhedra with a minimum number of vertices.';
        break;
    case 75:
 $clue = 'Is the number of orderings of 4 objects with ties allowed.';
        break;
    case 76:
 $clue = 'Is an automorphic number.';
        break;
    case 77:
 $clue = 'Is the largest number that cannot be written as a sum of distinct numbers whose reciprocals sum to 1.';
        break;
    case 78:
 $clue = 'Is the smallest number that can be written as the sum of 4 distinct squares in 3 ways.';
        break;
    case 79:
 $clue = 'Is a permutable prime.';
        break;
    case 80:
 $clue = 'Is the smallest number n where n and n+1 are both products of 4 or more primes.';
        break;
    case 81:
 $clue = 'Is the square of the sum of its digits.';
        break;
    case 82:
 $clue = 'Is the number of 6-hexes.';
        break;
    case 83:
 $clue = 'Is the number of strongly connected digraphs with 4 vertices.';
        break;
    case 84:
 $clue = 'Is the largest order of a permutation of 14 elements.';
        break;
    case 85:
 $clue = 'Is the largest n for which 12+22+32+ ... +n2 = 1+2+3+ ... +m has a solution.';
        break;
    case 86:
 $clue = 'Is 222 in base 6.';
        break;
    case 87:
 $clue = 'Is the sum of the squares of the first 4 primes.';
        break;
    case 88:
 $clue = 'Is one of only 2 numbers known whose square has no isolated digits.';
        break;
    case 89:
 $clue = 'Is 8 to power of one + 9 to the power of 2.';
        break;
    case 90:
 $clue = 'Is the number of degrees in a right angle.';
        break;
    case 91:
 $clue = 'Is the smallest pseudoprime in base 3.';
        break;
    case 92:
 $clue = 'Is the number of different arrangements of 8 non-attacking queens on an 8×8 chessboard.';
        break;
    case 93:
 $clue = 'Is 333 in base 5.';
        break;
    case 94:
 $clue = 'Is a Smith number.';
        break;
    case 95:
 $clue = 'Is the number of planar partitions of 10.';
        break;
    case 96:
 $clue = 'Is the smallest number that can be written as the difference of 2 squares in 4 ways.';
        break;
    case 97:
 $clue = 'Is the smallest number with the property that its first 3 multiples contain the digit 9.';
        break;
    case 98:
 $clue = 'Is the smallest number with the property that its first 5 multiples contain the digit 9.';
        break;
    case 99:
 $clue = 'Is a Kaprekar number.';
        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
65 65
66 66
67 67
68 68
69 69
70 70
71 71
72 72
73 73
74 74
75 75
76 76
77 77
78 78
79 79
80 80
81 81
82 82
83 83
84 84
85 85
86 86
87 87
88 88
89 89
90 90
91 91
92 92
93 93
94 94
95 95
96 96
97 97
98 98
99 99
";

  if ($across > 9) {
    $waswikistuff=$wikistuff;
    $wikistuffs=explode("\n", $wikistuff);
    while (sizeof($wikistuffs) < ($across * $across)) {
      $wikistuff.=$waswikistuff;
      $wikistuffs=explode("\n", $wikistuff);
    }
  }

  $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='./colouring_in_was_numbers_guessing_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 (isset($_GET['notice'])) {
  //$templategame=str_replace('>-</span>', '><a style="text-decoration:none;cursor:pointer;" title="Stop showing where contemplated as well." onclick="location.href=' . "'#no_tice=y'" . ';">-</a><a style="text-decoration:none;cursor:pointer;" title="Show where contemplated as well." onclick="location.href=' . "'#notice=y'" . ';">+</a></span>', $templategame);
  //} else {
  //$templategame=str_replace('>-</span>', '><a style="text-decoration:none;cursor:pointer;" title="Show where contemplated as well." onclick="location.href=' . "'#notice=y'" . ';">+</a><a style="text-decoration:none;cursor:pointer;" title="Stop showing where contemplated as well." onclick="location.href=' . "'#no_tice=y'" . ';">-</a></span>', $templategame);
  //}
  $arow='';
  $rownum=$across;
  $tdnum=($across * $across);
  if (1 == 5) {
  for ($ijk=(1 + $across); $ijk<=$thnum; $ijk++) {
    if (($ijk % $across) == 1) {
      $rownum=(1 + ((-1 + $ijk) / $across));
      $arow='<tr id=tr' . $rownum . '><th id=th' . $rownum . '1>' . $ijk . '</th></tr>';
    } else if (($ijk % $across) == 0) {
      $arow=str_replace('</tr>', '<th id=th' . $rownum . $across . '>' . $ijk . '</th></tr>', $arow);
      $templategame=str_replace('</table>', $arow . '</table>', $templategame);
      $arow='';
    } else if (($ijk % $across) > 1) {
      $arow=str_replace('</tr>', '<th id=th' . $rownum . ($ijk % $across) . '>' . $ijk . '</th></tr>', $arow);
      $templategame=str_replace('</table>', $arow . '</table>', $templategame);
      $arow='';
    }
  }
  $templategame=str_replace('</table>', $arow . '</table>', $templategame);
    //file_put_contents('xx.html', $templategame);
  }
  
  if ($across != $across || 1 == 1) {
    //$across=99;
    $irws=1;
    $sofartd='';
    $two=2;
    if (strpos($templategame, '</table>') !== false) {
    $wasih="<table" . explode("</table>", explode("<table", $templategame)[1])[0] . "</table>";
    $templategame=str_replace($wasih, '', $templategame);
    }
    if (strpos($templategame, '</p>') !== false) {
    $wasih='#f0f0f0;">' . explode('#f0f0f0;">', explode("</p>", $templategame)[0])[-1 + sizeof(explode('#f0f0f0;">', explode("</p>", $templategame)[0]))] . "</p>";
    $templategame=str_replace($wasih, '#f0f0f0;width:20px;height:25px;">&#x1f58c;</p>', $templategame);
    }
    $wasih='<div id="target"' . explode("</div>", explode('<div id="target"', $templategame)[1])[0]; // . "</div>";
    $origih=$wasih;
    
    $newih="<table border=1 cellpadding=0 cellspacing=0 style=width:90%;height:53%; id=mytable><tbody></tbody></table>";
    $cacross='' . $across;
    $iacross=strlen($cacross);
    $thisid='';
    $rowsofar='';
    $thistdid='td';
    $thistdidsuffix='';
    $ttcnt=1;
    
    for ($thisrow=1; $thisrow<=$across; $thisrow++) {
      $cthisrow='0000000000' . $thisrow;
      $rowsofar='<tr style="width:100%;" id=tr' . substr($cthisrow, -$iacross, $iacross) . '></tr>';
      $thistdid='td' . substr($cthisrow, -$iacross, $iacross) . '_';
      for ($thiscol=1; $thiscol<=$across; $thiscol++) {
        $cthiscol='0000000000' . $thiscol;
        $thistdidsuffix='' . substr($cthiscol, -$iacross, $iacross) . '';
        $rowsofar=str_replace('></tr>', '><td class="tdone" id=' . $thistdid . $thistdidsuffix . ' style=text-align:center; data-answer=' . $ttcnt . '></td></tr>', $rowsofar);
      }
      $newih=str_replace('</tbody>', $rowsofar . '</tbody>', $newih);
    }
    
    //echo strlen($newih);
    //exit;
    
    //file_put_contents('x.html', str_replace($origih, $wasih, $templategame));
    $templategame=str_replace($origih, str_replace('Drop Zone', 'Canvas Below&nbsp;&nbsp;<input onchange="document.getElementById(' . "'source'" . ').style.backgroundColor=this.value;" onblur="document.getElementById(' . "'source'" . ').style.backgroundColor=this.value;" type=color title="Pen colour" value="#f0f0f0" id=ddcolour></input>&nbsp;<input style=display:inline-block;width:60px; type=number title="Optional countdown in seconds before dragover takes effect from when you start dragging" id=dragoverdelay value=0 min=0></input><input style=display:none;background-color:yellow;width:60px; type=number title="Countdown in seconds before dragover takes effect from when you start dragging" id=dragovercountdown value=0 min=0></input>&nbsp;&nbsp;<input onblur=acrossf(this,' . $across . '); style=display:inline-block;width:60px;background-color:orange; type=number title="Number of squares across and down" id=numacross value=' . $across . ' min=10 max=499></input>&nbsp;&nbsp;<input onblur=fontweightf(this,' . $fontweight . '); style=display:inline-block;width:60px;background-color:lightblue; type=number title="Number of squares of brush stroke" id=numfontweight value=' . $fontweight . ' min=1 max=2></input>&nbsp;&nbsp;<button style=display:inline-block; title="Share via Email" id=semail onclick="doemail(' . "''" . ');">&#128231;</button>&nbsp;<button style=display:inline-block; title="Share via SMS" id=ssms onclick=dosms();>&#128223;</button>', $origih) . $newih, $templategame);
    $templategame=str_replace('1px dotted green', '0px dotted transparent', $templategame); 
    $justforcolouringin=" var rectis=document.getElementById('mytable').getBoundingClientRect(); document.getElementById('mytable').width='' + rectis.height + 'px'; setTimeout(function(){ document.getElementsByTagName('h3')[0].style.display='none'; document.getElementsByTagName('h4')[0].style.display='none'; document.getElementsByTagName('h3')[1].style.display='none';  }, 7000); cibihcheck(); ";
    $templategame=str_replace('<body onload="', '<body onload="' . $justforcolouringin, $templategame); 
  }  
    //file_put_contents('xxx.html', $templategame);
  
  $templategame=str_replace('</h4>', '<input type=hidden id="callback" value="' . $callbackurl . '"></input></h4>', $templategame);
  $templategame=str_replace('>Game<', '>Colouring In <font style=display:none;>Numbers Guessing Game</font> Game <font size=1>... drag pen over canvas to colour in</font><', $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);
  }
  
  //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  
    if (1 == 6) {
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' style=text-align:center; data-answer="' . $theans . '">' . $theans . '<', $templategame);
    $templategame=str_replace('">' . $thepsource . '<', 'text-align:center;" data-answer="' . $theans. '">' . $thecluewording . '<', $templategame);
    for ($i=1; $i<=99; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' style=text-align:center; data-answer="">' . $i . '</td', $templategame);
       }
    }
    }
    $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    echo $templategame;
  } else { // description up top and designations in drop zone
    if (1 == 6) {
    $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);
    echo $templategame;
  }
  

?>
