<?php
// what_is_the_english_word.php
// RJM Programming
// February, 2022
set_time_limit(58);

$score=0;
$goes=0;
$blurb='';
$dblurb="Score: 0/0";
$totalj=0;
$fortyeight="48";
$fortyeighti="";
$fortyeightj="";
$ob="";
$sh3="";
$aic="Am I Correct?";
if (isset($_GET['fortyeight'])) {
  $fortyeight=$_GET['fortyeight'];
  $fortyeighti="<input type=hidden name=fortyeight value='" . $fortyeight . "'></input>";
  $fortyeightj="&fortyeight=" . $fortyeight;
  $aic="Correct?";
  $ob=" onblur=\" if (this.value.trim() != '') { document.getElementById('subm').click(); } \" ";
  $sh3=" style='display:none;'";
}

if (isset($_GET['numplayers'])) {
  $preifs="<html>
<head><title>What Is The English Word - RJM Programming - February, 2022</title>
</head><body style='background-color:yellow;'><table style='width:100%;' border=20><tr><th colspan=2>Player</th></tr><tr><th>Number</th><th>Game</th></tr></table></body></html>";
  for ($ih=1; $ih<=$_GET['numplayers']; $ih++) {
     $preifs=str_replace("</table>", "<tr><td style=text-align:center;>" . $ih . "</td><td><iframe src='./what_is_the_english_word.php?fortyeight=24' style='width:100%;height:200px;'></iframe></table>", $preifs); 
  }
  echo $preifs;
  exit;
}

function ask() {
  global $score, $goes, $blurb, $fortyeightj;
  $letters=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
  $rl=rand(0,25);
  $lr=rand(5,12);
  $pr=rand(1,$lr);
  header('Location: ' . str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . explode('?',$_SERVER['REQUEST_URI'])[0] . '?position=' . $pr . '&length=' . $lr . '&letter=' . $letters[$rl] . '&score=' . $score . '&goes=' . $goes . $fortyeightj . '&blurb=' . urlencode($blurb))));
  exit;
}

if (isset($_GET['youranswer']) && isset($_GET['theanswer'])) {
  if (isset($_GET['score'])) { $score=$_GET['score'];  }
  if (isset($_GET['goes'])) { $goes=$_GET['goes'];  }
  if (isset($_GET['blurb'])) { $dblurb=str_replace('+',' ',urldecode($_GET['blurb']));  }
  $goes++;
  $blurb=str_replace('+',' ',urldecode($_GET['explanation'])) . "<font title='Thanks to The Free Dictionary, if you click' style=cursor:pointer;text-decoration:underline; onclick=\"window.open('//www.thefreedictionary.com/" . $_GET['theanswer'] . "', '_blank', 'top=100,left=100,width=500,height=600');  \" color=blue>" . $_GET['theanswer'] . "</font> ... Score: " . $score . "/" . $goes;
  if (trim(strtolower($_GET['youranswer'])) == trim(strtolower($_GET['theanswer']))) {
    $score++;
    $blurb="Congratulations! ... Score: " . $score . "/" . $goes;
  }
  ask();
} else if (isset($_GET['letter']) && isset($_GET['length']) && isset($_GET['position'])) {
  if (isset($_GET['score'])) { $score=$_GET['score'];  }
  if (isset($_GET['goes'])) { $goes=$_GET['goes'];  }
  if (isset($_GET['blurb'])) { $dblurb=str_replace('+',' ',urldecode($_GET['blurb']));  }
  $w14a=$_GET['letter'];
  $wzero="";
  $wz="";
  $dwz="_";
  for ($ii=1; $ii<=$_GET['length']; $ii++) {
    if ($ii == $_GET['position']) {
      $wzero.=$w14a;
      $wz.=$w14a;
    } else {
      $wzero.="0";
      $wz.=$dwz;
      if ($dwz == "_") { $dwz="-";  } else {  $dwz="_";  }
    }
  }
  $w10d="";
  $jj=0;
  while (strpos($w10d, ' ') === false) {
    $w10d = file_get_contents(str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=" . (1 + $_GET['length']) . "&max=" . (1 + $_GET['length']) . "&hastobe=" . $wzero . "&needclue=y&youllneverfindthis=y")));
    $jj++;
    $totalj++;
    if ($totalj > 100) { 
        exit; //header('Location: ' . str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . explode('?',$_SERVER['REQUEST_URI'])[0])));
    }
    if ($jj > 10) { ask(); }
    sleep(2);
  }
  
  echo "<html>
<head><title>What Is The English Word - RJM Programming - February, 2022</title>
</head><body style='background-color:yellow;' onload=\"  document.getElementById('youranswer').focus(); \">
<h1>What Is The " . $_GET['length'] . " Letter English Word With \"" . $_GET['letter'] . "\" at position " . $_GET['position'] . "?</h1>
<h3" . $sh3 . ">RJM Programming <a title='How many players?' style='cursor:pointer;text-decoration:underline;' onclick=\" var hm=prompt('How many players?','1'); if (hm == null) { hm=''; } if (hm.trim().replace('1','') != '') { location.href=document.URL.split('?')[0].split('#')[0] + '?fortyeight=24&numplayers=' + encodeURIComponent(hm);  } \">-</a> February, 2022</h3>
<h4>" . $dblurb . "</h4>
<form style='font-size:" . $fortyeight . "px;background-color:pink;padding: 5 5 5 5;width:100%;text-align:center;' method=GET action=./what_is_the_english_word.php>
<input type=hidden name=score value=" . $score . "></input>" . $fortyeighti . "
<input type=hidden name=theanswer value=" . explode(' ',$w10d)[0] . "></input>
<input type=hidden name=explanation value='" . "Sorry, last answer for " . $_GET['length'] . " Letter English Word With \"" . $_GET['letter'] . "\" at position " . $_GET['position'] . " with clue \"" . trim(substr($w10d, strlen(explode(' ',$w10d)[0])))  . "\" was " . "'></input>
<input type=hidden name=goes value=" . $goes . "></input><span style=font-size:" . $fortyeight . "px;>&nbsp;&nbsp;" . substr($w10d, strlen(explode(' ',$w10d)[0])) . " ... &nbsp;&nbsp;</span><input style=display:inline-block;font-size:" . $fortyeight . "px;width:30%; type=text" . $ob . " id=youranswer name=youranswer placeholder='" . $wz . "' value=''></input><span>&nbsp;&nbsp;</span><input id=subm onclick=\" this.style.cursor='progress'; document.body.style.cursor='progress';  \" style=display:inline-block;font-size:" . $fortyeight . "px; type=submit value='" . $aic . "'></input>
</form>
</body></html>
";
  
} else {
 ask();
}


?>
