以tik格式输出结果



我正在创建一个程序,该程序具有在tik tak toe游戏中可能获胜的恒定值。在函数内部,它应该确定是否已经进行了一次获胜的游戏,并向控制台输出X是否赢了,O是否输了,反之亦然。

例如console.log (diagonalWinInverse);应该输出X赢得O失去了

我刚刚开始,我有点不知道如何开始。

const rowWin = [
["O", "O", "O"],
["", "", ""],
["", "", ""]
]
const colWin = [
["", "X", ""],
["", "X", ""],
["", "X", ""]
]
const diagonalWin = [
["", "", "O"],
["", "O", ""],
["O", "", ""]
]
const diagonalWinInverse = [
["X", "", ""],
["", "X", ""],
["", "", "X"]
]
//This function will evaluate all the grids I have defined above
//And will tell the user which sign has won
let ask = prompt("Please Choose an option and only select the number:n 1 Row Win n2 Column Win n3 Diagonal Win n4 Inverse Diagonal Win")
function evaluatePlay(grid){

}

您可以创建一个函数并使用for循环来比较两个网格。如果任何一个网格中的单元格为空,则跳过检查。如果三个单元格相等,则表示存在匹配(有人赢了)。

const rowWin = [
["O", "O", "O"],
["", "", ""],
["", "", ""]
]
const colWin = [
["", "X", ""],
["", "X", ""],
["", "X", ""]
]
const diagonalWin = [
["", "", "O"],
["", "O", ""],
["O", "", ""]
]
const diagonalWinInverse = [
["X", "", ""],
["", "X", ""],
["", "", "X"]
]
function compare2grids(grid1, grid2)
{
let count = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (grid1[i][j] === "" || grid2[i][j] === "") {
continue;
}
else if (grid1[i][j] == grid2[i][j]) {
count++;
}
}
}
return count === 3;
}
function evaluatePlay(grid){
console.log(compare2grids(grid, diagonalWinInverse));
console.log(compare2grids(grid, diagonalWin));
}
evaluatePlay([
["X", "O", "O"],
["", "X", ""],
["O", "O", "X"]
]);

相关内容

  • 没有找到相关文章

最新更新