tennis 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51fd014a2c833965e4531fe2161bb9e3f332ee39
4
- data.tar.gz: 9c56807c77df6b4f088670d6405b03ebe79109ab
3
+ metadata.gz: ea95bc7f40b6be2a97429ab809d5ed3f94ea995c
4
+ data.tar.gz: 80ba1df7f16b3a264e03ca3bc6f8f700982b434b
5
5
  SHA512:
6
- metadata.gz: 493ff2541f5bd5549e3e64d93a15718acbbc3640d0728f33334b62f39bb3d43391fd69570c9225d75940576297107ecc0070e19a1ca4fe0a28c26ec1281f4902
7
- data.tar.gz: 6c8adb590d4b2c802b478a0142825086f09ab74a861368ef29ce7ce8ca836b95ed2e1e4631efe536bd32a2c57794e524d631d55e5887c803037dc57fe3a6e65b
6
+ metadata.gz: 94c17c9714bd48a45974e8d4fd641a0f80083aa6fd64eddd540bf1c3116dd8767d3103738260e81ad39a04b6d7290f163531cff6eac8db32444d8f7d98c90fb6
7
+ data.tar.gz: 96eda3aad474683c9c493637eee575aae279d247a57b9baaf7f4113e4e340f9d0a6d8ed81859b4f4c94127c15a71d5967eacd23c681ccf34ca8c3ae538425987
@@ -1,3 +1,3 @@
1
1
  class Tennis
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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
- [0, 2].each do |i|
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
- [0, 2, 4].each do |i|
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tennis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rohan Katyal