Javascript 如何编写一个函数来检查tic tac toe js中的获胜者?
我试图使用dataset的值和win组合的索引来检查tic tac toe js中的赢家。也许我需要比较一下 我想使用一些和每一个方法,但它不工作Javascript 如何编写一个函数来检查tic tac toe js中的获胜者?,javascript,Javascript,我试图使用dataset的值和win组合的索引来检查tic tac toe js中的赢家。也许我需要比较一下 我想使用一些和每一个方法,但它不工作 winCombinations: [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [2, 4, 6], [0, 4, 8]
winCombinations: [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[2, 4, 6],
[0, 4, 8]
]
getWinner(value, player){
let boardCells = document.querySelectorAll('[data-cell]');
return this.winCombinations.some(combination => {
return combination.every(index => {
boardCells.forEach(cell => {
if(cell.dataset.cell.index === 'x'){
console.log('winner!!!')
}
})
})
})
}
我会这样做的
const X_CLASS = 'x';
const CIRCLE_CLASS = 'circle';
const boardCells = document.querySelectorAll('[data-cell]');
let circleTurn;
startGame();
function startGame() {
circleTurn = false;
boardCells.forEach((cell) => { cell.classList.remove(X_CLASS);
cell.classList.remove(CIRCLE_CLASS);
cell.removeEventListener('click', handleClick);
cell.addEventListener('click', handleClick, { once: true }); });
}
function handleClick(e) {
const cell = e.target; const currentClass = circleTurn ? CIRCLE_CLASS : X_CLASS;
placeMark(cell, currentClass);
if (checkWin(currentClass)) { endGame(false); }
else if (isDraw()) { endGame(true); }
else { swapTurns();
constx_类='X';
const CIRCLE_CLASS=‘CIRCLE’;
常数组合=[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const cellElements=document.querySelectorAll(“[data cell]”);
const board=document.getElementById(“board”);
const winningMessageElement=document.getElementById('winningMessage');
const restartButton=document.getElementById('restartButton');
const winningMessageTextElement=document.querySelector(“[data winning message text]”);
让圆圈转动;
startGame();
restartButton.addEventListener('click',StartName');
函数startName(){
circleTurn=false;
cellElements.forEach((单元格)=>{
cell.classList.remove(X_类);
cell.classList.remove(圈出类);
cell.removeEventListener('click',handleClick);
cell.addEventListener('click',handleClick{
一次:对
});
});
setBoardHoverClass();
winningMessageElement.classList.remove('show');
}
函数handleClick(e){
常量单元格=e.目标;
const currentClass=circleTurn?CIRCLE\u CLASS:X\u CLASS;
位置标记(单元格,当前类别);
if(checkWin(currentClass)){
终局(假);
}else if(isDraw()){
终局(正确);
}否则{
swapTurns();
setBoardHoverClass();
}
}
函数结束(平局){
如果(抽签){
winningMessageTextElement.innerText='Draw!';
}否则{
winningMessageTextElement.innerText=${
圆圈“O”:“X”
}
胜利!;
}
winningMessageElement.classList.add('show');
}
函数isDraw(){
返回[…单元格元素]。每个((单元格)=>{
返回cell.classList.contains(X_类)| cell.classList.contains(CIRCLE_类);
});
}
函数位置标记(单元格,currentClass){
cell.classList.add(currentClass);
}
函数swapTurns(){
circleTurn=!circleTurn;
}
函数setBoardHoverClass(){
板。类列表。删除(X_类);
板。类列表。移除(圈出类);
如果(圆圈){
board.classList.add(圈出类);
}否则{
board.classList.add(X_类);
}
}
函数checkWin(currentClass){
返回中奖组合。一些((组合)=>{
返回组合。每个((索引)=>{
返回cellElements[index].classList.contains(currentClass);
});
});
}
重新启动
您对组合的回调。每个都不会返回任何东西-因此组合。每个都返回未定义的(falsy),因此您永远找不到任何东西-而且,forEach看起来不对位执行代码时是否有任何错误消息?:)@Mona它没有给我一个错误这是来自webdevsimplified的代码,但我想用他的方法来编写我的代码