从案例2到案例4,我的代码工作正常。但是从案例5到案例9,它只生成内部switch语句的输出,并且不检查案例5下面的if else条件,直到案例9。认为有一些小错误,我无法识别。请帮我看看问题出在哪里。
以下是代码:
$(document).ready(function() {
var a = [],
array,input,output,userclicked,comp,ranOne=0,ranTwo=0,ranThree=0,ranFour=0,ranFive=0,ranSix=0,ranSeven=0,ranEight=0,ranNine=0;
array = [".1,.2,.3,.4,.5,.6,.7,.8,.9"];
bootbox.dialog({
message:"Choose X or O",
title:"Tic Tac Toe game",
buttons:{
success:{
label:"X",
className: "btn-success",
callback:function(){
input="X";
output="O";
/*var randomInitialOutput = (function(ranarrayElement) {
var ranarray=[".1",".5",".7",".3",".9"];
ranarrayElement = ranarray[Math.floor(Math.random() * 5)]; //random element
return ranarrayElement;
})();*/
var randomInitialOutput=".1";
$(randomInitialOutput).html(output); //random initial output
a.push(output);
$(array.join("")).click(function() {
$(this).html(input);
var hello=this;//Crazy code starts
hello= hello.className.split(/\s+/)
var blaharray=["1","2","3","4","5","6","7","8","9"]
for(var i=0;i<hello.length;i++){
if(blaharray.indexOf(hello[i])>0){
userclicked="."+hello[i];
}
}//Crazy code ends to tell us what the user clicked
if(randomInitialOutput==".1"){
$(".1").off("click");
if((ranTwo==0)&&(ranThree==0)&&(ranFour==0)&&(ranFive==0)&&(ranSix==0)&&(ranSeven==0)&&(ranEight==0)&&(ranNine==0)){
switch (userclicked) {
case ".2":
ranTwo++;
$(".7").html(output);
break;
case ".3":
ranThree++;
$(".7").html(output);
break;
case ".4":
ranFour++;
$(".5").html(output);
break;
case ".5":
ranFive++;
$(".9").html(output);
break;
case ".6":
ranSix++;
$(".3").html(output);
break;
case ".7":
ranSeven++;
$(".3").html(output);
break;
case ".8":
ranEight++;
$(".3").html(output);
break;
case ".9":
ranNine++;
$(".3").html(output);
break;
}
}//if
if((ranTwo==1)&&(userclicked==".4")){
$(".5").html(output);
ranTwo=2;
}else if ((ranTwo==2)&&(userclicked==".3")) {
$(".9").html(output); //comp won
}else if ((ranTwo==2)&&(userclicked==".9")) {
$(".3").html(output); //comp won
}
if ((ranThree==1)&&(userclicked==".4")) {
$(".9").html(output);
ranThree=2;
}else if ((ranThree==2)&&(userclicked==".5")) {
$(".8").html(output); //comp won
}else if ((ranThree==2)&&(userclicked==".8")) {
$(".5").html(output); //comp won
}
if ((ranFour==1)&&(userclicked==".9")) {
$(".3").html(output);
ranfour=2;
}else if ((ranfour==2)&&(userclicked==".2")) {
$(".7").html(output);//comp won
}else if ((ranfour==2)&&(userclicked==".7")) {
$(".2").html(output);//comp won
}
if((ranFive==1)&&(userclicked==".7")){//error starts from number 5 upto 9
$(".3").html(output);
ranfive=2;
}else if ((ranfive==2)&&(userclicked==".2")) {
$(".6").html(output);//comp won
}else if ((ranfive==2)&&(userclicked==".6")) {
$(".2").html(output);//comp won
}
if((ranSix==1)&&(userclicked==".2")){
$(".7").html(output);
ranSix=2;
}else if ((ranSix==2)&&(userclicked==".4")) {
$(".5").html(output);//comp won
}else if ((ranSix==2)&&(userclicked==".5")) {
$(".4").html(output);//comp won
}
if ((ranSeven==1)&&(userclicked==".2")) {
$(".5").html(output);
ranSeven=2;
}else if ((ranSeven==2)&&(userclicked==".9")) {
$(".8").html(output);
ranSeven=3;
}else if ((ranSeven==3)&&(userclicked==".6")) {
$(".4").html(output);//tie
}else if ((ranSeven==3)&&(userclicked==".4")) {
$(".6").html(output); //tie
}
if((ranEight==1)&&(userclicked==".2")){
$(".5").html(output);
ranEight=2;
}else if ((ranEight==2)&&(userclicked==".9")) {
$(".7").html(output);//comp won
}else if ((ranEight==2)&&(userclicked==".7")) {
$(".9").html(output);//comp won
}
if ((ranNine==1)&&(userclicked==".2")) {
$(".7").html(output);
ranNine=2;
}else if ((ranNine==2)&&(userclicked==".4")) {
$(".5").html(output);//comp won
}else if ((ranNine==2)&&(userclicked==".5")) {
$(".4").html(output);//comp won
}
}// randomInitialOutput==".1"
else if (randomInitialOutput==".5") {
}else if (randomInitialOutput==".7") {
}else if (randomInitialOutput==".3") {
}else if (randomInitialOutput==".9") {
}
a.push($(this).html());
if (a.length == 9) {
$(".yo").html("Game Over!");
} //Game Over
});
}//callback
},
main:{
label:"O",
className: "btn-primary",
callback:function(){
input="O";
$(array).click(function() {
$(this).html(input);
a.push($(this).html());
if (a.length == 9) {
$(".yo").html("Game Over!");
} //Game Over
});
}//callback
}//main
}//buttons
});//BootBoxDialogue
});//document ready function
此处指向我的代码http://codepen.io/meow414/pen/rLJaPJ的链接 案例“.1”,“。2”等用于表示盒子div的类别
答案 0 :(得分:0)
有拼写错误,runFour在循环中编写了ranfour,而ranFive编写为ranfive。现在工作正常。