1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to create a Random, Sequencial lvl selection algorithm?

Discussion in 'JavaScript' started by Alexa Stanese, May 4, 2014.

  1. #1
    I would like to create a phonegap application with random lvl's and a repetitive lvl selection. I need to do this in jquery, jquery mobile and localstorage.

    I'm a beginner in javascript so I'll just post my exact need and also what I thought till now... The app is simmilar to 4 pics 1 word (for each lvl I have some pictures and I have to guess a word that is found in each of those 4 pics) The difference is that in my app I have diferite types of lvl's. (for example one is normal, one have the pics black and white, one have a counter, one have more pictures, once needs to guess wore words from that picture etc.)

    So let's say I have lvl's untill 200.

    • for lvl 1 to lvl 50 I need some lvls that have the same functionality but are random

    • for 50-100 I need another type of lvl that interleaves with the other one (ex: lvl 51: type a, lvl 52: type b, lvl 53: type a, lvl 54: type b etc... )

    • for 100-150 I need another type of lvl that interleaves with the other ones (ex: lvl 101: type a, lvl 102: type b, lvl 103: type c, lvl 104: type a, lvl 105: type b etc.)

    • for 150-200 I add another type and they repeat the same...

    • and for the rest lets say I dont need another type of lvl just these 4 to repeat themselves
    I want the algorithm to be flexible so if I want on lvl 1000 to add another type (ex type e) to implement it easy.

    The real problem is that as all apps like this one needs to be updated and to receive more lvls. I can't really think of how can I add those lvl's very easy without messing everything up AND keeping track of what lvls I passed and what not.

    For now I have a lvl variable stored in localstorage representing the lvl. For each lvl I start it actually goes within the lvl folder and shows up the images from that folder. So I would like to keep this thinking for all the rest of lvls.

    So heres what I thought for now.

    In the start of the app:

    A variable version wich checks if the version is the same as the one from the localstorage and if not than update the lvl array.

    //update the lvl array something like a loooong for wich adds the lvls that are missing from the old version to the new one. (I mean a literally long written for) I think the for is something very long:

    a=101a,a[i+1]=102b, a[i+2]=103c, a[i+3]=104a... And I think I have to write down each lvl in that for... (that 101 is actually an a type, and that 102 is actually a b type etc.)

    after the update is done

    I can shuffle that lvl array (ex.... 44a,102b, 34a, 56b, 103c.....) and before each start of lvl I search the first a type that is in range of 1-50 if my current lvl is <50 or 50-100 if my current lvl is between 50 and 100 etc. and display it. (because it'll be nice the lvl 948 if it's a type a to be a little harder than lvl 1 type a. Harder I mean through a more difficult picture) If the user completes that lvl, an x should be added to the start of the lvl name or simply to replace that lvl name with something. Next search the first b. Next search the first c etc.

    This is what I thought for now. I not sure if its the right thinking or if I just harden the it as a beginner. I have really no idea for now how to implement it exactly but if somebody can help me with a complete example wich I can implement further in my app I'll very gratefull to that guy :)

    Thank you!
     
    Alexa Stanese, May 4, 2014 IP
  2. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Well, each "type" has to be created as image files on disk and an accompanying puzzle for the enduser..

    So your levels are limited by how many "types" are available in the system.

    How i would do this is store the types as polymorphic treestructures on disk..
    For instance:
    .../siteData/types/type_a/images/1.jpg
    .../siteData/types/type_a/images/2.jpg
    .../siteData/types/type_a/images/3.jpg
    .../siteData/types/type_a/images/4.jpg

    .../siteData/types/type_b/images/1.jpg
    .../siteData/types/type_b/images/2.jpg
    .../siteData/types/type_b/images/3.jpg
    .../siteData/types/type_b/images/4.jpg
    .../siteData/types/type_b/images/5.jpg
    .../siteData/types/type_b/images/6.jpg

    ok so you get the picture. later you could add .../siteData/types/type_a/type.json and .../siteData/types/type_b/type.json which you then read in in PHP using $types = Array(); $types[] = json_decode(file_get_contents('.../siteData/types/type_a/type.json'),true);
    I have a routine for you which can find files in PHP recursively;
    
    function getFilePathList ( 
    //TODO: relatively untested complicated function, might be buggy
       
        $path,                                // start path 
        $recursive = false,                    // if true, we also process any subdirectory.
        $fileSpecRE = "/.*/",                // Regular Expression file specs - will be matched against any filename found.
        // ^-- this is NOT the same as normal "somefile-*.something.extension" type wildcards. see example above.
        $fileTypesFilter = array (),        // array (int=>string (filetype() result) ==== int=>"file"|"dir" )
        $ownerFilter = array (),            // array (int=>string (username) ); only return files owned by someone in $ownerFilter.
        $fileSizeMin = null,                // If >=0, any files returned must have a minimum size of $fileSizeMin bytes.
        $fileSizeMax = null,                // same as above, but maximum size
    
        /* all date parameters below must be provided in the mktime() format. */
        $aTimeMin = null,                    // calls fileatime(). Read The Friendly Manual. http://www.php.net/manual/
        $aTimeMax = null,                    //    ^- access includes a program reading from this file.
        $mTimeMin = null,                    // calls filemtime(). RTFM.
        $mTimeMax = null,
        $cTimeMin = null,                    // calls filectime(). rtfm.
        $cTimeMax = null,
        /*    on windows XP, cTime = creation time; mTime = modified time; aTime = access time. 
            I also noted some BUGS in retrieving these dates from my system.
        */
        $listCall = ""                        // interesting feature; lets you include results from any informational file function(s).
    /*    TODO : fix $*Date* parameter handling, 
        returns an array consisting of all files in a directory structure, filtered by the parameters given.
        results are returned in directory order. if ($recursive) then subdirectory content is listed before file content.
        OKAY, this one is monolithic :)   But very usefull, so an exception to the rule is granted here.
    example: 
        htmlDump (getFilePathList("c:/dat/web", true, "/.*\.php$|.*\.php\d$|.*\.inc$/",
            array(), array(), null, null, null, null, null, null, null, null,
            "\"ctime=\".date (\"Y/m/d H:m:s\", filectime (\$filepath)).".
            "\" - atime=\".date (\"Y/m/d H:m:s\", fileatime (\$filepath)).".
            "\" - mtime=\".date (\"Y/m/d H:m:s\", filemtime (\$filepath)).".
            ";"
            ));
        -== this returns an array with complete filepaths of all files under c:/dat/web, that have an extension like
            *.php, *.php3, *.php4 or *.inc. 
            for my system, it returns:
                array(4) {
                 [0]=>
                 string(115) "c:/dat/web/index.php - [listCall=ctime=2003/05/11 18:05:26 - atime=2003/05/16 05:05:44 - mtime=2003/05/16 05:05:44]"
                 [1]=>
                 string(122) "c:/dat/web/preProcessor.php - [listCall=ctime=2003/05/15 16:05:55 - atime=2003/05/16 04:05:47 - mtime=2003/05/15 17:05:35]"
                 [2]=>
                 string(116) "c:/dat/web/source.php - [listCall=ctime=2003/05/11 18:05:26 - atime=2003/05/16 04:05:47 - mtime=2003/04/28 13:04:07]"
                 [3]=>
                 string(117) "c:/dat/web/sources.php - [listCall=ctime=2003/05/11 18:05:26 - atime=2003/05/16 04:05:50 - mtime=2003/05/12 00:05:22]"
    }
            in this example, the $listCall is kinda complicated. but only to show it's power.
            if you're having trouble debugging your $listCall, turn on the relevant htmlDump() call in this function.
       
    another example:
        htmlDump (getFilePathList("c:/dat/web", false, "/.*\.php$|.*\.php\d$|.*\.inc$/", 
            array(), array(), null, null, null, null, null, time()-mktime (0,0,0,0,1,0));
        -== this returns, for my system, all *.php,*.php3/4,*.inc files in c:/dat/web, that havent changed since 24 hours ago:
    */
    
    ) {
    
    
        $result = array();
        //if (!in_array("file",$fileTypesFilter)) $fileTypesFilter[count($fileTypesFilter)]="file";
        //htmlOut (" --== $path ==--");
        if ($path[strlen($path)-1]!="/") $path.="/";
        if ($handle = opendir($path)) {
            /* This is the correct way to loop over the directory. */
            while (false !== ($file = readdir($handle))) { 
            if ($file != "." && $file != "..") { 
           
                $pass = true;
                $ft = filetype($path.$file); 
                if (!in_array ($ft, $fileTypesFilter)) $pass = false;
                // htmlDump ($ft, "filesys");
                if ($ft=="dir") $filepath = $path.$file."/"; else $filepath = $path.$file;
                //htmlDump ($filepath);
                //htmlDump ($fileSpecRE."  ---- ".$file);
                if ($pass) $pass = preg_match ($fileSpecRE, strToLower($file));
                //htmlDump ($pass);
                if ($pass && count($ownerFilter)>0) {
                    $fo = fileowner ($filepath);
                    if ($fo!=false) {
                        $fo = posix_getpwuid($fo);
                        if (!in_array ($fo, $ownerFilter)) $pass=false;
                    } else {
                    //couldn't retrieve username. be strict & safe, fail.
                        $pass = false;
                    }
                }
                if ($pass && isset($fileSizeMin)) if (filesize ($filepath) < $fileSizeMin) $pass=false;
                if ($pass && isset($fileSizeMax)) if (filesize ($filepath) > $fileSizeMax) $pass=false;
    
                if ($pass && isset($aTimeMin)) 
                    $pass=evalDate ("fileatime", $filepath, ">=", $aTimeMin, "aTimeMin");
                if ($pass==true && isset($aTimeMax)) 
                //    ^- if ($stringValue) == always true!, 
                //        so explicitly check for boolean true result after calling 
                //        functions that may return an (error) string.
                    $pass=evalDate ("fileatime", $filepath, "<=", $aTimeMax, "aTimeMax");
                if ($pass==true && isset($mTimeMin))
                    $pass=evalDate ("filemtime", $filepath, ">=", $mTimeMin, "mTimeMin");
                if ($pass==true && isset($mTimeMax))
                    $pass=evalDate ("filemtime", $filepath, "<=", $mTimeMax, "mTimeMax");
                if ($pass==true && isset($cTimeMin))
                    $pass=evalDate ("filectime", $filepath, ">=", $cTimeMin, "cTimeMin");
                if ($pass==true && isset($cTimeMax))
                    $pass=evalDate ("filectime", $filepath, "<=", $cTimeMax, "cTimeMax");
    
                if ($pass==true) {
                    //htmlOut ("PASSED");
                    $r = "";
    
                    $ev = "\$r = $listCall";
                    //htmlDump ($ev);
                    if (!empty($listCall)) eval ($ev);
                    $idx = count ($result);
                    if (!empty($r)) $r = " - [listCall=$r]";
                    $result[$idx] = $filepath.$r;
                }
                if (is_string($pass)) {
                    //htmlOut ("PASSED - checks failed");
                    $result[count($result)] = "[$pass]".$filepath;
                }
                if ($recursive && $ft=="dir") {
                    $subdir = getFilePathList ($filepath,$recursive, $fileSpecRE, 
                        $fileTypesFilter, $ownerFilter, $fileSizeMin, $fileSizeMax, 
                        $aTimeMin, $aTimeMax, $mTimeMin, $mTimeMax,
                        $cTimeMin, $cTimeMax, $listCall);
                    array_splice ($result, count($result)+1, 0, $subdir);
                }
            }
            }
        }
        //htmlDump ($result, "result");
        return $result;
    }
    
    Code (markup):
    This way you can build up in PHP a list of the details of your types available to the game, and export them automatically to javascript, where your app could be shaped like this:

    
    var game = {
        types : {
            type_a : {
                explanation : "what to do with type_a (as an end-user) [comes from type.json]",
                images : [
                    "1.jpg",
                    "2.jpg",
                    "3.jpg",
                    "4.jpg"
                ]
            },
            type_b : {
                ...
            }
        },
       
        startApp : function () {
            alert ('There are '+game.getMaximumLevel()+' levels in this game');
        },
       
        getMaximumLevel : function () {
            var typeCount = 0;
            for (var k in game.types) {
                typeCount++;
            }
            return typeCount * 50;
        },
       
        getType : function (level) {
            var 
            typeChosen = Math.random(level/50),
            typeCount = 0;
           
            for (var type in game.types) {
                typeCount++;
                if (typeCount==typeChosen) game.playWithType(game.types[type]);
            }
        },
       
        playWithType : function (type) {
            jQuery('#explanation').html(type.explanation);
            // good luck and have fun with the rest :)
        }
    
    };
    
    Code (markup):
     
    seductiveapps.com, Nov 16, 2014 IP