ECMAScript 6-635文字
f=(p,q,C)=>{B='';T=' ';L=(x)=>(B+x).length;M=(x,y)=>x>y?x:y;E=(a)=>{var x=L(a[0]),y=L(a[1]),q=M(x,y);for(;x++<q;)a[0]=T+a[0];for(;y++<q;)a[1]=T+a[1]};E(a=[p,q]);g=[m=n=o=i=t=z=0,0];s=[0,0];S=[0,0];w=2;O=(i)=>a[i]+(n+o>0?T+s[i]:B)+(o>0?(t?' ['+g[i]+']':' - '+[' 0',15,30,40,'Ad'][g[i]]+(z>0&&g[0]+g[1]==6&&i==w?' Deuce'+(z>1?' ('+z+')':B):B)):B)+'\n';while(W=C[i++]){w=--W;++o;if((d=++g[w])>M(t?6:3,(e=g[l=1-w])+1)){g=[o=z=0,0];j=++s[w];k=s[l];t=++n>11;if(j>M(5,m>4||k<6?k+1:6)){E(s);a[0]+=T+s[0];a[1]+=T+s[1];s=[n=0,0];++m;if(++S[w]>2)break}}else if(!t&&d+e>7){--g[w];--g[l];++z}}E(s);E(g);return O(0)+O(1)+(S[w]>2?(w?q:p)+' wins':B)}
コメント付き:
// Function f takes arguments:
// p - Player 1 name
// q - Player 2 name
// C - String of 1s and 2s representing points won by players.
f=(p,q,C)=>{
/* Empty String */ B='';
/* Space */ T=' ';
/* String Length Func. */ L=(x)=>(B+x).length;
/* Max Function */ M=(x,y)=>x>y?x:y;
/* Equalize Length Func. */ E=(a)=>{var x=L(a[0]),y=L(a[1]),q=M(x,y);for(;x++<q;)a[0]=T+a[0];for(;y++<q;)a[1]=T+a[1]};
/* No. of sets */ m=0;
/* No. games in set */ n=0;
/* No. points in game */ o=0;
/* Input Index */ i=0;
/* Output String */ E(a=[p,q]);
/* Current Game's Points */ g=[0,0];
/* Current Set's Games */ s=[0,0];
/* No. sets won */ S=[0,0];
/* Is a tiebreaker */ t=0;
/* No. of deuces */ z=0;
/* Current match result */ w=2;
/* Output Fnctn */ O=(i)=>a[i]+(n+o>0?T+s[i]:B)+(o>0?(t?' ['+g[i]+']':' - '+[' 0',15,30,40,'Ad'][g[i]]+(z>0&&g[0]+g[1]==6&&i==w?' Deuce'+(z>1?' ('+z+')':B):B)):B)+'\n';
while(W=C[i++]){
w=--W;
// w - index of winner of current game
// l - index of loser of current game
// d - winner of current point's game score
// e - loser of current point's game score
++o;
if((d=++g[w])>M(t?6:3,(e=g[l=1-w])+1)){
g=[0,0]; // Reset the game score.
o=z=0;
j=++s[w]; // j = Increment the winner's set score
k=s[l]; // k = Loser's set score
t=++n>11; // Is a tiebreak?
if(j>M(5,m>4||k<6?k+1:6)){
E(s);
a[0]+=T+s[0]; // Add to output
a[1]+=T+s[1]; // Add to output
s=[n=0,0]; // Reset current set's no. of games & no. of deuces.
++m; // Increment no. of sets.
if(++S[w]>2)break; // Increment winners no. sets won and check if match won.
}
}
else if(!t&&d+e>7){--g[w];--g[l];++z}// Check if deuces increased.
}
E(s); // Format sets strings.
E(g); // Format games strings.
return O(0)+O(1)+(S[w]>2?(w?q:p)+' wins':B);
}
テスト中
var tests=[
'',
'1',
'2',
'11',
'222',
'1111',
'2222',
'1112221',
'11122212',
'121212121',
'1212121212',
'1211222122111122111',
'12112221221111221112',
'121122212211112211122',
'1211222122111122111221',
'1111222211112222111122221111222211112222111122221212121212121212121',
'11111111111111111111111111111111111111111111111111111111111111111111111',
'111111111111111111111111111111111111111111111111111111111111111111111111',
'1111111111111111111111111111111111111111111111111111111111111111111111111'
];
for(var ex in tests) console.log('Test '+ex+'\n'+tests[ex]+'\n'+f('Wawrinka','Djokovic',tests[ex]));
テスト出力
Test 0
Wawrinka
Djokovic
Test 1
1
Wawrinka 0 - 15
Djokovic 0 - 0
Test 2
2
Wawrinka 0 - 0
Djokovic 0 - 15
Test 3
11
Wawrinka 0 - 30
Djokovic 0 - 0
Test 4
222
Wawrinka 0 - 0
Djokovic 0 - 40
Test 5
1111
Wawrinka 1
Djokovic 0
Test 6
2222
Wawrinka 0
Djokovic 1
Test 7
1112221
Wawrinka 0 - Ad
Djokovic 0 - 40
Test 8
11122212
Wawrinka 0 - 40
Djokovic 0 - 40 Deuce
Test 9
121212121
Wawrinka 0 - Ad
Djokovic 0 - 40
Test 10
1212121212
Wawrinka 0 - 40
Djokovic 0 - 40 Deuce (2)
Test 11
1211222122111122111
Wawrinka 1 - 40
Djokovic 1 - 30
Test 12
12112221221111221112
Wawrinka 1 - 40
Djokovic 1 - 40
Test 13
121122212211112211122
Wawrinka 1 - 40
Djokovic 1 - Ad
Test 14
1211222122111122111221
Wawrinka 1 - 40 Deuce
Djokovic 1 - 40
Test 15
1111222211112222111122221111222211112222111122221212121212121212121
Wawrinka 6 [10]
Djokovic 6 [ 9]
Test 16
11111111111111111111111111111111111111111111111111111111111111111111111
Wawrinka 6 6 5 - 40
Djokovic 0 0 0 - 0
Test 17
111111111111111111111111111111111111111111111111111111111111111111111111
Wawrinka 6 6 6
Djokovic 0 0 0
Wawrinka wins
Test 18
1111111111111111111111111111111111111111111111111111111111111111111111111
Wawrinka 6 6 6
Djokovic 0 0 0
Wawrinka wins