Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Location
    Idaho
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can I use Javascript to create a game calculator?

    So, I don't really know much about Javascript, just what I've learned over the past few days, so I've only got the minimal basics down at the moment.

    I am trying to create a script to use as a 'breeding calculator' for a fish game (Fish With Attitude iOS to be specific). The way it works is fairly simple.

    There are 6 traits. Trait1, trait2, trait3, trait4, trait5, trait6.

    There are 1-trait fish, 2-trait fish, and 3-trait fish. Since there are only 6 traits, there are only 6 types of 1-trait fish.

    Then for the 2-trait and 3-trait fish, you breed up to get them. For example, breeding a 1-trait fish with another different 1-trait fish will create a 2-trait fish that contains the two traits of the parents.

    Trait1+Trait2=Hybrid1
    Trait1+Trait3=Hybrid2
    And so on....Then:
    Hybrid1+Trait4= A hybrid with 3 traits.

    Something to add, is the parents can create any variety of offspring that contain the parents traits. Example:
    Breeding Trait1+Trait2 does not guarantee Hybrid1. It has the chance of the offspring being Trait1, or Trait2, or Hybrid1. Any combination of 1-3 traits is possible.

    So breeding a fish that contains Trait1, 2, and 3 with a fish that contains Trait 4, 5, and 6 can give any possible combination of offspring.

    So as far as I can see, the script sounds like it would be super simple to create, right?
    Code:
    I prompt for what the users fish are...
    var fishOne=prompt("What is the first parents breed?");
    var fishTwo=prompt("What is the second parents breed?");
    var fishOne=fishOne.toUpperCase();
    var fishTwo=fishTwo.toUpperCase();
    
    /*User enters the types of the parents.
    
    I'm assuming I will have to have an array containing all possibilities involved here.*/
    var NICE=[1,0,0,0,0,0];
    var MEAN=[0,2,0,0,0,0];
    var BRAVE=[0,0,3,0,0,0];
    var SHY=[0,0,0,0,4,0];
    var SMART=[0,0,0,0,5,0];
    var CREATIVE=[0,0,0,0,0,6];
    var CRAZY=[1,2,0,0,0,0,];
    var FLIRTY=[1,0,3,0,0,0,];
    var PRETTY=[1,0,0,4,0,0,];
    var FUNNY=[1,0,0,0,5,0,];
    var CLOWN=[1,0,0,0,0,6,];
    var TOUGH=[0,2,3,0,0,0,];
    var REBEL=[0,2,0,4,0,0,];
    var GREEDY=[0,2,0,0,5,0,];
    var PAPARAZZI=[0,2,0,0,0,6,];
    var DREAMY=[0,0,3,4,0,0,];
    var COOL=[0,0,3,0,5,0,];
    var CHEERLEADER=[0,0,3,0,0,6,];
    var NERDY=[0,0,0,4,5,0,];
    var DANCER=[0,0,0,4,0,6,];
    var ARTIST=[0,0,0,0,5,6,];
    var ADVENTURE=[1,2,3,0,0,0,];
    var GOSSIP=[1,2,0,4,0,0,];
    var SALESMAN=[1,2,0,0,5,0,];
    var MOVIE STAR=[1,2,0,0,0,6,];
    var PARTY=[1,0,3,4,0,0,];
    var PRESIDENT=[1,0,3,0,5,0,];
    var ROCK STAR=[1,0,3,0,0,6,];
    var SECRET AGENT=[1,0,0,4,5,0,];
    var MODEL=[1,0,0,4,0,6,];
    var ASTRONAUT=[1,0,0,0,5,6,];
    var EVIL=[0,2,3,4,0,0,];
    var ROBBER=[0,2,3,0,5,0,];
    var PIRATE=[0,2,3,0,0,6,];
    var MAD SCIENTIST=[0,2,0,4,5,0,];
    var NINJA=[0,2,0,4,0,6,];
    var POP STAR=[0,2,0,0,5,6,];
    var HOBO=[0,0,3,4,5,0,];
    var WIZARD=[0,0,3,4,0,6,];
    var COWBOY=[0,0,3,0,5,6,];
    var DESIGNER=[0,0,0,4,5,6,];
    Where do I go from here?

    So if user enters "Pretty" as fishOne, and "Nice" as fishTwo, I want to return the offspring possibilities, which would be all fish that contain any variety of the traits in the arrays of Pretty and Nice.

    The extent of my knowledge on JS at the moment is function, arrays, booleans, ifs and elses, !, ||, &&, switch.....That's about it for now. Anything else that I'll need to add to this script, I'm going to have to google to figure it out.

    Any help is appreciated. Thanks!
    Last edited by MrsMorris; 11-02-2012 at 11:14 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Ummmm...yes, you could use Java.

    But I don't really think you care if you can or not.

    I think what you are really interested in is JavaSCRIPT.

    Java and JavaScript are VERY different languages, despite the similarity of name.

    Anyway, you have a good starting point there, but you don't want to do it *QUITE* that way.

    Instead of
    Code:
    var NICE=[1,0,0,0,0,0];
    var MEAN=[0,2,0,0,0,0];
    var BRAVE=[0,0,3,0,0,0];
    ...
    You need to start with
    Code:
    var availableBreeds = {
        NICE : [1,0,0,0,0,0],
        MEAN : [0,2,0,0,0,0],
        BRAVE : [0,0,3,0,0,0],
        ...
    };
    But you do *NOT* want to use prompt( ). Very primitive. Very ugly.

    Instead, use HTML and a <form>.

    Example:
    Code:
    <form id="myForm">
    <input name="parentBreed1"/>
    <input name="parentBreed2"/>
    ...
    And then the coding to find the array that matches a breed is easy:
    Code:
    var theForm = document.getElementById("myForm");
    var traitArray1 = availableBreeds[ theForm.parentBreed1.value.toUpperCase() ];
    var traitArray2 = availableBreeds[ theForm.parentBreed2.value.toUpperCase() ];
    At this point, though, you are just going to have to start figuring out what your algorithm(s) is/are and then convert the algorithm(s) to code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Location
    Idaho
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry, I literally went from 0-knowledge to just a few things, didn't know there was a difference.

    Didn't know I could make the list like that. Awesome.

    Would the rest just be a giant mess of ifs/ands/elses I need to manually type out? Or is there some way I can make it generate the list of possibilities on it's own?

    Also, so when the user enters the breed, it would be assigned one of the arrays as values? (If they put 'nice', then fishOne should be equal to [1,0,0,0,0,0], or am I not understanding this correctly?

    I suppose if I'm going to be manually entering in all the answers, then I don't even need that bulky list of traits, I can just manually write out the answer to every possible combination entered.....That sounds like fun.

    Sorry if I sound like a total moron. @_@

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Looking at this again, I think you have it a little too complex.

    I don't see any reason to use this:
    Code:
    var availableBreeds = { 
        NICE : [1,0,0,0,0,0], 
        MEAN : [0,2,0,0,0,0], 
        BRAVE : [0,0,3,0,0,0], 
        ... 
    };
    simplify!
    Code:
    var availableBreeds = { 
        NICE : [1], 
        MEAN : [2], 
        BRAVE : [3], 
        ... 
        EVIL : [2,3,4],
        ROBBER : [2,3,5],
        ...
    };
    That is, ONLY list the numbers of the traits the the given name has. Don't put in any zeroes.

    Now, to combine two parents, you would do something like this:
    Code:
    var breed1 = availableBreeds["...some name..."];
    var breed2 = availableBreeds["...some name..."];
    
    var combos = breed1.concat(breed2).sort();
    So if you combined, say, [1,3,4] wiith [2,4] then after the above code your combos array would contain [1,2,3,4,4]

    You could run through that array removing the duplicates to get [1,2,3.4] and from that you could pick all possible combinations (according to whatever rules you use) to get various arrays:
    [1,2]
    [1,3]
    [1,4]
    [2,3]
    [2,4]
    [3,4]
    [1,2,3]
    etc.

    (I'm not clear on what your combining rules are, so you can work that out.)

    Then, finally, I would build an INVERTED table, thus:
    Code:
    var inverted = {
       "1" : "Nice",
       "2" : "Mean",
       "3" : "Brave",
       ...
       "234" : "Evil",
       "235" : "Robber",
       ...
    };
    And so, then, you could look up the name thus:
    Code:
        var breedArray = [2,3,5];  // example only
        var name = inverted[ "" + breedArray.join("") ];
    You don't have to build the inverted table manually. You can build it easily in code starting from your availableBreeds array, thus:
    Code:
    var inverted = [ ];
    for ( var bname in availableBreeds )
    {
        var val = new String( availableBreeds[bname].join("") );
        inverted[val] = bname;
    }
    **********
    Here, a demo to show it works:
    Code:
    <script>
    var availableBreeds = { 
        NICE : [1], 
        MEAN : [2], 
        BRAVE : [3], 
        EVIL : [2,3,4], 
        ROBBER : [2,3,5]
    };
    
    var inverted = [ ]; 
    for ( var bname in availableBreeds ) 
    { 
        var val = new String( availableBreeds[bname].join("") ); 
        inverted[val] = bname; 
    }
    
    document.write( "<hr>234 is " + inverted["234"] );
    document.write( "<hr>2 is " + inverted["2"] );
    document.write( "<hr>235 is " + inverted["235"] );
    </script>
    Last edited by Old Pedant; 11-03-2012 at 12:31 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    MrsMorris (11-03-2012)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    so when the user enters the breed, it would be assigned one of the arrays as values? (If they put 'nice', then fishOne should be equal to [1,0,0,0,0,0], or am I not understanding this correctly?
    Yes, except if you use my suggestion you would have
    Code:
    var fishOne = availableBreeds["NICE"]; // NICE comes from user input
    
    // and now you actually have
    // fishOne == [1]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •