Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Created August 4, 2014 20:46
Show Gist options
  • Save ashleygwilliams/a0b79f42d655b12d9024 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/a0b79f42d655b12d9024 to your computer and use it in GitHub Desktop.
// A LESS TRITE EXAMPLE
// we might make an object to use in a game, for example let's think of
// Pokemon
function Pokemon(options) {
this.name = options.name;
this.hp = options.hp;
this.moves = options.moves;
this.attack = function() {
return this.moves[Math.floor(Math.random()*2)];
}
}
var charmander = new Pokemon({
name: "charmander",
hp: 300,
moves: [
{name: "scratch", hp: 100},
{name: "fire blast", hp: 400}
]
});
var squirtle = new Pokemon({
name: "squirtle",
hp: 200,
moves: [
{name: "tackle", hp: 200},
{name: "surf", hp: 300}
]
});
var pikachu = new Pokemon({
name: "pikachu",
hp: 400,
moves: [
{name: "quick attack", hp: 200},
{name: "thunderbolt", hp: 400}
]
});
function battle(pokemon1, pokemon2) {
var attack1 = pokemon1.attack();
var attack2 = pokemon2.attack();
if (attack1.hp > pokemon2.hp ) {
return pokemon1.name + " attacked with " + attack1.name + " and defeated " + pokemon2.name;
} else if (attack2.hp > pokemon1.hp) {
return pokemon2.name + " attacked with " + attack2.name + " and defeated " + pokemon1.name;
} else {
return "it's a tie!";
}
}
@tkellen
Copy link

tkellen commented Aug 4, 2014

The max random move number should be the length of the moves array.

This is rad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment