我正在绑定以筛选出老虎机的模式,在本例中,如果索引具有相同的值,我希望选择以下索引。
如果索引0、2和6的值相同,则应该输出。我在想一些类似于函数调用的东西,也许是这样
if (win_filter([0, 2, 6] == "slot-2") {
console.log("You won");
}我的代码如下。
var final_score = new Array();
$(".shoot").click(function() {
//var numbers_array = ["slot-1", "slot-1", "slot-1", "slot-1", "slot-1", "slot-2", "slot-2", "slot-2", "slot-2", "slot-3", "slot-3", "slot-3", "slot-4", "slot-4", "slot-5"];
var numbers_array = ["slot-1", "slot-2", "slot-3", "slot-4", "slot-5"];
var target = $("div.window table");
target.find("tr td > div").each(function() {
$(this).fadeOut(function() {
$(this).removeAttr('class');
$(this).addClass(numbers_array[Math.floor(Math.random() * numbers_array.length)]);
$(this).fadeIn();
final_score.push($(this).attr("class"));
});
});
function filterResults(arr) {
return final_score.filter(function(el) {
return arr.some(function(e) {
return el.timeframe == e;
});
});
}
var result = filterResults(['0','2','6']);
console.log(result);
$.each(numbers_array, function(index, value) {
if (result == value) {
$(".notice").html("You have won.").addClass("success").show();
console.log("You won!");
}
});
console.log(final_score);
});编辑
如果不清楚我指的是数组中的索引,那么如果我从这个生成的数组中选择索引0、2和6,那么这些索引的值将是(即使它们不是相同的)。
0 => "slot-2", 2 => "slot-5", 6 => "slot-1"目标是检查所选索引是否具有相同的值输出。而且索引的数量不应该被硬编码,它可以是从3个索引搜索到5个索引搜索。
Array[0]
0 : "slot-2"
1 : "slot-3"
2 : "slot-5"
3 : "slot-5"
4 : "slot-4"
5 : "slot-3"
6 : "slot-1"
7 : "slot-4"
8 : "slot-1"
9 : "slot-2"
10 : "slot-2"
11 : "slot-4"
12 : "slot-5"
13 : "slot-1"
14 : "slot-4"发布于 2017-01-11 15:29:50
首先,我避免了对代码进行任何重大修改,因此在小提琴中,我编写了验证,这正是您所期望的(在on('click')中)。
我所做的:
在计算任何数据之前,我们需要时隙数据。您已经将它保存在final_score中,但是这样做的函数是一个回调。等待回调值得吗?-我不这么认为,因为它是一个简单的css (fadeOut)动画。
const classVal = numbers_array[Math.floor(Math.random() * numbers_array.length)];
$(this.fadeOut(function() { // Rest of your code }
final_score.push(classVal);计算每一行的长度,您知道它们都是相等的长度,并且它们都在一个数组(final_score)中,所以只要简单地除以行数就足够了。
const lines = 3;
const lineLength = final_score.length/lines;对于每一行,我们检查其他行的值是否与此行相同。考虑到您的数组顺序不是基于显示顺序,而是基于生成它们的顺序,我们可以简单地检查具有相同索引的数组(但迭代每一行)。
final_score[i] === final_score[i+lineLength] && final_score[i] === final_score[i+lineLength*2]
其结果是:
const lineLength = final_score.length/3;
for(let i=0; i<lineLength; i++) {
if (final_score[i] === final_score[i+lineLength] && final_score[i] === final_score[i+lineLength*2]) {
console.info(`win col ${i}`);
}
}如果你需要它,你可以很容易的n-ify这个。
const lineLength = final_score.length/3;
for(let i=0; i<lineLength; i++) {
const lineOneSlot = final_score[i];
let allEqual = true;
for (let j=1; j<lines; j++) {
console.info(`Comparing ${i} ${i+lineLength*j}`);
if (lineOneSlot !== final_score[i+lineLength*j]) {
allEqual = false;
break;
}
}
if (allEqual) {
console.info(`win col ${i}`);
}
}由于您还要求进行对角线检查,所以应该如下所示:
但是,您必须确保网格是一个正方形,才能获得预期的结果。否则,您将不得不重新定义在这些情况下应该做什么。
final_score[i] === final_score[i+1+lineLength] && final_score[i] === final_score[i+line+lineLength*line]
发布于 2017-01-16 17:37:47
阅读您的摘要和评论,得到的想法是,您正在尝试创建一些老虎机,其中的中间行是用于匹配的评估。因此,更改了代码中的几行代码来完成它。JS小提琴演示
注:我尝试过两种方法,一种是胜利者。
/* checks if selected items are matching the winning pattern ... */
function areItemsMatchingWinningPattern(selectedItems, winningItemsPattern) {
return !!selectedItems && !!winningItemsPattern && (selectedItems.length == winningItemsPattern.length) && selectedItems.every(function (elm, idx) {
return elm === winningItemsPattern[idx];
})
}
/* checks if all selected items are same .... */
function areAllItemsMatching(source) {
return !!source && source.length > 0 && source.every(function (elm, idx, array) {
return idx === 0 || array[idx - 1] == elm;
});
}发布于 2017-01-17 09:14:18
该方案可以根据有效索引的非空任意长度列表检查获胜条件。这是工作的JSFiddle。
var win_filter = function(indexes, allValues) {
// assumes all indexes are valid
// gather all the values from the slot machine by index.
var values = [];
for (var i = 0; i < indexes.length; i++)
values.push(allValues[indexes[i]]);
// if every value matches the others, then all values match the first
if (values.every(function(x) { return x == values[0]; }))
return values[0]; // return the value that was the same
// No match found, return null to signify that no winning condition was found.
return null;
}
$(".shoot").on('click', function() {
var final_score = [];
var numbers_array = ["slot-1", "slot-2", "slot-3", "slot-4", "slot-5"];
// ...
// Instead of iterating through the possible winning values, if there was any match, it means they won, and winningMatch will contain that matching value. ie 'slot-1'
var winningMatch = win_filter([0, 2, 6], final_score);
if (winningMatch) {
$(".notice").html("You have won.").addClass("success").show();
}
});请注意,如果任何可用类名的匹配集合都获胜,那么我们就不需要迭代可能的获胜案例列表来确定游戏是否赢了,因为任何比赛都不会赢吗?
https://stackoverflow.com/questions/41550195
复制相似问题