пока тай страх выходит
function change() {
var allhlt1 = 0;
var allhlt2 = 0;
for (var key in arr)
{
allhlt1 = parseFloat(allhlt1) + parseFloat(arr[key]['def']);
}
for (var key in arr2)
{
allhlt2 = parseFloat(allhlt2) + parseFloat(arr2[key]['def']);
}
var atk2 = (Math.random() * (arr.length - 1)).toFixed();
var def1 = (Math.random() * (arr.length - 1)).toFixed();
var atk1 = (Math.random() * (arr2.length - 1)).toFixed();
var def2 = (Math.random() * (arr2.length - 1)).toFixed();
i++;
arr[def1]['def'] = parseFloat(arr[def1]['def']) - parseFloat(arr2[atk1]['atk']);
if (arr[def1]['def'] <= 0) arr.splice(def1, 1);
arr2[def2]['def'] = parseFloat(arr2[def2]['def']) - parseFloat(arr[atk2]['atk']);
if (arr[def2]['def'] <= 0) arr2.splice(def2, 1);
var inProgress = allhlt1 > 0 && allhlt2 > 0 && i < 20;
console.log(i, allhlt1, allhlt2, inProgress, arr[def1]['def'], arr2[atk1]['atk'], arr2[def2]['def'], arr[atk2]['atk']);
if (inProgress) {
setTimeout(change, 1000);
}
else
{
(allhlt1 > allhlt2) ? cong = '1 игрок' : cong = '2 игрок';
alert ("win "+cong);
}
}
setTimeout(change, 1000);