tennis 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/tennis/version.rb +1 -1
- data/lib/tennis.rb +95 -89
- data/spec/tennis_spec.rb +81 -0
- data/tennis.gemspec +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 51fd014a2c833965e4531fe2161bb9e3f332ee39
|
|
4
|
+
data.tar.gz: 9c56807c77df6b4f088670d6405b03ebe79109ab
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 493ff2541f5bd5549e3e64d93a15718acbbc3640d0728f33334b62f39bb3d43391fd69570c9225d75940576297107ecc0070e19a1ca4fe0a28c26ec1281f4902
|
|
7
|
+
data.tar.gz: 6c8adb590d4b2c802b478a0142825086f09ab74a861368ef29ce7ce8ca836b95ed2e1e4631efe536bd32a2c57794e524d631d55e5887c803037dc57fe3a6e65b
|
data/lib/tennis/version.rb
CHANGED
data/lib/tennis.rb
CHANGED
|
@@ -1,101 +1,107 @@
|
|
|
1
1
|
require "tennis/version"
|
|
2
2
|
|
|
3
3
|
class Tennis
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
def initialize(scores)
|
|
5
|
+
@scores = scores != 'default-1' && scores != 'default-2' ? scores.split(/[-,,]/).map(&:to_i) : scores
|
|
6
|
+
@result = (1 if scores == 'default-1') || (2 if scores == 'default-2') || :default
|
|
7
|
+
if @result == :default
|
|
8
|
+
# to check if score for only 1 set has been input
|
|
9
|
+
validation_1 = @scores.length == 2
|
|
10
|
+
# to check if any input > 7
|
|
11
|
+
validation_2 = @scores.any? { |score| score > 7 }
|
|
12
|
+
# to check if one of the input is 7 and the other is not 6
|
|
13
|
+
# bad tie break input
|
|
14
|
+
validation_3 = false
|
|
15
|
+
@scores.each_slice(2).each {|r| validation_3 = true if r.any? {|score| score == 7} && !r.any? {|score| score == 6} }
|
|
16
|
+
@result = :error if validation_1 || validation_2 || validation_3
|
|
17
|
+
# if set score is not complete eg: 4-6,7-6,4-1
|
|
18
|
+
@scores.each_slice(2).each {|r| @result = :incomplete_match if r[0] < 6 && r[1] < 6 }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
# returns who won the match
|
|
23
|
+
# :incomplete_match (bad input/incomplete match)
|
|
24
|
+
# :error (bad input for sure)
|
|
25
|
+
# 1 (player-1 won)
|
|
26
|
+
# 2 (player-2 won)
|
|
27
|
+
def result
|
|
28
|
+
return @result if @result != :default
|
|
29
|
+
return @result = (@scores.length == 4) ? two_sets : three_sets
|
|
30
|
+
end
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
# returns an array of points
|
|
33
|
+
# returns (points_player_1 , points_player_2)
|
|
34
|
+
# returns (0,0) for bad input
|
|
35
|
+
def points
|
|
36
|
+
@result = self.result
|
|
37
|
+
(return [0, 0]) if @result == :error
|
|
38
|
+
return (complete_match_points if @result == 1 || @result == 2) || incomplete_match_points
|
|
39
|
+
end
|
|
34
40
|
|
|
35
|
-
|
|
41
|
+
private
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
# helper method: called by RESULT method for valid matches with 2 sets
|
|
44
|
+
def two_sets
|
|
45
|
+
set_results = []
|
|
46
|
+
[0, 2].each do |i|
|
|
47
|
+
# tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
|
|
48
|
+
if @scores[i] == 7 || @scores[i+1] == 7
|
|
49
|
+
set_results << (@scores[i] == 7 ? 1 : 2)
|
|
50
|
+
# regular set victory - 6 games with a margin of 2
|
|
51
|
+
else
|
|
52
|
+
return :incomplete_match if ( @scores[i] - @scores[i + 1] ).abs < 2
|
|
53
|
+
set_results << (@scores[i] == 6 ? 1 : 2)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
# incomplete match e.g: 6-4,5-3
|
|
57
|
+
return (set_results[0] if set_results[0] == set_results[1]) || :incomplete_match
|
|
58
|
+
end
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
60
|
+
# helper method: called by RESULT method for valid matches with 3 sets
|
|
61
|
+
def three_sets
|
|
62
|
+
set_results = []
|
|
63
|
+
[0, 2, 4].each do |i|
|
|
64
|
+
# tie breaker (assuming a 7 point tie breaker) or a 7-5 score
|
|
65
|
+
if @scores[i] == 7 || @scores[i + 1] == 7
|
|
66
|
+
set_results << (@scores[i] == 7 ? 1 : 2)
|
|
67
|
+
# regular set victory - 6 games with a margin of 2
|
|
68
|
+
else
|
|
69
|
+
return :incomplete_match if (@scores[i] - @scores[i + 1]).abs < 2
|
|
70
|
+
set_results << (@scores[i] == 6 ? 1 : 2)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
# checks if the result has been decided in the first 2 sets
|
|
74
|
+
# but the 3rd set is also present in the input
|
|
75
|
+
return :error if set_results[0] == set_results[1]
|
|
76
|
+
return set_results.count(1) == 2 ? 1 : 2
|
|
77
|
+
end
|
|
72
78
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
# helper method: called by POINTS for complete matches
|
|
80
|
+
def complete_match_points
|
|
81
|
+
points = [0, 0]
|
|
82
|
+
@result = self.result
|
|
83
|
+
points[@result - 1] = (@scores.length == 6) ? 12 : 14
|
|
84
|
+
runner_up = (@result == 1) ? 2 : 1
|
|
85
|
+
runner_up_points = player_points(runner_up)
|
|
86
|
+
points[runner_up - 1] = runner_up_points < 8 ? runner_up_points : 8
|
|
87
|
+
return points
|
|
88
|
+
end
|
|
83
89
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
# helper method: called by POINTS for incomplete matches
|
|
91
|
+
def incomplete_match_points
|
|
92
|
+
points = [0, 0]
|
|
93
|
+
player_1_points = player_points(1)
|
|
94
|
+
player_2_points = player_points(2)
|
|
95
|
+
points[0] = player_1_points < 10 ? player_1_points : 10
|
|
96
|
+
points[1] = player_2_points < 10 ? player_2_points : 10
|
|
97
|
+
return points
|
|
98
|
+
end
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
# helper method: returns the POINTS of a player given the player number
|
|
101
|
+
def player_points(player)
|
|
102
|
+
player_scores = []
|
|
103
|
+
@scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player - 1) }
|
|
104
|
+
player_scores = player_scores.sort! { |x, y| y <=> x }
|
|
105
|
+
return player_scores[0] + player_scores[1]
|
|
106
|
+
end
|
|
101
107
|
end
|
data/spec/tennis_spec.rb
CHANGED
|
@@ -11,7 +11,88 @@ describe Tennis, "#scores" do
|
|
|
11
11
|
score = Tennis.new("6-4, 6-4")
|
|
12
12
|
expect(score.result).to eq 1
|
|
13
13
|
end
|
|
14
|
+
|
|
15
|
+
it "finds the winner properly in three sets" do
|
|
16
|
+
score = Tennis.new("4-6, 6-2, 3-6")
|
|
17
|
+
expect(score.result).to eq 2
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "finds the winner properly in two sets with tie break" do
|
|
21
|
+
score = Tennis.new("6-4, 7-6")
|
|
22
|
+
expect(score.result).to eq 1
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "finds the winner properly in three sets with tie break" do
|
|
26
|
+
score = Tennis.new("6-4, 6-7, 7-6")
|
|
27
|
+
expect(score.result).to eq 1
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "reports incomplete match score (set 1-1)" do
|
|
31
|
+
score = Tennis.new("6-4,4-6")
|
|
32
|
+
expect(score.result).to eq :incomplete_match
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "reports incomplete match score (set incomplete)" do
|
|
36
|
+
score = Tennis.new("6-4,4-5")
|
|
37
|
+
expect(score.result).to eq :incomplete_match
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "checks invalid score: difference in games won < 2" do
|
|
41
|
+
score = Tennis.new("6-5,4-6,7-6")
|
|
42
|
+
expect(score.result).to eq :incomplete_match
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "checks invalid score: only 1 set input" do
|
|
46
|
+
score = Tennis.new("6-4")
|
|
47
|
+
expect(score.result).to eq :error
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "checks invalid score: result decided in first 2 sets but 3rd set input" do
|
|
51
|
+
score = Tennis.new("6-4,6-4,4-6")
|
|
52
|
+
expect(score.result).to eq :error
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "checks invalid score: bad input for tie break" do
|
|
56
|
+
score = Tennis.new("7-0,4-6,6-2")
|
|
57
|
+
expect(score.result).to eq :error
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "checks invalid score: no score > 7" do
|
|
61
|
+
score = Tennis.new("8-4,2-6,6-1")
|
|
62
|
+
expect(score.result).to eq :error
|
|
63
|
+
end
|
|
64
|
+
|
|
14
65
|
end
|
|
15
66
|
|
|
16
67
|
describe Tennis, "#points" do
|
|
68
|
+
it "returns 12 points in a three set win in a complete match" do
|
|
69
|
+
score = Tennis.new("6-4, 4-6, 6-4")
|
|
70
|
+
expect(score.points).to eq [12,8]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "returns 14 points in two set win in a complete match" do
|
|
74
|
+
score = Tennis.new("4-6, 4-6")
|
|
75
|
+
expect(score.points).to eq [8,14]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "returns a max of 8 points for the runners up in a complete match" do
|
|
79
|
+
score = Tennis.new("4-6, 6-2, 3-6")
|
|
80
|
+
expect(score.points).to eq [8,12]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "return a max of 10 points for each player in an incomplete match" do
|
|
84
|
+
score = Tennis.new("7-6,6-7")
|
|
85
|
+
expect(score.points).to eq [10,10]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "return a max of 10 points for each player in an incomplete match" do
|
|
89
|
+
score = Tennis.new("7-6,4-6,4-1")
|
|
90
|
+
expect(score.points).to eq [10,10]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it "return [0,0] for bad input" do
|
|
94
|
+
score = Tennis.new("8-1")
|
|
95
|
+
expect(score.points).to eq [0,0]
|
|
96
|
+
end
|
|
97
|
+
|
|
17
98
|
end
|
data/tennis.gemspec
CHANGED
|
@@ -6,7 +6,7 @@ require 'tennis/version'
|
|
|
6
6
|
Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "tennis"
|
|
8
8
|
spec.version = Tennis::VERSION
|
|
9
|
-
spec.authors = ["Carlos Puchol"]
|
|
9
|
+
spec.authors = ["Rohan Katyal", "Carlos Puchol"]
|
|
10
10
|
spec.email = ["cpg+git@amahi.org"]
|
|
11
11
|
spec.summary = %q{A gem to manage tennis scores.}
|
|
12
12
|
spec.description = %q{A gem to manage and validate tennis scores.}
|