tennis 0.1.0 → 0.1.1
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 +21 -3
- data/spec/tennis_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ea95bc7f40b6be2a97429ab809d5ed3f94ea995c
|
|
4
|
+
data.tar.gz: 80ba1df7f16b3a264e03ca3bc6f8f700982b434b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 94c17c9714bd48a45974e8d4fd641a0f80083aa6fd64eddd540bf1c3116dd8767d3103738260e81ad39a04b6d7290f163531cff6eac8db32444d8f7d98c90fb6
|
|
7
|
+
data.tar.gz: 96eda3aad474683c9c493637eee575aae279d247a57b9baaf7f4113e4e340f9d0a6d8ed81859b4f4c94127c15a71d5967eacd23c681ccf34ca8c3ae538425987
|
data/lib/tennis/version.rb
CHANGED
data/lib/tennis.rb
CHANGED
|
@@ -5,6 +5,8 @@ class Tennis
|
|
|
5
5
|
@scores = scores != 'default-1' && scores != 'default-2' ? scores.split(/[-,,]/).map(&:to_i) : scores
|
|
6
6
|
@result = (1 if scores == 'default-1') || (2 if scores == 'default-2') || :default
|
|
7
7
|
if @result == :default
|
|
8
|
+
# check blank input ''
|
|
9
|
+
@result = :error if @scores.any? { |score| score.nil? || score == 0 }
|
|
8
10
|
# to check if score for only 1 set has been input
|
|
9
11
|
validation_1 = @scores.length == 2
|
|
10
12
|
# to check if any input > 7
|
|
@@ -15,10 +17,26 @@ class Tennis
|
|
|
15
17
|
@scores.each_slice(2).each {|r| validation_3 = true if r.any? {|score| score == 7} && !r.any? {|score| score == 6} }
|
|
16
18
|
@result = :error if validation_1 || validation_2 || validation_3
|
|
17
19
|
# 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 }
|
|
20
|
+
@scores.each_slice(2).each {|r| @result = :incomplete_match if r[0] < 6 && r[1] < 6 } if @result != :error
|
|
19
21
|
end
|
|
20
22
|
end
|
|
21
23
|
|
|
24
|
+
# getter method for the original score string
|
|
25
|
+
def get_result
|
|
26
|
+
return self.result.to_s
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# flip score ( P1-P2 to P2-P1)
|
|
30
|
+
# returns the flipped score in string
|
|
31
|
+
def flip
|
|
32
|
+
flipped_score = ''
|
|
33
|
+
(0...@scores.length).step(2).each do |i|
|
|
34
|
+
flipped_score = flipped_score + @scores[i+1].to_s + '-' + @scores[i].to_s
|
|
35
|
+
flipped_score = flipped_score + ',' if !(i == @scores.length-2)
|
|
36
|
+
end
|
|
37
|
+
return flipped_score
|
|
38
|
+
end
|
|
39
|
+
|
|
22
40
|
# returns who won the match
|
|
23
41
|
# :incomplete_match (bad input/incomplete match)
|
|
24
42
|
# :error (bad input for sure)
|
|
@@ -43,7 +61,7 @@ class Tennis
|
|
|
43
61
|
# helper method: called by RESULT method for valid matches with 2 sets
|
|
44
62
|
def two_sets
|
|
45
63
|
set_results = []
|
|
46
|
-
|
|
64
|
+
(0...@scores.length).step(2).each do |i|
|
|
47
65
|
# tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
|
|
48
66
|
if @scores[i] == 7 || @scores[i+1] == 7
|
|
49
67
|
set_results << (@scores[i] == 7 ? 1 : 2)
|
|
@@ -60,7 +78,7 @@ class Tennis
|
|
|
60
78
|
# helper method: called by RESULT method for valid matches with 3 sets
|
|
61
79
|
def three_sets
|
|
62
80
|
set_results = []
|
|
63
|
-
|
|
81
|
+
(0...@scores.length).step(2).each do |i|
|
|
64
82
|
# tie breaker (assuming a 7 point tie breaker) or a 7-5 score
|
|
65
83
|
if @scores[i] == 7 || @scores[i + 1] == 7
|
|
66
84
|
set_results << (@scores[i] == 7 ? 1 : 2)
|
data/spec/tennis_spec.rb
CHANGED
|
@@ -62,6 +62,11 @@ describe Tennis, "#scores" do
|
|
|
62
62
|
expect(score.result).to eq :error
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
it "checks invalid score: blank score '' " do
|
|
66
|
+
score = Tennis.new("")
|
|
67
|
+
expect(score.result).to eq :error
|
|
68
|
+
end
|
|
69
|
+
|
|
65
70
|
end
|
|
66
71
|
|
|
67
72
|
describe Tennis, "#points" do
|
|
@@ -95,4 +100,9 @@ describe Tennis, "#points" do
|
|
|
95
100
|
expect(score.points).to eq [0,0]
|
|
96
101
|
end
|
|
97
102
|
|
|
103
|
+
it "checks invalid score: blank score '' " do
|
|
104
|
+
score = Tennis.new("")
|
|
105
|
+
expect(score.points).to eq :error
|
|
106
|
+
end
|
|
107
|
+
|
|
98
108
|
end
|