View Full Version : Can I use Javascript to create a game calculator?

11-02-2012, 11:13 PM
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.

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?

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!

Old Pedant
11-02-2012, 11:35 PM
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

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

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>.


<form id="myForm">
<input name="parentBreed1"/>
<input name="parentBreed2"/>

And then the coding to find the array that matches a breed is easy:

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.

11-03-2012, 12:12 AM
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. :rolleyes:

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

Old Pedant
11-03-2012, 01:25 AM
Looking at this again, I think you have it a little too complex.

I don't see any reason to use this:

var availableBreeds = {
NICE : [1,0,0,0,0,0],
MEAN : [0,2,0,0,0,0],
BRAVE : [0,0,3,0,0,0],


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:

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:

(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:

var inverted = {
"1" : "Nice",
"2" : "Mean",
"3" : "Brave",
"234" : "Evil",
"235" : "Robber",

And so, then, you could look up the name thus:

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:

var inverted = [ ];
for ( var bname in availableBreeds )
var val = new String( availableBreeds[bname].join("") );
inverted[val] = bname;

Here, a demo to show it works:

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"] );

Old Pedant
11-03-2012, 01:34 AM
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

var fishOne = availableBreeds["NICE"]; // NICE comes from user input

// and now you actually have
// fishOne == [1]