temp_algorithm.js 9.38 KB
function pre_algorithm(hitter_list1, hitter_list2, pitcher1, pitcher2){//algorithm(hitter_list1, hitter_list2, pitcher1, pitcher2) => html에서 가지고와야 한다.
    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;
    document.getElementById('jstest').innerHTML="";
    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
            document.getElementById('jstest').innerHTML+=(ining+ "회 초"+ "<br\><br\>");
        }
        else if(home_away == 2){
            var hitter = hitter_list2[hitter_num2]; //hitter = hitter_list2[hitter_number2%9]
            var pitcher = pitcher1; //pitcher = pitcher2
            document.getElementById('jstest').innerHTML+=(ining+ "회 말"+ "<br\><br\>");
        }
        
        var num = Math.random(); 
        //hitter_number1 = hitter_number(kia); hitter_number2 = hitter_number(dusan);
        if(home_away == 1){
            document.getElementById('jstest').innerHTML+=(hitter_num1+"번 타자: "+hitter[0]+ "<br\><br\>");
        }
        else if(home_away == 2){
            document.getElementById('jstest').innerHTML+=(hitter_num2+"번 타자: "+hitter[0]+ "<br\><br\>");
        }  
        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;
                    document.getElementById('jstest').innerHTML+=("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;
                    document.getElementById('jstest').innerHTML+=("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;
                    document.getElementById('jstest').innerHTML+=("2루타!");
                   
                }
                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;
                    document.getElementById('jstest').innerHTML+=("2루타!");
                    
                }
                else{
                    switch(home_away){
                        case 1:
                            score1 = score1 + base3 + base2;
                            break;
                        case 2:
                            score2 = score2 + base3 + base2;
                            break;
                    }
                    base3 = 0;
                    base2 = 1;
                    base1 = 0;

                    document.getElementById('jstest').innerHTML+=("2루타!");
                  
                }
                
            }
            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;
                document.getElementById('jstest').innerHTML+=("3루타!");
              
            }
            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;
                document.getElementById('jstest').innerHTML+=("홈런!");
               
            }
        

            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;
                    }
                }
             document.getElementById('jstest').innerHTML+=("볼넷!");
            }

            else{//아웃
            out++;
            document.getElementById('jstest').innerHTML+=("아웃!");

            }
            
            document.getElementById('jstest').innerHTML+=("베이스 상태"+"<br\>"+ "1루   " + base1 + "<br\>"+"2루    " + base2 + "<br\>"+
            "3루    " + base3 + "<br\><br\>");///////////////////////////  
            document.getElementById('jstest').innerHTML+=("score: "+score1 +"vs"+score2+ "<br\>");
            document.getElementById('jstest').innerHTML+=("아웃카운트: "+out+"<br\><br\>");
            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;
                    }
                }
            }
            
    }
    document.getElementById('jstest').innerHTML+=("<br\><br\>"+"최종스코어 : "+score1+"vs"+score2);
}