Created
July 1, 2014 06:41
-
-
Save andrecaribe/46fa146df9f15ebd8411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MOREIA_DE_MANA = 'Moreia de Mana'; | |
var IMAGEM_ESPELHADA = 'Imagem Espelhada'; | |
var deck, | |
hand, | |
matches = 0, | |
perfectHand = 0; | |
/** | |
* (Re)inicializa variáveis. | |
*/ | |
var reset = function() { | |
deck = []; | |
hand = []; | |
} | |
/** | |
* Classe que implementa uma carta. | |
*/ | |
var Card = function(name) { | |
this.name = name; | |
} | |
/** | |
* Adiciona cartas ao deck. | |
*/ | |
var make = function() { | |
var i = 0; | |
// 26 cartas quaisquer | |
for(i = 0; i < 26; ++i) { | |
deck.push(new Card('Unknow')); | |
} | |
deck.push(new Card(MOREIA_DE_MANA)); | |
deck.push(new Card(MOREIA_DE_MANA)); | |
deck.push(new Card(IMAGEM_ESPELHADA)); | |
deck.push(new Card(IMAGEM_ESPELHADA)); | |
} | |
/** | |
* Compra uma carta do deck e coloca na mão. | |
* | |
* @private | |
* @method draw | |
*/ | |
var draw = function() { | |
var index = randomRangeInt(0, deck.length - 1); | |
hand.push(deck[index]); | |
removeItemFromArray(deck, index); | |
} | |
/** | |
* Remove uma carta da mão e coloca no deck. | |
* | |
* @private | |
* @method mulligan | |
* @param {int} Index do elemento no vetor 'hand' | |
*/ | |
var mulligan = function(index) { | |
deck.push(hand[index]); | |
removeItemFromArray(hand, index); | |
} | |
/** | |
* Verifica cartas na mão e dá mulligan se necessário. | |
* | |
* @private | |
* @method checkHand | |
*/ | |
var checkHand = function() { | |
var result = []; | |
var numOfManaWyrm = 0; | |
var numOfMirrorImage = 0; | |
while (hand.length > 0) { | |
if (hand[0].name === MOREIA_DE_MANA) { | |
if (numOfManaWyrm === 0) { | |
result.push(hand[0]); | |
removeItemFromArray(hand, 0); | |
} else { | |
mulligan(0); | |
} | |
} else if (hand[0].name === IMAGEM_ESPELHADA) { | |
if (numOfMirrorImage === 0) { | |
result.push(hand[0]); | |
removeItemFromArray(hand, 0); | |
} else { | |
mulligan(0); | |
} | |
} else { | |
mulligan(0); | |
} | |
} | |
hand = result; | |
for (var i = hand.length; i < 4; i++) { | |
draw(); | |
} | |
analyzeHand(); | |
} | |
/** | |
* Analisa resultado da mão final. | |
* | |
* @private | |
* @method showHand | |
*/ | |
var analyzeHand = function() { | |
var hasManaWyrm = false; | |
var hasMirroImage = false; | |
for (var i = 0; i < hand.length; i++) { | |
if (hand[i].name === MOREIA_DE_MANA) { | |
hasManaWyrm = true; | |
} else if (hand[i].name === IMAGEM_ESPELHADA) { | |
hasMirroImage = true; | |
} | |
} | |
if (hasManaWyrm && hasMirroImage) { | |
perfectHand++; | |
} | |
//showHand(); | |
} | |
/** | |
* Imprime lista de cartas da mão. | |
* | |
* @private | |
* @method showHand | |
*/ | |
var showHand = function() { | |
for (var i = 0; i < hand.length; i++) { | |
console.log(hand[i].name); | |
} | |
} | |
/** | |
* Remove um elemento de um vetor. | |
* | |
* @private | |
* @method removeItemFromArray | |
* @param {Array} Vetor | |
* @param {int} Index do elemento no vetor 'hand' | |
*/ | |
var removeItemFromArray = function(array, index) { | |
array.splice(index, 1); | |
} | |
/** | |
* Retorna um número inteiro aleatório dentro dos limites de mínimo e máximo. | |
* | |
* @private | |
* @method randomRangeInt | |
* @param {int} Mínimo | |
* @param {int} Máximo | |
*/ | |
var randomRangeInt = function(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
/** | |
* Inicializa iterações. | |
* | |
* @public | |
* @param {int} Número de iterações | |
*/ | |
function start(iterations) { | |
for (var i = 0; i < iterations; i++) { | |
reset(); | |
make(); | |
draw(); | |
draw(); | |
draw(); | |
draw(); | |
checkHand(); | |
matches++; | |
} | |
console.log("matches: " + matches); | |
console.log("perfect hands: " + perfectHand); | |
console.log("probability: " + ((perfectHand * 100 / matches) * 0.5)); | |
} | |
start(10000000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment