#!/usr/bin/php

<?
// init
// random character value array
$score = array ("a"=>"20",
    
"b"=>"12312",
    
"c"=>"453",
    
"d"=>"3",
    
"e"=>"4653456",
    
"f"=>"2453",
    
"g"=>"12300",
    
"h"=>"9843",
    
"i"=>"4532",
    
"j"=>"34",
    
"k"=>"7765",
    
"l"=>"322",
    
"m"=>"21",
    
"n"=>"99",
    
"o"=>"322111",
    
"p"=>"9978634",
    
"q"=>"9433323445",
    
"r"=>"7",
    
"s"=>"56",
    
"t"=>"42",
    
"u"=>"342456",
    
"v"=>"67",
    
"w"=>"8975",
    
"x"=>"2344223",
    
"y"=>"98534",
    
"z"=>"123456789");


// functions

// -----------------------------------------------
function getWordValue($word) {
    
// must declare score array as global
    
global $score;
    
// get a word from the passed variable
    //print "orig word: $word";
    // chop the newline character from the end
    
$word chop($word);
    
//print "chopped: $word\n";
    // how many charcters are there in the word?
    
$length strlen($word);
    
//print "length of word: $length chars\n";
    // for each character in the string...
    
$wordval 0;
    for (
$m=0$m $length$m++) {
            
// get the character
                        
$char substr($word$m1);
                        
//print "char $m: $char - ";
                        // convert it to Score value
            
$char_score[$m] = $score[$char];
            
//print "$char_score[$m] in Score\n";
            
$wordval $wordval $char_score[$m];
            
    }
    return 
$wordval;
    
//print "wordvalue is $wordval\n";
}
// -----------------------------------------------




// -----------------------------------------------
// main program
print "Da Jumbla!\n";

// get words from user
$i 0;
while (
$i 5) {
    
fwrite(STDOUT"Enter word $i: ");
    
$jwords[$i] = fgets(STDIN);
    
$jwords[$i] = chop($jwords[$i]);
    
$jwordval[$i] = getWordValue($jwords[$i]);
    
//DEBUGprint "word - $jwords[$i] value - $jwordval[$i]\n";
    
print "WORD: $jwords[$i] ($jwordval[$i])\n";
    print 
"POSSIBLE: ";
    
// read in dictionary file
    
$words file("words.txt");
    foreach (
$words as $word) {
        
$wordval getWordValue($word);
        if (
$wordval == $jwordval[$i]) {
            print 
chop($word) . " ";
        }
    }
    print 
"\n";
    
$i++;
}




?>