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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bd5759605bb85cc0627c04bcbcee66593b513a8
4
- data.tar.gz: 0fa38355782c45b1fe77347a51da96657b0a2831
3
+ metadata.gz: 51fd014a2c833965e4531fe2161bb9e3f332ee39
4
+ data.tar.gz: 9c56807c77df6b4f088670d6405b03ebe79109ab
5
5
  SHA512:
6
- metadata.gz: 10074593f568f7929736fa2258a01f91fbe8761f955abdf01696b4a6498c546749cb85c2dd7764552fb9029bab95a414bdeb9c2c468cf513ad672cb2849efe12
7
- data.tar.gz: 152ca820399495c84fbc3dc12413e07d0004d8908f53c72334e371b3eecea6d06d7b83a5966fd3eeb04c77afbb98174973c1587c692bbab5741daecf06915828
6
+ metadata.gz: 493ff2541f5bd5549e3e64d93a15718acbbc3640d0728f33334b62f39bb3d43391fd69570c9225d75940576297107ecc0070e19a1ca4fe0a28c26ec1281f4902
7
+ data.tar.gz: 6c8adb590d4b2c802b478a0142825086f09ab74a861368ef29ce7ce8ca836b95ed2e1e4631efe536bd32a2c57794e524d631d55e5887c803037dc57fe3a6e65b
@@ -1,3 +1,3 @@
1
1
  class Tennis
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/tennis.rb CHANGED
@@ -1,101 +1,107 @@
1
1
  require "tennis/version"
2
2
 
3
3
  class Tennis
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
- @result = :error if validation_1 || validation_2
13
- end
14
- end
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
- # returns who won the match
17
- # :incomplete_match (bad input/incomplete match)
18
- # :error (bad input for sure)
19
- # 1 (player-1 won)
20
- # 2 (player-2 won)
21
- def result
22
- return @result if @result != :default
23
- return @result = (@scores.length == 4) ? two_sets : three_sets
24
- end
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
- # returns an array of points
27
- # returns (points_player_1 , points_player_2)
28
- # returns (0,0) for bad input
29
- def points
30
- @result = self.result
31
- (return [0, 0]) if @result == :error
32
- return (complete_match_points if @result == 1 || @result == 2) || incomplete_match_points
33
- end
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
- private
41
+ private
36
42
 
37
- # helper method: called by RESULT method for valid matches with 2 sets
38
- def two_sets
39
- set_results = []
40
- [0, 2].each do |i|
41
- # tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
42
- if @scores[i] == 7 || @scores[i+1] == 7
43
- set_results << (@scores[i] == 7 ? 1 : 2)
44
- # regular set victory - 6 games with a margin of 2
45
- else
46
- return :incomplete_match if ( @scores[i] - @scores[i + 1] ).abs < 2
47
- set_results << (@scores[i] == 6 ? 1 : 2)
48
- end
49
- end
50
- # incomplete match e.g: 6-4,5-3
51
- return (set_results[0] if set_results[0] == set_results[1]) || :incomplete_match
52
- end
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
- # helper method: called by RESULT method for valid matches with 3 sets
55
- def three_sets
56
- set_results = []
57
- [0, 2, 4].each do |i|
58
- # tie breaker (assuming a 7 point tie breaker) or a 7-5 score
59
- if @scores[i] == 7 || @scores[i + 1] == 7
60
- set_results << (@scores[i] == 7 ? 1 : 2)
61
- # regular set victory - 6 games with a margin of 2
62
- else
63
- return :incomplete_match if (@scores[i] - @scores[i + 1]).abs < 2
64
- set_results << (@scores[i] == 6 ? 1 : 2)
65
- end
66
- end
67
- # checks if the result has been decided in the first 2 sets
68
- # but the 3rd set is also present in the input
69
- return :error if set_results[0] == set_results[1]
70
- return set_results.count(1) ? 1 : 2
71
- end
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
- # helper method: called by POINTS for complete matches
74
- def complete_match_points
75
- points = [0, 0]
76
- @result = self.result
77
- points[@result - 1] = (@scores.length == 6) ? 12 : 14
78
- runner_up = (@result == 1) ? 2 : 1
79
- runner_up_points = player_points(runner_up)
80
- points[runner_up - 1] = runner_up_points < 8 ? runner_up_points : 8
81
- return points
82
- end
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
- # helper method: called by POINTS for incomplete matches
85
- def incomplete_match_points
86
- points = [0, 0]
87
- player_1_points = player_points(1)
88
- player_2_points = player_points(2)
89
- points[0] = player_1_points < 10 ? player_1_points : 10
90
- points[1] = player_2_points < 10 ? player_2_points : 10
91
- return points
92
- end
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
- # helper method: returns the POINTS of a player given the player number
95
- def player_points(player)
96
- player_scores = []
97
- @scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player - 1) }
98
- player_scores = player_scores.sort! { |x, y| y <=> x }
99
- return player_scores[0] + player_scores[1]
100
- end
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.}
metadata CHANGED
@@ -1,9 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tennis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
+ - Rohan Katyal
7
8
  - Carlos Puchol
8
9
  autorequire:
9
10
  bindir: bin