我正在尝试制作Yahtzee游戏。我有一个功能来检查掷骰子是否为小直。
var sortedDice = rollDice().sort(); // rollDice() generates an array with 5 random numbers
我的功能,以确定是否有一个小的顺子:
function isSmStraight(checkSmStraight){
var smStraight = false;
var i = 1;
var j = 0;
//will remove a die if there are duplicates
while(i < checkSmStraight.length){
if (checkSmStraight[i] == checkSmStraight[j]){
i++;
} else {
j++;
checkSmStraight[j] = checkSmStraight[i];
i++;
}//end if else
}//end while loop that moves duplicates to last index of array
checkSmStraight.pop();//removes last index of array
if (isLgStraight(checkSmStraight) == true){
smStraight = true;
} else if (checkSmStraight.length == 4 && checkSmStraight[checkSmStraight.length-1] - checkSmStraight[0] == 3){
smStraight = true;
}//end if else if
return smStraight;
}//end function isSmStraight()
我已将sortedDice复制到另一个数组fourDice,可用于调用isSmStraight()。我只希望此函数使用四索引数组,但它总是与sortedDice混淆,因此程序的其余部分使用四骰子数组。(这不是整个程序,只是我认为相关的部分。另外,程序完成了,我只是想首先使评分功能正确)。
默认情况下,数组是通过JavaScript引用传递的。因此,如果您只是在做类似的事情var fourDice = sortedDice
,然后编辑fourDice,sortedDice也将被编辑。请在问题中将代码复制到sortedDice到fourDice的地方。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句