temp_algorithm.js
12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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{//아웃
var ran = Math.random()*0.2;
if(ran>0.197){
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(ran>0.1955){
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(ran>0.18){
var n = Math.random();
if(n <= 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 < n && n <=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{
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);
var score = [score1, score2];
return score;
}