tic tac toe javascript应答检查器
我正在用javascript/jquery制作一个简单的井字游戏,我不知道如何检查是否有人赢了。 这是游戏场:tic tac toe javascript应答检查器,javascript,jquery,html,Javascript,Jquery,Html,我正在用javascript/jquery制作一个简单的井字游戏,我不知道如何检查是否有人赢了。 这是游戏场: <div id="gamefield"> <table border="0"> <tr> <td><img alt="" title="" src="img/empty.jpg" /></td>
<div id="gamefield">
<table border="0">
<tr>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
</tr>
<tr>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
</tr>
<tr>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
</tr>
</table>
</div>
这是随机开始代码:
var randomStart = Math.floor(Math.random() * 2);
执行Tictatcoe游戏,他们使用此功能检查谁赢了:
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
函数计算器(正方形){
常量行=[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for(设i=0;i
从
正方形是由从左到右、从上到下的九个正方形组成的数组。它包含已填充方块的
x
或o
,并返回获胜者的字母。您可以将模型保存在3,3大小的矩阵上,并检查行、列或对角线是否可以获胜
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}