Skip to content

Commit

Permalink
Now its working, time to upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
agarie committed May 3, 2012
1 parent dcdaec9 commit e83e119
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 107 deletions.
222 changes: 115 additions & 107 deletions src/typecalc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
typeCalc: An app for analysing a pokemon team's type-based weaknesses
typeCalc: An app for analysing a pokemon team's type-based weaknesses.
@author Carlos "Onox" Agarie
@version 0.1
*/
Expand All @@ -11,8 +11,8 @@ var TYPECALC = (function () {
// 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 = {
var TYPES = ["normal", "fire", "water", "electric", "grass", "ice", "fighting", "poison", "ground", "flying", "psychic", "bug", "rock", "ghost", "dragon", "dark", "steel"];
var TYPE_ORDER = {
normal: 0,
fire: 1,
water: 2,
Expand All @@ -30,8 +30,8 @@ var TYPECALC = (function () {
dragon: 14,
dark: 15,
steel: 16
},
TYPE_CHART = {
};
var 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],
Expand All @@ -51,121 +51,129 @@ var TYPECALC = (function () {
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 the effectiveness of attackType attacking defenseType
var getEffect = function (attackType, defenseType) {
var defenseNumber = TYPE_ORDER[defenseType];

return TYPE_CHART[attackType][defenseNumber];
},
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$/, "");
},
var printEffect = function (type, atkOrDef, matrix) {
var r = "",
M = matrix || {};

// 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()];
},
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);
}

// 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;
for (var key in TYPE_ORDER) {
if (TYPE_ORDER.hasOwnProperty(key)) {
r += key + " " + M[type.toLowerCase()][TYPE_ORDER[key]];
r += "\n";
}

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;
},
}

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

// Return an array with the effectiveness of given attack type
var 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
var 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
var transpose = function (matrix) {
if (typeof matrix !== "object") {
return false;
}

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

// Assumes that the input is an object of arrays
transpose: function (matrix) {
if (typeof matrix !== "object") {
return false;
for (var key in matrix) {
if (matrix.hasOwnProperty(key)) {
colName.push(key);
}

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

}

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)) {
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]);
}
col.push(matrix[key][i]);
}

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

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

// 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;
return transposed;
};

// Element-wise multiplication of two arrays
var 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;
};

return {
getEffect: getEffect,
printEffect: printEffect,
getArrayOfAtkEffect: getArrayOfAtkEffect,
getArrayOfDefEffect: getArrayOfDefEffect,
transpose: transpose,
dotProduct: dotProduct
};
}());
Empty file added style.css
Empty file.
32 changes: 32 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html>
<head>
<title>typeCalc: Discover your weakness.</title>
</head>
<body>

<form id="input">
<label for="type1">Type1:</label><input type="text" name="type1" id="type1" />
<label for="type2">Type2:</label><input type="text" name="type2" id="type2" />
</form>

<button id="calculate">Calculate!</button>

<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">
$(document).ready(function () {
$("#calculate").click(function () {
var type_1 = $("#input input[name='type1']").val();
var type_2 = $("#input input[name='type2']").val();

var def = TYPECALC.getEffect(type_1, type_2);

// $("#output").html(def);
console.log(def);
});
});
</script>
</body>
</html>

0 comments on commit e83e119

Please sign in to comment.