gpt4 book ai didi

javascript - JavaScript 中的 Tic Tac Toe

转载 作者:行者123 更新时间:2023-12-03 09:36:50 28 4
gpt4 key购买 nike

代码工作正常,但 compMove() 函数未按我的预期工作。我希望它执行以下操作:1. 如果用户(X)有获胜的举动,则阻止它。2. 如果计算机有获胜的棋步(O),则采取它。

这是该程序的链接:

This is the hyperlink to the jsfiddle implementation of my code

下面给出的是游戏的 javascript 部分。

$(document).ready(function () {
var count = 0;

function isAvailableField($this) {
return !$this.attr('data-move');
}

function userMove($this, move) {
$this.attr('data-move', move);
$this.addClass(move);
}

function reset() {
$('.move-x').removeClass('move-x');
$('.move-o').removeClass('move-o');
$('.grid td').attr('data-move', '');
count = 0;
}

$('.grid td').click(function () {
if (isAvailableField($(this))) {
userMove($(this), 'move-x');
count++;
checkWin();
compMove();
}
});

function compMove(){
if(isAvailableField('#field' + 1)&&($('#field'+2).attr('data-move') == $('#field' + 3).attr('data-move') || $('#field' + 5).attr('data-move') == $('#field' + 9).attr('data-move') || $('#field' + 4).attr('data-move')==$('#field' + 7).attr('data-move'))) {
userMove('#field'+1,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+2)&&($('#field'+1).attr('data-move')==$('#field'+3).attr('data-move')||$('#field'+5).attr('data-move')==$('#field'+8).attr('data-move'))){
userMove('#field'+2,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+3)&&($('#field'+2).attr('data-move')==$('#field'+1).attr('data-move')||$('#field'+5).attr('data-move')==$('#field'+7).attr('data-move')||$('#field'+6).attr('data-move')==$('#field'+9).attr('data-move'))){
userMove('#field'+3,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+4)&&($('#field'+1).attr('data-move')==$('#field'+7).attr('data-move')||$('#field'+5).attr('data-move')==$('#field'+6).attr('data-move'))){
userMove('#field'+4,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+5)&&($('#field'+2).attr('data-move')==$('#field'+8).attr('data-move')||$('#field'+1).attr('data-move')==$('#field'+9).attr('data-move')||$('#field'+3).attr('data-move')==$('#field'+7).attr('data-move')||$('#field'+4).attr('data-move')==$('#field'+6).attr('data-move'))){
userMove('#field'+5,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+6)&&($('#field'+4).attr('data-move')==$('#field'+5).attr('data-move')||$('#field'+3).attr('data-move')==$('#field'+9).attr('data-move'))){
userMove('#field'+6,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+7)&&($('#field'+1).attr('data-move')==$('#field'+4).attr('data-move')||$('#field'+5).attr('data-move')==$('#field'+3).attr('data-move')||$('#field'+8).attr('data-move')==$('#field'+9).attr('data-move'))){
userMove('#field'+7,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+8)&&($('#field'+2).attr('data-move')==$('#field'+5).attr('data-move')||$('#field'+7).attr('data-move')==$('#field'+9).attr('data-move'))){
userMove('#field'+8,'move-o');
count++;
checkWin();
}

else if(isAvailableField('#field'+9)&&($('#field'+1).attr('data-move')==$('#field'+5).attr('data-move')||$('#field'+3).attr('data-move')==$('#field'+6).attr('data-move')||$('#field'+7).attr('data-move')==$('#field'+8).attr('data-move'))){
userMove('#field'+9,'move-o');
count++;
checkWin();
}

else{
if(isAvailableField('#field'+5)){
userMove('#field'+5,'data-move');
count++;
checkWin();
}
else if(isAvailableField('#field'+1)){
userMove('#field'+1,'data-move');
count++;
checkWin();
}
else if(isAvailableField('#field'+9)){
userMove('#field'+9,'data-move');
count++;
checkWin();
}
else if(isAvailableField('#field'+8)){
userMove('#field'+8,'data-move');
count++;
checkWin();
}
else{
userMove('#field'+4,'data-move');
count++;
checkWin();
}
}

}

var winconditions = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 5, 7],
[1, 5, 9],
[3, 6, 9]
];

function judgmentDay(move) {
for (var i = 0; i < winconditions.length; i++) {
var line = winconditions[i];
var j = 0;
for (; j < line.length; j++) {
var num = line[j];
if ($('#field' + num).attr('data-move') != move) {
break;
}
}
if (j == line.length) {
return true;
}
}
}

function checkWin() {
if (count < 5) {
return;
}
if (judgmentDay('move-x')) {
alert("X wins!");
reset();
} else if (judgmentDay('move-o')) {
alert("O wins!");
reset();
} else if (count == 9) {
alert("It's a draw!");
reset();
}

}
});

最佳答案

您没有使用 jquery 元素作为 isAvailableField 和 userMove 的参数。例如

isAvailableField('#field'+3)

必须

isAvailableField($('#field'+3))

http://jsfiddle.net/ha4n3c27/是一个工作示例

关于javascript - JavaScript 中的 Tic Tac Toe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31311994/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com