Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Member Mist's Avatar
    Join Date
    Dec 2002
    Posts
    61
    Member #
    395
    Two members from vbulletin.org kindly helped me code a script for a little assignment at college. It basically is like a game of scrabble. It pulls 100 random words from the 'word' table and then calculates how many times each letter is used and then below each letter is assigned a points value and then a final column shows how many points the word requires. All appeared to be working well but after close inspection something is not right. Occasionally it simply doesn't calculate the number of times a letter occurs and therefore does not output the correct data.

    Here's the script and if someone can help me sort the problem out i'd be most grateful:

    PHP Code:
    <?php
    require('global.php');
    //get string $word from db

    $words $DB_site->query('SELECT title FROM word ORDER BY RAND(NOW()) LIMIT 100');

    // Start output table..
    echo '<table border=1><tr><td align="center">Word</td>';
    for (
    $i 0$i <= 25$i++) {
        echo 
    '<td align="center">' chr($i ord('A')). '</td>';
    }
    echo 
    '<td align="center">Wordscore</td></tr>';

    $totalscore 0;
    while (
    $word $DB_site->fetch_array($words)) {
    $wordstring strtolower($word['title']);

    $wordlength=strlen($word);

    for (
    $i 0$i <= $wordlength$i++) {
    $letters[$i]=substr ($wordstring,$i,1);
    }

    $wordscore=0;
    for (
    $i 0$i <= 25$i++) {
        
    $count[$i]=0;
    }

    for (
    $i 0$i <= $wordlength$i++) {
    switch(
    $letters[$i]) {
    case 
    a:
            
    $wordscore=$wordscore+3;
            
    $count[0]=$count[0]+1;
            break;
    case 
    b:
            
    $wordscore=$wordscore+2;
            
    $count[1]=$count[1]+1;
            break;
    case 
    c:
            
    $wordscore=$wordscore+2;
            
    $count[2]=$count[2]+1;
            break;
    case 
    d:
            
    $wordscore=$wordscore+1;
            
    $count[3]=$count[3]+1;
            break;
    case 
    e:
            
    $wordscore=$wordscore+1;
            
    $count[4]=$count[4]+1;
            break;
    case 
    f:
            
    $wordscore=$wordscore+1;
            
    $count[5]=$count[5]+1;
            break;
    case 
    g:
            
    $wordscore=$wordscore+1;
            
    $count[6]=$count[6]+1;
            break;
    case 
    h:
            
    $wordscore=$wordscore+5;
            
    $count[7]=$count[7]+1;
            break;
    case 
    i:
            
    $wordscore=$wordscore+2;
            
    $count[8]=$count[8]+1;
            break;
    case 
    j:
            
    $wordscore=$wordscore+4;
            
    $count[9]=$count[9]+1;
            break;
    case 
    k:
            
    $wordscore=$wordscore+5;
            
    $count[10]=$count[10]+1;
            break;
    case 
    l:
            
    $wordscore=$wordscore+2;
            
    $count[11]=$count[11]+1;
            break;
    case 
    m:
            
    $wordscore=$wordscore+4;
            
    $count[12]=$count[12]+1;
            break;
    case 
    n:
            
    $wordscore=$wordscore+5;
            
    $count[13]=$count[13]+1;
            break;
    case 
    o:
            
    $wordscore=$wordscore+3;
            
    $count[14]=$count[14]+1;
            break;
    case 
    p:
            
    $wordscore=$wordscore+6;
            
    $count[15]=$count[15]+1;
            break;
    case 
    q:
            
    $wordscore=$wordscore+4;
            
    $count[16]=$count[16]+1;
            break;
    case 
    r:
            
    $wordscore=$wordscore+1;
            
    $count[17]=$count[17]+1;
            break;
    case 
    s:
            
    $wordscore=$wordscore+2;
            
    $count[18]=$count[18]+1;
            break;
    case 
    t:
            
    $wordscore=$wordscore+10;
            
    $count[19]=$count[19]+1;
            break;
    case 
    u:
            
    $wordscore=$wordscore+9;
            
    $count[20]=$count[20]+1;
            break;
    case 
    v:
            
    $wordscore=$wordscore+2;
            
    $count[21]=$count[21]+1;
            break;
    case 
    w:
            
    $wordscore=$wordscore+9;
            
    $count[22]=$count[22]+1;
            break;
    case 
    x:
            
    $wordscore=$wordscore+5;
            
    $count[23]=$count[23]+1;
            break;
    case 
    y:
            
    $wordscore=$wordscore+1;
            
    $count[24]=$count[24]+1;
            break;
    case 
    z:
            
    $wordscore=$wordscore+6;
            
    $count[25]=$count[25]+1;
            break;
    }
    }

    echo 
    '<tr><td>' $wordstring '</td>';
    for (
    $i 0$i <= 25$i++) {
        echo 
    '<td>' $count[$i] . '</td>';
    }
    echo 
    '<td>' $wordscore '</td></tr>';
    $totalscore += $wordscore;
    }
    echo 
    '<tr><td>Total</td><td colspan=26></td><td>' $totalscore '</td></tr></table>'
    ?>
    Regards

    - miSt

  2.  

  3. #2
    Senior Member
    Join Date
    Mar 2002
    Location
    Alabama sans the damn flag
    Posts
    263
    Member #
    38
    Would the count be off on a wordstring with length > 25, by any chance?

    It's always a dangerous idea to hardwire the dimensions on something that is dynamic.

    In all seriousness, I think more information is necessary. At the least, I'd like a word string that gives a kosher result and one that doesn't.
    DerFarm
    I talk to squirrels.
    Squirrels Answer.

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Also you get rid of that entire switch statement by using the ASCII value of the character in question.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    Member Mist's Avatar
    Join Date
    Dec 2002
    Posts
    61
    Member #
    395
    I myself don't know too much PHP so can someone try and help me out. It's to do with the counting of the letters. The calculation of the score works well but the actual letters aren't counted. So if someone can help out and fix it i'd be most grateful

    Thanks guys!

    - miSt

  6. #5
    Member Mist's Avatar
    Join Date
    Dec 2002
    Posts
    61
    Member #
    395
    I have had some breakthrough. It's something to do with only 6 letters being counted.

    So say if the word was:

    mischief

    It would only count 6 of the letters (not necessarily the first 6)

    Any ideas?

    Regards

    - miSt

  7. #6
    Member Mist's Avatar
    Join Date
    Dec 2002
    Posts
    61
    Member #
    395
    Hehe my dad fixed it after an hour of anaylsing the code...

    PHP Code:
    $wordlength=strlen($word); 
    The problem was with this line. It wasn't determing the wordlength from the actual word. It was returning it from a previously defined varaible called $word. Just simply adding an s to $word makes $words and then it calls the correct lenght of the word

    Voila lol!

    Regards

    - miSt


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:32 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com