我想创建一个函数来检查我的子集是否包含我在Javascript中的一个超集。它应该返回true或false。
有7个固定的超集,每个超集都是一个固定3个整数的数组,见下文:
var supersets = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]];
子集也是一个数组,但具有随机长度并且是随机顺序。例如:
var subset1 = [2, 4, 8, 1, 3];
或
var subset2 = [3, 7, 5];
对于这两个子集,函数应返回true (subset1包含1,2和3)(subset2包含3,5和7)
但是这应该返回false,因为它不包含任何超集:
var subset3 = [2, 1, 7, 6]
该功能应该是这样的:
if (subset contains of of the 7 supersets) {
return true;
} else {
return false;
}
对于想要了解我使用该代码的项目的人来说,它是Tic Tac Too游戏的荷兰语版本。
var whosTurnElement = document.getElementById("whosTurnElement"),
errorOutputElement = document.getElementById("errorOutput"),
fieldOneElement = document.getElementById("fieldOne"),
fieldTwoElement = document.getElementById("fieldTwo"),
fieldTreeElement = document.getElementById("fieldTree"),
fieldFourElement = document.getElementById("fieldFour"),
fieldFiveElement = document.getElementById("fieldFive"),
fieldSixElement = document.getElementById("fieldSix"),
fieldSevenElement = document.getElementById("fieldSeven"),
fieldEightElement = document.getElementById("fieldEight"),
fieldNineElement = document.getElementById("fieldNine");
// Switch die aan de hand van een getal het juste element terug geeft
function getCorrespondingElement(field) {
console.log(playerTurn + "Start switch" + field);
switch (field) {
case 1:
console.log("returing element");
return document.getElementById("fieldOne");
case 2:
return document.getElementById("fieldTwo");
case 3:
return document.getElementById("fieldTree");
case 4:
return document.getElementById("fieldFour");
case 5:
return document.getElementById("fieldFive");
case 6:
return document.getElementById("fieldSix");
case 7:
return document.getElementById("fieldSeven");
case 8:
return document.getElementById("fieldEight");
case 9:
return document.getElementById("fieldNine");
}
};
// Array met winnende compinaties, elke combinatie is opgeslagen als een Array in een Array
var winningMoves = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]
];
// object met de ingevulde vakken
var moves = {
// Array met player 1 zijn ingevulde vakken
player1: [],
// Array met player 2 zijn ingevulde vakken
player2: []
};
// Check of de moves Array vande spleer overeen komt met een van de de Winning Array
function winCheck(player) {
// Zet de playerMoves variable naar de huidige player, en sorteert de vakjes
var playerMoves;
if (player == "player1") {
playerMoves = moves.player1.sort();
} else if (player == "player2") {
playerMoves = moves.player2.sort();
}
// Loopt door de plaermoves heel en kijkt of een een winnende combinatie in zit
for (var i = 0, l = winningMoves.length; i < l; i++) {
var found = winningMoves[i].every(function(el) {
return playerMoves.indexOf(el) > -1;
});
if (found) return true;
}
return false;
};
// Functie die kijkt of het vakje al is ingevult, het vakje wordt megegeven als parameter
function fieldCheckedCheck(field) {
var isChecked = false;
// Loopt door mijn moves Array
for (var i = 0; i < moves.player1.length; i++) {
if (moves.player1[i] == field) {
ischecked = true;
}
};
// Loopt door de computers moves Array
for (var i = 0; i < moves.player2.length; i++) {
if (moves.player2[i] == field) {
ischecked = true;
}
};
return isChecked;
};
// Functie die het ingevulde vakje toevoegd aan het Object met in gevulde vakken. Deze functie krijgt 2 parameters mee, 'player' de huidige speler en 'field' om welk vakje het gaat.
function setMove(player, field) {
console.log("setMove");
if (player == "player1") {
// Kijkt of het vakje al is ingevult
if (fieldCheckedCheck(field) == true) {
// Geeft een error weer als het al is ingevult
errorOutputElement.innerHTML = "Dat vakje is al ingevult";
return false;
} else if (fieldCheckedCheck(field) == false) {
// Voegt het vakje toe aan mijn Array
moves.player1.push(field);
console.log(field);
// Vult het vakje in met een X
getCorrespondingElement(field).innerHTML = "×";
return true;
}
} else if (player == "player2") {
// Kijkt of het vakje al is ingevult
if (fieldCheckedCheck(field) == true) {
// Geeft een error weer als het al is ingevult
errorOutputElement.innerHTML = "Dat vakje is al ingevult";
return false;
} else if (fieldCheckedCheck(field) == false) {
// Voegt het vakje toe aan mijn Array
moves.player2.push(field);
console.log(field);
// Vult het vakje in met een X
getCorrespondingElement(field).innerHTML = "๐";
return true;
}
}
};
// Zet de eerste beurt voor Player1
var playerTurn = "player1";
function player1Click(field) {
if (setMove("player1", field) == true) {
if (winCheck("player1") == true) {
window.location.assign = "./player1wins.html";
} else {
playerTurn = "player2";
}
}
};
function player2Click(field) {
if (setMove("player2", field) == true) {
if (winCheck("player2") == true) {
window.location.assign = "./player2wins.html";
} else {
playerTurn = "player1";
}
}
};
// Speler 1 aan de beurt is
while (playerTurn == "player1") {
whosTurnElement.innerHTML = "speler 1";
fieldOneElement.onclick = player1Click(1);
fieldTwoElement.onclick = player1Click(2);
fieldTreeElement.onclick = player1Click(3);
fieldFourElement.onclick = player1Click(4);
fieldFiveElement.onclick = player1Click(5);
fieldSixElement.onclick = player1Click(6);
fieldSevenElement.onclick = player1Click(7);
fieldEightElement.onclick = player1Click(8);
fieldNineElement.onclick = player1Click(9);
}
// Speler 2 aan de beurt is
while (playerTurn == "player2") {
whosTurnElement.innerHTML = "speler 2";
fieldOneElement.onclick = player2Click(1);
fieldTwoElement.onclick = player2Click(2);
fieldTreeElement.onclick = player2Click(3);
fieldFourElement.onclick = player2Click(4);
fieldFiveElement.onclick = player2Click(5);
fieldSixElement.onclick = player2Click(6);
fieldSevenElement.onclick = player2Click(7);
fieldEightElement.onclick = player2Click(8);
fieldNineElement.onclick = player2Click(9);
}
&#13;
@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,300);
body {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: 'Open Sans', sans-serif;
background-color: #f0f3f4;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.2em;
}
h2 strong {
color: #83a7d9;
}
p {
font-size: 1em;
font-weight: 300;
line-height: 2em;
}
.container {
margin: 0 5vw;
}
header {
position: fixed;
top: 0;
left: 0;
z-index: 3;
width: 100vw;
height: 10vh;
background-color: white;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
header h1 {
text-align: center;
line-height: 10vh;
color: #83a7d9;
margin: 0;
}
main {
position: fixed;
top: 10vh;
bottom: 5vh;
left: 0;
width: 100vw;
height: 85vh;
overflow: hidden;
}
section.card {
width: 80vw;
background-color: white;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
text-align: center;
border-radius: 2vh;
padding: 5vh 5vw;
margin-top: 5vh;
}
table {
width: 100%;
height: 100%;
background-color: black;
border: none;
}
table td {
font-size: 4em;
color: #83a7d9;
background-color: white;
}
&#13;
<link href="https://necolas.github.io/normalize.css/4.0.0/normalize.css" rel="stylesheet" />
<header>
<div class="container">
<h1>Boter Kaas en Eiren</h1>
</div>
</header>
<main>
<div class="container">
<section class="card">
<h2>Het is <strong id="whosTurnElement">..</strong> zijn beurt.</h2>
<p id="errorOutput"></p>
</section>
<section class="card">
<table>
<tr>
<td id="fieldOne"></td>
<td id="fieldTwo"></td>
<td id="fieldTree"></td>
</tr>
<tr>
<td id="fieldFour"></td>
<td id="fieldFive"></td>
<td id="fieldSix"></td>
</tr>
<tr>
<td id="fieldSeven"></td>
<td id="fieldEight"></td>
<td id="fieldNine"></td>
</tr>
</table>
</section>
</div>
</main>
&#13;
答案 0 :(得分:2)
使用集合和数组方法很容易:
var subset = new Set([2, 4, 8, 1, 3]);
supersets.some(arr => arr.every(item => subset.has(item))); // true
答案 1 :(得分:1)
更多老派版本:
function find(superset, subset) {
for (var i = 0, l = superset.length; i < l; i++) {
var elementExists = function (el) { return subset.indexOf(el) > -1; }
if (superset[i].every(elementExists)) return true;
}
return false;
}
find(superset, subset);
答案 2 :(得分:0)
另一个循序渐进的解决方案。
function check(a) {
return supersets.some(function (b) {
return b.every(function (c) {
return a.some(function (d) {
return c === d;
});
});
});
}
var supersets = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]];
document.write(check([2, 4, 8, 1, 3]) + '<br>');
document.write(check([3, 7, 5]) + '<br>');
document.write(check([1, 2, 8]) + '<br>');