2016-07-22 5 views
-1

私のコードはケース2からケース4までうまく機能しますが、ケース5からケース9までは内部switch文の出力を生成するだけで、それ以外の場合はチェックしませんケース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" へのリンクなどがボックスのdivのクラス

+0

私は 'switch'ステートメント内でこれらのケースのコードに違いは見られません。おそらくあなたのロジックに問題がありますか? –

+2

私の神、182行...してください、[_minimal_例](http://stackoverflow.com/help/mcve)を提供してください。 – LostMyGlasses

+0

@TimBiegeleisenそれが私の考えです。私は違いは見当たりませんが、ケース5からケース9までは、ケース5からケース9の場合、それ以外の場合は実行されません。ケース ".5"のelseループがranFive == 1で、switch文の後にifそれ以外の場合は、 ".2"から ".4"の場合は簡単に実行されません。問題はどこですか? –

答えて

0

ありますを示すために使用されています。ここでは

はコードである 『2。』スペルミス、ranFourはループフォーでranfour、ranFiveはranfiveと書かれています。今正しく動作します。

関連する問題