2016-07-22 2 views
-1

내 코드는 케이스 2에서 케이스 4까지 잘 작동합니다. 케이스 5에서 케이스 9까지는 내부 스위치 명령문의 출력을 생성하고 다른 경우는 확인하지 않습니다 케이스 5 ~ 케이스 9까지의 조건. 약간의 사소한 오류가 있다고 생각합니다. 식별 할 수 없습니다. 문제가 어디 있는지 보도록 도와주세요. 여기내 코드가 케이스 5에서 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 케이스 "0.1"에 대한 링크 등이 박스 사업부의 클래스

+0

'switch' 문 안에 이런 경우 코드의 차이점은 없습니다. 논리에 문제가 있습니까? –

+2

내 하나님, 182 라인 ... 제발, 제공 [_minimal_ 예] (http://stackoverflow.com/help/mcve). – LostMyGlasses

+0

@TimBiegeleisen 그것이 내가 생각하는 것입니다. 나는 차이점을 찾지 못했지만 case 5에서 9까지는 case 5에서 9까지 else와 관련이 있으면 실행되지 않습니다. case ".5"의 else 루프가 ranFive == 1 인 경우 switch 문 다음에 if else는 ".2"에서 ".4"의 경우 쉽게 실행되지 않습니다. 어디에 문제가 있습니까? –

답변

0

있습니다을 표시하는 데 사용됩니다 여기에

코드는 "2." 맞춤법 오류, ranFour은 루프와 ranfive에 ranfive 작성된 ranfour 작성됩니다. 이제 올바르게 작동합니다.