HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

My online hangman solver

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
solverhangmanonline

Problem

I've created a simple hangman solver and although I'd consider myself proficient in webdev I am by no means an expert. The trick with this is that since HTTP is stateless, I can't really think of a better way to look words up than to start from scratch each time (and lookup is performed with ajax EVERY time the input changes). So any special data structures would need to be created from the input each time, an overhead which isn't really worth it.

I'm especially interested in any ways to optimize my backend lookup code:

/*  lookup.php  */

$length = $_POST['length'];
$proto = $_POST['proto'];
$dead = $_POST['dead'];
$live = str_replace("*", "", $proto);

// check that $length is number

// load appropriate dictionary into $dict
$dict = file("dicts/" . $length . "_letters.txt", FILE_IGNORE_NEW_LINES);
$words = array();

// loop through each word in dictionary
foreach($dict as $word){
    $flag = true;
    // check that word matches proto
    for($i=0; $i $v){
    $letters[$l] = $letters[$l] / $num_words;
}

//remove letters with zero frequency
foreach($letters as $l=>$v){
    if($v == 0 || $v == 1){
        $letters = array_diff($letters, array($l=>$v));
    }
}
unset($l);

$ret = array("words"=>$words, "letters"=>$letters);
echo json_encode($ret);


Here is the JS on my front end

```
var length;
var response;
var blocking = false;

$(document).ready(function(){
setProto();
var height = Math.ceil($(window).height() / 40);
var width = Math.ceil($(window).width() / 40);

document.getElementById("length").oninput = function(){
if( !isNaN(parseInt( $("#length").val() )) ){
length = parseInt($("#length").val());
}
setProto();
evaluate();
}
document.getElementById("proto").oninput = evaluate;
document.getElementById("dead").oninput = evaluate;

$(".headings h2").click(function(e){
dataSelect(e.target);
});

$(".reset button").click(reset);
});

var evaluate = function(){

Solution

With a common english dictionary wordlist being 1.2MB in size, i would suggest loading the wordlist from the server and solving it in javascript.

Aside of that: a more efficient way of solving this would be to construct a regex from your input, then looping over the wordlist.

if (strlen($dead)) {
    $letterMask = '[^'.$dead.']';
} else {
    $letterMask = '.'; //match any char
}

$regex = '/' . str_replace('*', $letterMask, $proto).'/'; // 
$words = [];
foreach ($dict as word) {
    if (preg_match($regex, $word)) {
        $words[] = $word;
    }
}

Code Snippets

if (strlen($dead)) {
    $letterMask = '[^'.$dead.']';
} else {
    $letterMask = '.'; //match any char
}

$regex = '/' . str_replace('*', $letterMask, $proto).'/'; // 
$words = [];
foreach ($dict as word) {
    if (preg_match($regex, $word)) {
        $words[] = $word;
    }
}

Context

StackExchange Code Review Q#129790, answer score: 3

Revisions (0)

No revisions yet.