predict.js 8.99 KB
function add_predict(){
    var s="";
    s+=("<option value="+10+">"+10+"</option>");
    s+=("<option value="+20+">"+20+"</option>");
    s+=("<option value="+50+">"+50+"</option>");
    s+=("<option value="+70+">"+70+"</option>");
    s+=("<option value="+100+">"+100+"</option>");
    s+=("<option value="+500+">"+500+"</option>");
    s+=("<option value="+1000+">"+1000+"</option>");
    s+=("<option value="+5000+">"+5000+"</option>");
    s+=("<option value="+10000+">"+10000+"</option>");
    s+=("<option value="+50000+">"+50000+"</option>");
    s+=("<option value="+100000+">"+100000+"</option>");
    document.getElementById('pr').innerHTML=s;
}

function predict(value, hitter_list1, hitter_list2, pitcher1, pitcher2){
    var score=[0,0];
    var w=0;
    var h=0;
    var d=0;
    for(var i=0; i<value; i++){
        score=algorithms(hitter_list1, hitter_list2, pitcher1, pitcher2);
        if(score[0]>score[1]){
            w++;
        }else if(score[0]<score[1]){
            h++;
        }else{
            d++;
        } 
    }
    document.getElementById('jstest').innerHTML="예측 결과"+"<br\>"+"TEST "+value+"번"+"<br\><br\>"+"원정팀 승리 횟수 : "+w+"<br\>"
        +"홈팀 승리 횟수 : "+h+"<br\>"+"무승부 횟수 : "+d+"<br\><br\>"+"원정팀 승리 확률 : "+w*100/value+"%"+"<br\>"+"홈팀 승리 확률 : "+h*100/value+"%"+"<br\>";
}

function algorithms(hitter_list1, hitter_list2, pitcher1, pitcher2){
    var base1 = 0; var base2 = 0; var base3 = 0; //1이면 주자 있고 0이면 비어있는것
    var out = 0;  var ining = 1; var home_away = 1;//아웃카운트, 이닝, home이면 1이고 away이면 2. 
    var score1 = 0; var score2 = 0;
    var hitter_num1 = 1; var hitter_num2 = 1;
    var hit=false;
    while(true){
        if(ining == 9 && home_away == 2 && score1 < score2){//9회 초 후 home팀이 이기고 있을 때!
            break;
        }
        else if(ining==10 && home_away==1 && score1>score2 && hit==false){//9회 말 후 home팀이 이기고 있을 때!
            break;
        }
        else if(ining>9 && score1 < score2){//9회 말 후 home팀이 이기고 있을 때!
            break;
        }
        else if(ining>=11 && home_away==1 && hit==false && score1>score2){//연장 이후 원정팀이 이기고 있을 때!
            break;
        }
        else if(ining>=10 && home_away==2 && hit==true && score1<score2){//끝내기
            break;
        }
        else if(ining==13 && score1==score2){//무승부
            break;
        }

        if(home_away == 1){
            var hitter = hitter_list1[hitter_num1]; //hitter = hitter_list1[hitter_number1%9]
            var pitcher = pitcher2; //pitcher = pitcher1

        }
        else if(home_away == 2){
            var hitter = hitter_list2[hitter_num2]; //hitter = hitter_list2[hitter_number2%9]
            var pitcher = pitcher1; //pitcher = pitcher2

        }
        
        var num = Math.random(); 
        //hitter_number1 = hitter_number(kia); hitter_number2 = hitter_number(dusan);
        var one = 0; var two = 0; var three = 0; var homerun = 0;
        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];
        hit=true;
            if(num <= one){ //안타
                var num1 = Math.random();
                if(num1 <= 0.5){
                    switch(home_away){
                        case 1:
                            score1 = score1 + base3;
                            break;
                        case 2:
                            score2 = score2 + base3;
                            break;
                    }               
                    base3 = base2;
                    base2 = base1;
                    base1 = 1;
                    
                }
                else{
                    switch(home_away){
                        case 1:
                            score1 = score1 + base3 + base2;
                            break;
                        case 2:
                            score2 = score2 + base3 + base2;
                            break;
                    }               
                    base3 = base1;
                    base2 = 0;
                    base1 = 1;
                   
                }
                    
            }
            else if((one < num) && (num <= one + two)){//2루타
                var num2 = Math.random();
                if(num2 <= 0.4){
                    switch(home_away){
                        case 1:
                            score1 = score1 + base3 + base2;
                            break;
                        case 2:
                            score2 = score2 + base3 + base2;
                            break;
                    }
                    base3 = base1;
                    base2 = 1;
                    base1 = 0;
                    
                }
                else if(0.4 < num2 && num2 <=0.7){
                    switch(home_away){
                        case 1:
                            score1 = score1 + base3 + base2 + base1;
                            break;
                        case 2:
                            score2 = score2 + base3 + base2 + base1;
                            break;
                    }
                    base3 = 0;
                    base2 = 1;
                    base1 = 0;
                    
                }
                else{
                    switch(home_away){
                        case 1:
                            score1 = score1 + base3 + base2;
                            break;
                        case 2:
                            score2 = score2 + base3 + base2;
                            break;
                    }
                    base3 = 0;
                    base2 = 1;
                    base1 = 0;

                }
                
            }
            else if((one + two < num) && (num <= one + two + three)){//3루타
                switch(home_away){
                    case 1:
                        score1 = score1 + base3 + base2 + base1;
                        break;
                    case 2:
                        score2 = score2 + base3 + base2 + base1;
                        break;
                }
                base3 = 1;
                base2 = 0;
                base1 = 0;
                
            }
            else if(one + two + three < num && num <= one + two + three + homerun){//홈런
                switch(home_away){
                    case 1:
                        score1 = score1 + base3 + base2 + base1 + 1;
                        break;
                    case 2:
                        score2 = score2 + base3 + base2 + base1 + 1;
                        break;
                }
                base3 = 0;
                base2 = 0;
                base1 = 0;
                
            }
        

            else if( pitcher[2] < num && (num <= pitcher[2] +pitcher[1]*0.01)){//볼넷
                if(base1==1 && base2==1 && base3==1){//만루 밀어내기 득점 -> 스코어 ++
                    if(home_away==1){
                        score1++;
                    }
                    else{
                        score2++;
                    }
                }
                else{//스코어 추가되지 않는 경우
                    if((base2==1 && base1==0 )||(base3==1 && base1==0)||(base2==1&&base3==1&&base1==0)){//2루만 채워져있거나 3루만 채워져있거나 2,3루만 채워져있거나
                        base1=1;
                    }
                    else if(base1==1 && base3==1 && base2==0){//1루 3루 채워져있고 2루 비워져있거나
                        base2=base1;
                        base1=1;
                    }
                    else{
                        base3 = base2;
                        base2 = base1;
                        base1 = 1;
                    }
                }
            
            }

            else{//아웃
            out++;
            
            }
            
            if(out >= 3){
                switch(home_away){
                    case 1:
                        home_away = 2;
                        hit=false;
                        break;
                    case 2:
                        home_away = 1;
                        ining++;
                        hit=false;
                        break;
                }
                out=0;
                base1=0; base2=0; base3=0;
            }
            else{
                if(home_away == 1){
                    hitter_num1++;
                    if(hitter_num1>=10){
                        hitter_num1=1;
                    }
                }
                else if(home_away == 2){
                    hitter_num2++;
                    if(hitter_num2>=10){
                        hitter_num2=1;
                    }
                }
            }
            
    }
    var score = [score1, score2];
    return score;
}