/
Score.cs
executable file
·138 lines (114 loc) · 3.01 KB
/
Score.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tennis
{
public class Score
{
public readonly int a;
public readonly int b;
public Score (int a, int b)
{
this.a = a;
this.b = b;
}
public Score (Score score)
{
this.a = score.a;
this.b = score.b;
}
public Score Deuce ()
{
return this.a > 2 && this.a == this.b ? new Score (4, 4) : new Score (this);
}
public static Score Add (Score first, Score second)
{
return new Score (first.a + second.a, first.b + second.b);
}
public static Score Sum (List<Score> scores)
{
return new Score (scores.Aggregate (new Score (0, 0), Add));
}
public static Score AddWithDeuce (Score first, Score second)
{
return Add (first, second).Deuce ();
}
public static Score SumWithDeuce (List<Score> scores)
{
return new Score (scores.Aggregate (new Score (0, 0), AddWithDeuce));
}
public static Score AddWin (Score counter, Score score)
{
return (score.a > score.b) ? new Score (counter.a + 1, counter.b) : new Score (counter.a, counter.b + 1);
}
public static Score SumOfWins (List<Score> scores)
{
return new Score (scores.Aggregate (new Score (0, 0), AddWin));
}
public static List<Score> ScoreList (List<Score> scores)
{
List<Score> list = scores.Aggregate (new List<Score> () { new Score (0, 0) }, AddToScores);
list.RemoveAt (0);
return list;
}
public static List<Score> AddToScores (List<Score> list, Score score)
{
list.Add (Add (list.Last (), score));
return list;
}
public static List<Score> GameScoreList (List<Score> scores)
{
List<Score> list = scores.Aggregate (new List<Score> () { new Score (0, 0) }, AddToGameScores);
list.RemoveAt (0);
return list;
}
public static List<Score> AddToGameScores (List<Score> list, Score score)
{
list.Add (AddWithDeuce (list.Last (), score));
return list;
}
public static string ScoreString (Score s)
{
return s.a + " - " + s.b;
}
public static string ScoreStringHuman (Score s)
{
return Translate.Dict (s.a) + " - " + Translate.Dict (s.b);
}
public bool HasMin (int minPoints)
{
return Max () >= minPoints;
}
public bool HasAdvance (int advance)
{
return this.a >= this.b + advance || this.b >= this.a + advance;
}
public int Max ()
{
return this.a > this.b ? this.a : this.b;
}
public char Winner ()
{
return this.a > this.b ? 'a' : 'b';
}
public string WinScore ()
{
return this.a > this.b ? this.a + " : " + this.b : this.b + " : " + this.a;
}
public bool BothEqualTo (int minGames)
{
return (this.a == this.b) && (this.a == minGames);
}
public override bool Equals (object obj)
{
Score o = obj as Score;
return (this.a == o.a) && (this.b == o.b);
}
public override int GetHashCode ()
{
return base.GetHashCode ();
}
}
}