【发布时间】:2020-09-01 17:51:14
【问题描述】:
大家好,我正在尝试创建一种迷你文字游戏,其中每个玩家都被分配一个随机角色(每个角色都有一定的力量,每次战斗后都会减少他击败的角色的力量) 这里是所有乐趣发生的函数
var characters = [
'c1',
'c2',
'c3',
'c4',
'c5',
'c6',
'c7',
'c8',
'c9',
'c10'
]
var PL = [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
new_arr[0] = gra_arr;
//the gra_arr is an array withe name of the players that i take from the user (that happens in another part of the code)
new_arr[1] = characters;
new_arr[2] = PL;
start = function() {
new_arr[0] = gra_arr;
new_arr[1] = characters;
new_arr[2] = PL;
function shuffle(array) {
var currentIndex = array[1].length,
temporaryValue, randomIndex;
var currentIndex2 = array[2].length,
temporaryValue2;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
currentIndex2 -= 1;
// And swap it with the current element.
temporaryValue = array[1][currentIndex];
array[1][currentIndex] = array[1][randomIndex];
array[1][randomIndex] = temporaryValue;
temporaryValue2 = array[2][currentIndex2];
array[2][currentIndex2] = array[2][randomIndex];
array[2][randomIndex] = temporaryValue2;
}
return array;
}
shuffle(new_arr);
set1 = function(array1) {
for (i = 0; i < array1[0].length; i++) {
new_arr[0][i] = new_arr[0][i] + " - " + new_arr[1][i];
}
}
set1(new_arr);
print_list = function(div_id, grp_vett, grp) {
var ul = $("<ul>");
var h1 = $("<h1>")
var span = $("<span>").text(grp);
h1.append(span);
$(div_id).append(ul);
$(div_id).prepend(h1);
for (i = 0; i < grp_vett.length; i++) {
var h12 = $("<h3>").html(grp_vett[i]);
var li = $("<li>").append(h12);
ul.append(li);
}
}
print_list("#gra1", new_arr[0], "Assignments");
//RESHUFFLE
function shuffle(array) {
var currentIndex = array[0].length,
temporaryValue, randomIndex;
var currentIndex2 = array[0].length,
temporaryValue2;
var currentIndex3 = array[0].length,
temporaryValue3;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
currentIndex2 -= 1;
currentIndex3 -= 1;
// And swap it with the current element.
temporaryValue = array[0][currentIndex];
array[0][currentIndex] = array[0][randomIndex];
array[0][randomIndex] = temporaryValue;
temporaryValue2 = array[1][currentIndex2];
array[1][currentIndex2] = array[1][randomIndex];
array[1][randomIndex] = temporaryValue2;
temporaryValue3 = array[2][currentIndex3];
array[2][currentIndex3] = array[2][randomIndex];
array[2][randomIndex] = temporaryValue3;
}
return array;
}
shuffle(new_arr);
var round = 0;
matches = function() {
round += 1;
var h1 = $("<h1>").text("Round " + round + " Matches");
var x = $("#r1");
x.append(h1);
var arCopy = new_arr;
for (i = 0; i < arCopy[0].length; i += 2) {
if (arCopy[2][i] > arCopy[2][i + 1]) {
new_arr[0].splice(i + 1, 1)
new_arr[1].splice(i + 1, 1)
new_arr[2].splice(i + 1, 1)
} else {
new_arr[0].splice(i, 1)
new_arr[1].splice(i, 1)
new_arr[2].splice(i, 1)
}
}
}
matches();
基本上当我进行第一次测试时(假设所有 3 个数组的长度均为 10)并查看控制台时,我期望 new_arr[0],new_arr1,new_arr[2] 的长度为 5,但它们有长度 6
最终我的目标是做到这一点,直到只剩下 1 个玩家
matches = function() {
round += 1;
var h1 = $("<h1>").text("Round " + round + " Matches");
var x = $("#r1");
x.append(h1);
while (new_arr[0].length > 1) {
var arCopy = new_arr;
for (i = 0; i < arCopy[0].length; i += 2) {
if (arCopy[2][i] > arCopy[2][i + 1]) {
new_arr[0].splice(i + 1, 1)
new_arr[1].splice(i + 1, 1)
new_arr[2].splice(i + 1, 1)
} else {
new_arr[0].splice(i, 1)
new_arr[1].splice(i, 1)
new_arr[2].splice(i, 1)
}
}
}
}
【问题讨论】:
-
当您有相关数据时,通常最好使用单个对象数组,而不是为每个对象使用单独的数组。
-
你有两个函数名为
shuffle。 -
我尝试了对象,但我目前对对象的了解有点“有限”,所以我尝试了数组,关于我没有注意到的函数,谢谢
标签: javascript arrays function simulation array-splice