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

$across=9;
$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
";

  $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='./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');
  $arow='';
  $rownum=4;
  $tdnum=99;
  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);
  
  if ($across != 3 && 1 == 1) {
    $irws=1;
    $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));
    $rowas=explode("</td>", $wasih);
    for ($irws=0; $irws<sizeof($rowas); $irws++) {
       if (((1 + $irws) % $across) == 0) {
          $sofartd.=$rowas[$irws] . '</td>';
          $wasih=str_replace($sofartd, $sofartd . '</tr><tr id=tr' . $two . '>', $wasih);    
          $sofartd='';
          $two++;
       } else {
          $sofartd.=$rowas[$irws] . '</td>';
       }
    }
    //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="callback" value="' . $callbackurl . '"></input></h4>', $templategame);
  $templategame=str_replace('>Game<', '>Numbers Guessing Game Game <font size=1>... drag to matching correct answer to score</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);
  
  //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="' . $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
    $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;
  }
  

?>
