var boxId = ['boxMid','boxLeft','boxRight','boxTopMid','boxTopRight','boxTopLeft','boxLow Mid','boxLowLeft','boxLowRight'];
var turn = 0;
var pOne = "X";
var pTwo = "O";
var blankBox = "";
var boxMid = document.getElementById('boxMid').innerhtml;
var boxLeft = document.getElementById('boxLeft').innerhtml;
var boxRight = document.getElementById('boxRight').innerhtml;
var boxTopMid = document.getElementById('boxTopMid').innerhtml;
var boxTopLeft = document.getElementById('boxTopLeft').innerhtml;
var boxTopRight = document.getElementById('boxTopRight').innerhtml;
var boxLowMid = document.getElementById('boxLowMid').innerhtml;
var boxLowLeft = document.getElementById('boxLowLeft').innerhtml;
var boxLowRight = document.getElementById('boxLowRight').innerhtml;
var i = 0;
我的玩家获胜功能不起作用,我还有其他几个人看过它,所以我想我会得到一个外部视角。
function xWins() {
if(boxId[0,1,2] == 'X'||
boxTopLeft == "X" && boxTopMid == "X" && boxTopRight == "X"||
boxLowMid == "X" && boxLowLeft == "X" && boxLowRight == "X"||
boxMid == "X" && boxTopMid == "X" && boxLowMid == "X"||
boxLeft == "X" && boxTopLeft == "X" && boxLowLeft == "X"||
boxRight == "X" && boxTopRight == "X" && boxLowRight == "X"||
boxMid == "X" && boxTopLeft == "X" && boxLowRight == "X"||
boxMid == "X" && boxLowLeft == "X" && boxTopRight == "X" ) {
alert ('Player One Wins!');
turn = 1;
}
}
function oWins(){
if(boxId[0,1,2] == "O"||
boxTopLeft == "O" && boxTopMid == "O" && boxTopRight == "O"||
boxLowMid == "O" && boxLowLeft == "O" && boxLowRight == "O"||
boxMid == "O" && boxTopMid == "O" && boxLowMid == "O"||
boxLeft == "O" && boxTopLeft == "O" && boxLowLeft == "O"||
boxRight == "O" && boxTopRight == "O" && boxLowRight == "O"||
boxMid == "O" && boxTopLeft == "O" && boxLowRight == "O"||
boxMid == "O" && boxLowLeft == "O" && boxTopRight == "O" ) {
alert ('Player Two Wins!');
turn = 0;
}
}
function test(bx){
if(document.getElementById(boxId[bx]).innerHTML == ""){
if (turn<1) {
document.getElementById(boxId[bx]).innerHTML = pOne;
document.onclick (turn++);
oWins();
xWins();
}
if (turn == 1) {
document.getElementById(boxId[bx]).innerHTML = pTwo;
document.onclick (turn--);
oWins();
xWins();
}
}
}
function reset() {
for(i=0;i<boxId.length;i++)
document.getElementsByClassName('boxy')[i].innerHTML = blankBox;
}
据我所知,除了我的记分牌外,其他一切都有效,但是当我到达那里时我会想到这一点。请记住,我是一名高中学生,编码的实际经验不到2个月,所以我可能做了一些可能更容易,更干的事情。
答案 0 :(得分:0)
Bug的原因:你通过调用oWins函数告诉它oWins(),它通过你的其中一个匹配的OR条件的cascasde运行。所以你有两个问题。 1,oWins()总是通过OR语句测试为true,2,xWins()在每次评估为true的oWins()函数之后被调用。串联OR或类似条件是有问题的,通常很难调试。
if (turn == 1) {
document.getElementById(boxId[bx]).innerHTML = pTwo;
document.onclick (turn--);
oWins();
xWins();
}