Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
agarie committed May 3, 2012
0 parents commit dcdaec9
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jsTestDriver.conf
.DS_STORE
typechart.png
36 changes: 36 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<doctype html>
<html>
<head>
<title>typeCalc: Discover your weaknesses.</title>
</head>
<body>

<form id="input">
<div id="pkmn1" class="pokemon">
<label for="type1">Type1:</label><input type="text" name="type1" value="normal" id="type1" />
<label for="type2">Type2:</label><input type="text" name="type2" id="type2" />
</div>

<button id="getWeaknesses">Calculate!</button>
</form>

<div id="output"></div>

<script type="text/javascript" charset="utf-8" src="lib/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="src/typecalc.js"></script>
<script type="text/javascript" charset="utf-8">
// Create a loop that gets all the info from the UI
var start = function () {
var type1 = $("#input #pkmn1 #type1").val();
var type2 = $("#input #pkmn1 #type2").val();

var def = TYPECALC.getArrayOfDefEffect(type1, type2);

console.log(def);
};

$("#input button").click(start);
// Appends the result to the output div
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions lib/jquery.min.js

Large diffs are not rendered by default.

171 changes: 171 additions & 0 deletions src/typecalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
typeCalc: An app for analysing a pokemon team's type-based weaknesses
@author Carlos "Onox" Agarie
@version 0.1
*/

"use strict";

var TYPECALC = (function () {
// TYPE_CHART: Constant object which stores the type-chart
// Organization: rows = attack, colums = defese
// Type order: normal, fire, water, electric, grass, ice, fighting, poison, ground, flying,
// psychic, bug, rock, ghost, dragon, dark, steel
var TYPES = ["normal", "fire", "water", "electric", "grass", "ice", "fighting", "poison", "ground", "flying", "psychic", "bug", "rock", "ghost", "dragon", "dark", "steel"],
TYPE_ORDER = {
normal: 0,
fire: 1,
water: 2,
electric: 3,
grass: 4,
ice: 5,
fighting: 6,
poison: 7,
ground: 8,
flying: 9,
psychic: 10,
bug: 11,
rock: 12,
ghost: 13,
dragon: 14,
dark: 15,
steel: 16
},
TYPE_CHART = {
normal: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0, 1, 1, 0.5],
fire: [1, 0.5, 0.5, 1, 2, 2, 1, 1, 1, 1, 1, 2, 0.5, 1, 0.5, 1, 2],
water: [1, 2, 0.5, 1, 0.5, 1, 1, 1, 2, 1, 1, 1, 2, 1, 0.5, 1, 1],
electric: [1, 1, 2, 0.5, 0.5, 1, 1, 1, 0, 2, 1, 1, 1, 1, 0.5, 1, 1],
grass: [1, 0.5, 2, 1, 0.5, 1, 1, 0.5, 2, 0.5, 1, 0.5, 2, 1, 0.5, 1, 0.5],
ice: [1, 0.5, 0.5, 1, 2, 0.5, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 0.5],
fighting: [2, 1, 1, 1, 1, 2, 1, 0.5, 1, 0.5, 0.5, 0.5, 2, 0, 1, 2, 2],
poison: [1, 1, 1, 1, 2, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 0],
ground: [1, 2, 1, 2, 0.5, 1, 1, 2, 1, 0, 1, 0.5, 2, 1, 1, 1, 2],
flying: [1, 1, 1, 0.5, 2, 1, 2, 1, 1, 1, 1, 2, 0.5, 1, 1, 1, 0.5],
psychic: [1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0.5, 1, 1, 1, 1, 0, 0.5],
bug: [1, 0.5, 1, 1, 2, 1, 0.5, 0.5, 1, 0.5, 2, 1, 1, 0.5, 1, 2, 0.5],
rock: [1, 2, 1, 1, 1, 2, 0.5, 1, 0.5, 2, 1, 2, 1, 1, 1, 1, 0.5],
ghost: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 0.5, 0.5],
dragon: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 0.5],
dark: [1, 1, 1, 1, 1, 1, 0.5, 1, 1, 1, 2, 1, 1, 2, 1, 0.5, 0.5],
steel: [1, 0.5, 0.5, 0.5, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 0.5]
};

