In this tutorial
We will be creating 2 objects, 1 called Dice and the other called Thrower. The Dice class will be an object that can be set with a naximum number of sides and rolled to get a number. The Thrower object will take multiple dice and roll them all at once returning the resultant sum of all the dice rolled.
Lets get started
When starting out creating an object it is useful to know what the object will need. FOr instance, you wouldn't want to give the object Air a variable called breathing_rate, so make sure you have thought your object through before creating them.
A note about coding - To continue, or begin creating good programming habits we will be naming all objects with a capital letter. As you saw with the Air object the "A" is capitalized. This is because it is the standard for the time being. For anyone who has programmed in Java, Actionscript, C++ or any other number of languages you know that this is the case, and has been for quite a while. Later on, when we startcreating sub-functions we will be maning them starting with lower case and then using camel case for new words. That looks like so: getNum
So, to get started with our code we create a function called Dice (this will be our object), and we give it these 2 variables: number and max. This code looks like so:
function Dice(){
this.number;
this.max;
}
NOTICE - When using variables that are for an object you must use this otherwise the variables are deleted after they are done being used. This is of course unless they are global, but we don't want to have a whole bunch of global variables.
Because we want to be able to set the max we need to add a parameter to Dice that is set to max:
function Dice(maxNum){
this.number;
this.max = maxNum;
}
Now, however we have a problem. What if someone doesn't set the max?
This can easily be checked for by and if/ else statement:
function Dice(maxNum){
this.number;
if(maxNum){
this.max = maxNum;
}
else{
this.max = 6; //some initial value, in this case a 6 sided dice.
}
}
Good, now that we have the basics of our object complete lets start adding some sub-functions (methods in Java, class functions in C++).
Adding functions
Adding functions to an object is a little different than in other languages. This is because it is done inside of a function and acts like a sub-function.
This can be seen as such:
function Dice(maxNum){
this.number;
if(maxNum){
this.max = maxNum;
}
else{
this.max = 6; //some initial value, in this case a 6 sided dice.
}
this.roll = function(){
// Do stuff here
}
}
In the case of our roll function we will want to have it calculate a random number between 1 and its max. THis is accomplished like so:
function Dice(maxNum){
this.number;
if(maxNum){
this.max = maxNum;
}
else{
this.max = 6; //some initial value, in this case a 6 sided dice.
}
this.roll = function(){
this.number = Math.round(Math.random()*(this.max-1))+1;
return this.number;
}
}
Which simply gets a random number (returns a number between 0 and 1), multiplies it by 1 less than max, rounds it to the nearest int, and adds to it. Adding 1 sets the minimum the number can be as 1.
NOTICE - The function returns the number that is calculated.
Now, just because we have returned a number once doesn't mean that we can just let it sit there until we roll again. What if someone wants to take another look at the number?
In that case we will need to let them see it. This is done by creating another sub-function called getNum.
PARTICIPATION OPP! - If you feel like trying out some coding on your own for this object create a sub-function that returns the number of the object. If you don't care to try things on your own at this point continue reading.
So, now that you have made up your mind, here is the current code:
function Dice(maxNum){
this.number;
if(maxNum){
this.max = maxNum;
}
else{
this.max = 6;
}
this.roll = function(){
this.number = Math.round(Math.random()*(this.max-1))+1;
return this.number;
}
this.getVal = function(){
return this.number;
}
}
Creating a Thrower
PARTICIPATION OPP! - For those of you who feel like trying more OOJS on your own here is your chance to try creating an object on your own! The object name is Thrower and will take an array (or single) dice, be able to roll all the dice, returning the sum of all the rolled dice; and add extra dice to the array.
Now, we will be creating another object. This will be able to control the dice objects (in a sense of the word).
Starting out we will create the object:
function Thrower(){
// Stuff here
}
To start out the Thrower function will need the variable myDice. After that is complete we need to get a function to roll the dice created.
For the moment the code looks like so:
function Thrower(dice){
this.myDice = dice;
}
When creatingthe roll function it is important to remember that we need to have it loop through the whole length of the array this.myDice. We also want it to return the sum of the dice, this means we need to have it counting through the loop.
Here is what the thrower object looke like with the roll functuion:
function Thrower(dice){
this.myDice = dice;
this.roll = function(){
var sum = 0;
for(var i=0; i<this.myDice.length; i++){
sum += this.myDice[i].roll();
}
return sum;
}
}
Now, what do we do if people want to add more dice?
This is easy enough, you add to the end of the array this.myDice with a simple function called addDie
Here if the final code for the Thrower object:
function Thrower(dice){
this.myDice = dice;
this.roll = function(){
var sum = 0;
for(var i=0; i<this.myDice.length; i++){
sum += this.myDice[i].roll();
}
return sum;
}
this.addDie = function(dice){
this.myDice[this.myDice.length] = dice;
}
}
Creating instances of an object
When creating instances of an object it is important to remember that you need to use the new keyword.
Here is a code snippet we can use to create a single instance of a Dice:
var dice = new Dice();
REALIZE - You can call to all the functions and variables inside of the object instance like so dice.(FUNCTION/VARIABLE NAME HERE). So, if we were to call dice.roll() it would roll the instance of the object that the variable dice is pointing to.
The Finished Product!!!
Here is a look at the whole code that will output the number and everything:
function Thrower(dice){
this.myDice = dice;
this.roll = function(){
var sum = 0;
for(var i=0; i<this.myDice.length; i++){
sum += this.myDice[i].roll();
}
return sum;
}
this.addDie = function(dice){
this.myDice[this.myDice.length] = dice;
}
}
function Dice(maxNum){
this.number;
if(maxNum){
this.max = maxNum;
}
else{
this.max = 6;
}
this.roll = function(){
this.number = Math.round(Math.random()*(this.max-1))+1;
return this.number;
}
this.getVal = function(){
return this.number;
}
}
var dice = [new Dice(), new Dice()];
var hands = new Thrower(dice);
hands.addDie(new Dice());
for(var i=0; i<100; i++){
document.write("Dice roll "+(i+1)+" = "+hands.roll()+"<br>");
}
The End
Enjoy your additional knowledge.






MultiQuote




|