-
-
Save paulcanning/11fb21ff819e6f8bbda4f5cabefde9a4 to your computer and use it in GitHub Desktop.
Battle 2 players
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
'use strict'; | |
var b1_block1 = {hp: 100, str: 2, chrg: 1, spd: 1, lck: 1}; | |
var b1_block2 = {hp: 100, str: 2, chrg: 1, spd: 1, lck: 1}; | |
var b1_block3 = {hp: 100, str: 2, chrg: 1, spd: 1, lck: 1}; | |
var b2_block1 = {hp: 100, str: 2, chrg: 1, spd: 1, lck: 2}; | |
var b2_block2 = {hp: 100, str: 2, chrg: 1, spd: 1, lck: 2}; | |
var b2_block3 = {hp: 100, str: 2, chrg: 1, spd: 1, lck: 2}; | |
var PLAYER_1 = [b1_block1, b1_block2, b1_block3]; | |
var PLAYER_2 = [b2_block1, b2_block2, b2_block3]; | |
var turns = 0; | |
var stats = [{name: 'player1', wins: 0, losses: 0}, {name: 'player2', wins: 0, losses: 0}]; | |
var rounds = 1000; | |
function isBlockDead(block) | |
{ | |
if (block.hp <= 0) | |
{ | |
return true; | |
} | |
return false; | |
} | |
function isPlayerDead(player, name) | |
{ | |
if (isBlockDead(player[0]) && isBlockDead(player[1]) && isBlockDead(player[2])) | |
{ | |
//console.log(name + " died in " + turns + " turns"); | |
var loserStat = findByMatchingProperties(stats, { name: name }); | |
loserStat[0].losses++; | |
return true; | |
} | |
return false; | |
} | |
function fight(player1, player2) | |
{ | |
//var players = []; | |
// first, randomise the order of the players, based on speed static | |
//players.push(player2); | |
//players.push(player1); | |
//players = shuffle(players); | |
while (!(isPlayerDead(player1, "player1") || isPlayerDead(player2, "player2"))) | |
{ | |
/*for(var x = 0; players.length; x++) | |
{ | |
console.log("hp: " + players); | |
}*/ | |
for(var n = 0; n < player1.length; n++) | |
{ | |
if (player1[n].hp > 0) | |
{ | |
player1[n].hp -= (Math.floor(Math.random() * 5) + 1) * player2[Math.floor(Math.random() * player2.length)].str; | |
} | |
} | |
for(var n = 0; n < player2.length; n++) | |
{ | |
if (player2[n].hp > 0) | |
{ | |
player2[n].hp -= (Math.floor(Math.random() * 5) + 1) * player1[Math.floor(Math.random() * player1.length)].str; | |
} | |
} | |
//console.log(player1); | |
//console.log("--------------------------------------------------"); | |
//console.log(player2); | |
//console.log("**************************************************"); | |
turns++; | |
} | |
stats[0].wins = rounds - stats[0].losses; | |
stats[1].wins = rounds - stats[1].losses; | |
stats[0].win_percentage = Math.round((rounds - stats[0].losses) / rounds * 100); | |
stats[1].win_percentage = Math.round((rounds - stats[1].losses) / rounds * 100); | |
turns = 0; | |
} | |
function battle(player1, player2, rounds) | |
{ | |
var i = 0; | |
while(i < rounds) | |
{ | |
var p1 = JSON.parse(JSON.stringify(player1)); // gotta clone them all! | |
var p2 = JSON.parse(JSON.stringify(player2)); | |
fight(p1, p2); | |
i++; | |
} | |
console.log(stats); | |
} | |
function findByMatchingProperties(set, properties) { | |
return set.filter(function (entry) { | |
return Object.keys(properties).every(function (key) { | |
return entry[key] === properties[key]; | |
}); | |
}); | |
} | |
function shuffle(array) { | |
var m = array.length, t, i; | |
// While there remain elements to shuffle… | |
while (m) { | |
// Pick a remaining element… | |
i = Math.floor(Math.random() * m--); | |
// And swap it with the current element. | |
t = array[m]; | |
array[m] = array[i]; | |
array[i] = t; | |
} | |
return array; | |
} | |
var startTime = Date.now(); | |
battle(PLAYER_1, PLAYER_2, rounds); | |
var endTime = Date.now(); | |
console.log('time taken: ' + (endTime - startTime) / 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment