algorithm.js 5 KB
function algorithm(){//algorithm(hitter_list1, hitter_list2, pitcher1, pitcher2) => html에서 가지고와야 한다.
    var base1 = 0; base2 = 0; base3 = 0; //1이면 주자 있고 0이면 비어있는것
    var out = 0;  ining = 1; home_away = 1;//아웃카운트, 이닝, home이면 1이고 away이면 2.
    var hitter_number1 = 0; hitter_number2 = 0; //이전 타석 타자 번호 
    var score = 0; score2 = 0;
    while(true){
        document.write(ining, '회', "<br><br>");
        if(ining >= 9 && home_away == 2 && score1 != score2){
            score = []
            score.push(score1); score.push(score2);
            return score;
        }
        if(home_away == 1){
            var hitter = kia_hitter[hitter_number1%9]; //hitter = hitter_list1[hitter_number1%9]
            var pitcher = dusan_pitcher[1]; //pitcher = pitcher1
        }
        else if(home_away == 2){
            var hitter = dusan_hitter[hitter_number2%9]; //hitter = hitter_list2[hitter_number2%9]
            var pitcher = kia_pitcher[1]; //pitcher = pitcher2
        }
        var num = Math.random(); 
        document.write(num, "<br><br>");//////////////////////
        //hitter_number1 = hitter_number(kia); hitter_number2 = hitter_number(dusan);    
        document.write(hitter[0], "<br><br>")
        var one = 0; var two = 0; var three = 0; var homerun = 0;
        if( num <= kia_pitcher[1][2]){
            one = pitcher[2] * (hitter[2] - hitter[3] - hitter[4] - hitter[5])/hitter[2];
            two = pitcher[2] * hitter[3]/hitter[2];
            three = pitcher[2] * hitter[4]/hitter[2];
            homerun = pitcher[2] * hitter[5]/hitter[2];
            document.write("확률" + "<br>" + "1루타   " + one, "<br>","2루타  " +
                two, "<br>","3루타  "+ three, "<br>","홈런  " + homerun, "<br><br>");//////////////////////////////           
                
            if(num <= one){ //안타
                var num1 = Math.random();
                if(num1 <= 0.5){
                    switch(home_away){
                        case home_away == 1:
                            score1 = score1 + base3;
                            hitter_number1++;
                        case home_away == 2:
                            score2 = score2 + base3;
                            hitter_number2++;
                    }                
                    base3 = base2;
                    base2 = base1;
                    base1 = 1;
                    document.write("1루타!")
                    document.write("베이스 상태"+"<br>", "1루   " + base1 + "<br>", "2루    " + base2 + "<br>",
                    "3루    " + base3 + "<br><br>")///////////////////////////  
                    document.write("score: "+score+ "<br><br><br>")
                }
                if(num1 > 0.5){
                    switch(home_away){
                        case home_away == 1:
                            score1 = score1 + base3 + base2;
                            hitter_number1++;
                        case home_away == 2:
                            score2 = score2 + base3 + base2;
                            hitter_number2++;
                    }               
                    base3 = base1;
                    base2 = 0;
                    base1 = 1;
                    document.write("1루타!")
                    document.write("베이스 상태"+"<br>", "1루   " + base1 + "<br>", "2루    " + base2 + "<br>",
                    "3루    " + base3 + "<br><br>")///////////////////////////  
                    document.write("score: "+score+ "<br><br><br>")
                }
                    
            }
            else if(one < num && num <= one + two){//2루타
                var num2 = Math.random();
                if(num2 <= 0.4){
                    switch(home_away){
                        case home_away == 1:
                            score1 = score1 + base3 + base2;
                            hitter_number1++;
                        case home_away == 2:
                            score2 = score2 + base3 + base2;
                            hitter_number2++;
                    }
                    base3 = base1;
                    base2 = 1;
                    base1 = 0;
                    document.write("2루타!")
                    document.write("베이스 상태"+"<br>", "1루   " + base1 + "<br>", "2루    " + base2 + "<br>",
                    "3루    " + base3 + "<br><br>")///////////////////////////  
                    document.write("score: "+score+ "<br><br><br>")
                }
                if(0.4 < num2 && num2 <=0.7){
                    switch(home_away){
                        case home_away == 1:
                            score1 = score1 + base3 + base2 + base1;
                            hitter_number1++;
                        case home_away == 2:
                            score2 = score2 + base3 + base2 + base1;
                            hitter_number2++;
                    }
                    base3 = 0;
                }
            }
        }
    }
}