<?php
// start_word_for_wordle_helper.php
// RJM Programming
// February, 2022
// Help suggest a start five letter word for Wordle

$sofar="";
$sugnum=10;
$tablebit="<table style='width:90%;' border=20 cellpadding=5 cellspacing=5><tr><th>Suggested Word</th><th>Weirdness Score</th></tr></table>";
$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';
$letscores='1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10';
$letscs=explode(',', $letscores);
$letss=explode(',', $letters);
$sortit=false;
$uniquifyit=false;
if (isset($_POST['others'])) {
if (strpos(strtolower(urldecode($_POST['others'])), 'sort') !== false) { $sortit=true; }
if (strpos(strtolower(urldecode($_POST['others'])), 'uniqu') !== false) { $uniquifyit=true; }
}
if (isset($_GET['others'])) {
if (strpos(strtolower(urldecode($_GET['others'])), 'sort') !== false) { $sortit=true; }
if (strpos(strtolower(urldecode($_GET['others'])), 'uniqu') !== false) { $uniquifyit=true; }
}
$arris=[];
$hsc="<head>
<title>Start five letter word for Wordle - RJM Programming - February, 2022</title>
<meta id='myviewport' name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.1, maximum-scale=8, user-scalable=yes' >
<style>
td {
text-align: center;
}

td:nth-child(2n+1) {
background-color: #f3f7fa;
}

th:nth-child(2n+1) {
background-color: #f3f7fa;
}

body {
background-color: #f7f7f7;
}
</style>
<script type='text/javascript'>
var wois=null;

function onl() {
if (window.self !== window.top) {
document.body.innerHTML = ('' + document.body.innerHTML).replace(/h1/g, 'h2');
}
}

function mixitup() {
var others=prompt('Space separate the word(s) \"sort\" to sort for Weirdness and/or \"unique\" for five letter words with no repeated letters. Optionally precede by that number of five letter word suggestions to create for use with Wordle, followed by a space character.', '');
var clickthis='';
if (others == null) { others=''; }
others=others.replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'');
others=others.toLowerCase().replace('sort',' sort').replace('uniqu',' uniqu');
if (others.trim() != '') {
var aothers=others.trim().split(' ');
if (aothers[0].replace(/0/g,'').replace(/1/g,'').replace(/2/g,'').replace(/3/g,'').replace(/4/g,'').replace(/5/g,'').replace(/6/g,'').replace(/7/g,'').replace(/8/g,'').replace(/9/g,'') == '') {
document.getElementById('defopt').value='-' + aothers[0];
document.getElementById('defopt').innerHTML=aothers[0];
document.getElementById('sugnum').value='-' + aothers[0];
others=others.replace(aothers[0],'').trim();
clickthis='mysub';
}
}
if (others.trim() != '') {
var sv=document.getElementById('sugnum').value;
document.getElementById('myform').innerHTML+='<input type=hidden name=others value=\"' + others + '\"></input>';
document.getElementById('sugnum').value=sv;
}
if (clickthis != '') { document.getElementById(clickthis).click(); }
}

</script>
</head>";

if (isset($_POST['sugnum'])) { $sugnum=str_replace('-','',str_replace('+',' ',urldecode($_POST['sugnum']))); }
if (isset($_GET['sugnum'])) { $sugnum=str_replace('-','',str_replace('+',' ',urldecode($_GET['sugnum']))); }

function ourstrtolower($insis) {
global $uniquifyit, $sofar;
$outsis=strtolower($insis);
if (strpos($sofar, ' ' . $outsis) !== false) { return ' '; }
if (!$uniquifyit) { $sofar.=' ' . $outsis . ' '; return $outsis; }
$okay=true;
for ($m=0; $m<(-1 + strlen($insis)); $m++) {
if (strpos(substr($outsis,(1 + $m)), substr($outsis,$m,1)) !== false) { $okay=false; }
}
if ($okay) { $sofar.=' ' . $outsis . ' '; return $outsis; }
return ' ';
}

for ($i=0; $i<$sugnum; $i++) {
$aword = ' ';
while ($aword == ' ') {
$aword=ourstrtolower(file_get_contents('http://www.rjmprogramming.com.au/PHP/surprise.php?min=6&max=6&youllneverfindthis=y'));
}
$iscore=0;
for ($j=0; $j<strlen($aword); $j++) {
for ($k=0; $k<sizeof($letss); $k++) {
if (substr($aword,$j,1) == $letss[$k]) { $iscore+=$letscs[$k]; }
}
}
if (!$sortit) {
$tablebit=str_replace('</table>', '<tr><td>' . $aword . '</td><td>' . $iscore . '</td></tr></table>', $tablebit);
} else {
array_push($arris, substr("0000000000" . $iscore, -10,10) . $aword);
}
}

if ($sortit) {
sort($arris);
for ($i=0; $i<$sugnum; $i++) {
$tablebit=str_replace('</table>', '<tr><td>' . substr($arris[$i],10) . '</td><td>' . ltrim(substr($arris[$i],0,10), '0') . '</td></tr></table>', $tablebit);
}
}

echo "<html>" . $hsc . "<body onload=onl();><h1><span>Start five letter word for <a target=_blank href='//www.powerlanguage.co.uk/wordle/'>Wordle</a> ... </span><input onclick=mixitup(); title='Allow for Unique Letters, Weirdness Sorting' type=button style=display:inline-block; value='via '></input></span><form id=myform style=display:inline-block; action=./start_word_for_wordle_helper.php method=POST><input id=mysub type=submit value=Submit style=display:none;></input><select style=display:inline-block; id=sugnum name=sugnum onchange=document.getElementById('mysub').click();><option id=defopt value=-" . $sugnum . ">" . $sugnum . "</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option><option value=6>6</option><option value=7>7</option><option value=8>8</option><option value=9>9</option><option value=10>10</option><option value=20>20</option><option value=25>25</option><option value=50>50</option><option value=100>100</option></select><span> suggestions ...</span></h1><h3>RJM Programming ... February, 2022</h3><h4>Suggestions below (bigger associated number, the weirder is the suggestion) ...</h4><br>" . $tablebit . "</body></html>";

?>