return {
// Return the effectiveness of attackType attacking defenseType
getEffect: function(attackType, defenseType) {
var defenseNumber = TYPE_ORDER[defenseType];

return TYPE_CHART[attackType][defenseNumber];
},

printEffect: function (type, atkOrDef, matrix) {
var r = "",
M = matrix || {};

atkOrDef = atkOrDef || "atk";

// matrix is used mainly for debugging, to see the order of
if (atkOrDef === "atk" && M !== matrix) {
M = TYPE_CHART;
}
else if (M !== matrix) {
M = this.transpose(TYPE_CHART);
}

for (var key in TYPE_ORDER) {
if (TYPE_ORDER.hasOwnProperty(key)) {
r += key + " " + M[type.toLowerCase()][TYPE_ORDER[key]];
r += "\n";
}
}

return r.replace(/\n$/, "");
},

// Return an array with the effectiveness of given attack type
getArrayOfAtkEffect: function (type) {
if (typeof type !== "string" || TYPE_CHART[type] === undefined) {
return false;
}

return TYPE_CHART[type.toLowerCase()];
},

// Returns the effectiveness of all types attacking a type1/type2 pokemon
getArrayOfDefEffect: function (type1, type2) {
if (typeof type1 !== "string" || TYPE_CHART[type1] === undefined) {
return false;
}

var TRANSPOSED_TYPE_CHART = this.transpose(TYPE_CHART),
result = [],
ary1 = TRANSPOSED_TYPE_CHART[type1.toLowerCase()],
ary2 = (typeof type2 === "string" && type2.length > 0) ? TRANSPOSED_TYPE_CHART[type2.toLowerCase()] : [];

result = this.dotProduct(ary1, ary2) || ary1;

return result;
},

// Assumes that the input is an object of arrays
transpose: function (matrix) {
if (typeof matrix !== "object") {
return false;
}

var m = 0,
col = [],
colName = [],
transposed = {},
i = 0;

for (var key in matrix) {
if (matrix.hasOwnProperty(key)) {
colName.push(key);
}
}

if (colName.length === 0) {
return false;
}
else {
m = matrix[colName[0]].length;
}

if (!m || m === undefined) {
return false;
}

for (i = 0; i < m; i = i + 1) {
for (var key in matrix) {
if (matrix.hasOwnProperty(key)) {
col.push(matrix[key][i]);
}
}

transposed[colName[i]] = col;
col = [];
}

return transposed;
},

// Element-wise multiplication of two arrays
dotProduct: function (u, v) {
if (u.length !== v.length) {
return false;
}

var n = u.length,
result = [],
i;

for (i = n; i > 0; i = i - 1) {
result[i-1] = u[i-1] * v[i-1];
}

return result;
}
};
}());
82 changes: 82 additions & 0 deletions tests/typecalc-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
TestCase("TypeChartTest", {
setUp: function() {
this.type1 = "water";
this.type2 = "steel";
this.type3 = "ice";
this.type4 = "ghost";
this.type5 = "normal";
},

"test TYPECALC should be an object": function () {
assertObject(TYPECALC);
},

"test constant TYPE_ORDER should be hidden": function() {
assertUndefined(TYPECALC.TYPE_ORDER);
},

"test constant TYPE_CHART should be hidden": function() {
assertUndefined(TYPECALC.TYPE_CHART);
},

"test getEffect should return number": function() {
assertNumber(TYPECALC.getEffect(this.type1, this.type2));
assertNumber(TYPECALC.getEffect(this.type1, this.type3));
assertNumber(TYPECALC.getEffect(this.type2, this.type3));
},

"test getEffect should return 0, 0.5, 1 or 2": function() {
assertEquals(0, TYPECALC.getEffect(this.type5, this.type4));
assertEquals(0.5, TYPECALC.getEffect(this.type2, this.type1));
assertEquals(1, TYPECALC.getEffect(this.type1, this.type2));
assertEquals(2, TYPECALC.getEffect(this.type2, this.type3));
},

"test getArrayOfAtkEffect should return false if input isn't string of a valid type": function () {
assertFalse(TYPECALC.getArrayOfAtkEffect("invalid"));
assertFalse(TYPECALC.getArrayOfAtkEffect({}));
},

"test getArrayOfAtkEffect should return an array with length 17": function () {
assertNotUndefined(TYPECALC.getArrayOfAtkEffect("water").length);
assertEquals(17, TYPECALC.getArrayOfAtkEffect("poison").length);
},

"test getArrayOfAtkEffect should return correct array for 1 type": function () {
assertEquals([1, 2, 1, 1, 1, 2, 0.5, 1, 0.5, 2, 1, 2, 1, 1, 1, 1, 0.5], TYPECALC.getArrayOfAtkEffect("rock"));
},

"test getArrayOfDefEfect should return array": function () {
assertNotUndefined(TYPECALC.getArrayOfDefEffect("normal").length);
assertNotUndefined(TYPECALC.getArrayOfDefEffect("normal", "fire").length);
},

"test transpose should return false if input isn't object": function () {
assertFalse(TYPECALC.transpose([]));
},

"test transpose should return false if the matrix is degenerated": function () {
assertFalse(TYPECALC.transpose({}));
assertFalse(TYPECALC.transpose({key: [], key2: []}));
},

"test transpose should return correct matrix": function () {
assertEquals({key1: [1, 2, 3], key2: [4, 5, 6], key3: [7, 8, 9]}, TYPECALC.transpose({key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}));
},

"test dotProduct should return an array": function () {
assertNotUndefined(TYPECALC.dotProduct([1, 2], [3, 7]));
},

"test dotProduct should return false if arrays' dimensions differ": function () {
assertFalse(TYPECALC.dotProduct([1, 2, 3], [4, 5]));
},

"test dotProduct should return correct result": function () {
assertEquals([1, 14, 9, 55], TYPECALC.dotProduct([1, 2, 3, 5], [1, 7, 3, 11]));
},

"test dotProduct(a, b) = dotProduct(b, a)": function () {
assertEquals(TYPECALC.dotProduct("normal", "steel"), TYPECALC.dotProduct("steel", "normal"));
}
});

0 comments on commit dcdaec9

Please sign in to comment.