tennis 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Makefile +3 -0
- data/README.md +1 -1
- data/lib/tennis/version.rb +1 -1
- data/lib/tennis.rb +11 -19
- data/spec/spec_helper.rb +4 -0
- data/spec/tennis_spec.rb +21 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92920cd754c3ac7a92528a35692060e8ed472dbe
|
4
|
+
data.tar.gz: 8e91f9f5b0a4c2f4902b4eba2b1bc53204cb50bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eaffb4274e0b704fe739ef00127b8084ae5b8f7dacdd31f3f38eae002b42b64c6a9036d0c1039ed47f7da715e36fe9157a70c4502f8137af33a785c21dc02a1
|
7
|
+
data.tar.gz: 0ecc8a77bf85c347177ade9930aa863882c4f11a14dadee09b013ad60663da176d841c5c0c529c3c6a7c3959d27d3a46cabb35cfb24304cbc3ba074f5ae4b283
|
data/Gemfile
CHANGED
data/Makefile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Tennis
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/tennis.svg)](http://badge.fury.io/rb/tennis) [![Build Status](https://travis-ci.org/racketlogger/tennis.svg)](https://travis-ci.org/racketlogger/tennis)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/tennis.svg)](http://badge.fury.io/rb/tennis) [![Build Status](https://travis-ci.org/racketlogger/tennis.svg)](https://travis-ci.org/racketlogger/tennis) [![Code Climate](https://codeclimate.com/github/racketlogger/tennis/badges/gpa.svg)](https://codeclimate.com/github/racketlogger/tennis) [![Test Coverage](https://codeclimate.com/github/racketlogger/tennis/badges/coverage.svg)](https://codeclimate.com/github/racketlogger/tennis)
|
4
4
|
|
5
5
|
Ruby gem with utilities to manage, print and validate tennis scores
|
6
6
|
|
data/lib/tennis/version.rb
CHANGED
data/lib/tennis.rb
CHANGED
@@ -6,7 +6,7 @@ class Tennis
|
|
6
6
|
@result = (1 if scores == 'default-1') || (2 if scores == 'default-2') || :default
|
7
7
|
if @result == :default
|
8
8
|
# check blank input ''
|
9
|
-
@result = :error if @scores.any? { |score| score.nil?
|
9
|
+
@result = :error if @scores.any? { |score| score.nil? }
|
10
10
|
# to check if score for only 1 set has been input
|
11
11
|
validation_1 = @scores.length == 2
|
12
12
|
# to check if any input > 7
|
@@ -24,21 +24,13 @@ class Tennis
|
|
24
24
|
# to_s
|
25
25
|
# return the score in string format
|
26
26
|
def to_s
|
27
|
-
|
28
|
-
(0...@scores.length).step(2).each do |i|
|
29
|
-
score << [@scores[i], @scores[i+1]].join('-')
|
30
|
-
end
|
31
|
-
score.join(', ')
|
27
|
+
(0...@scores.length).step(2).map{ |i| [@scores[i], @scores[i+1]].join('-') }.join(', ')
|
32
28
|
end
|
33
29
|
|
34
30
|
# flip score ( P1-P2 to P2-P1)
|
35
|
-
# returns the flipped score
|
31
|
+
# returns the flipped score as a string
|
36
32
|
def flipped
|
37
|
-
|
38
|
-
(0...@scores.length).step(2).each do |i|
|
39
|
-
flipped_score << [@scores[i+1], @scores[i]].join('-')
|
40
|
-
end
|
41
|
-
flipped_score.join(', ')
|
33
|
+
(0...@scores.length).step(2).map{ |i| [@scores[i+1], @scores[i]].join('-') }.join(', ')
|
42
34
|
end
|
43
35
|
|
44
36
|
# returns who won the match
|
@@ -48,7 +40,7 @@ class Tennis
|
|
48
40
|
# 2 (player-2 won)
|
49
41
|
def winner
|
50
42
|
return @result if @result != :default
|
51
|
-
|
43
|
+
@result = (@scores.length == 4) ? two_sets : three_sets
|
52
44
|
end
|
53
45
|
|
54
46
|
# returns an array of points
|
@@ -57,7 +49,7 @@ class Tennis
|
|
57
49
|
def points
|
58
50
|
@result = winner
|
59
51
|
(return [0, 0]) if @result == :error
|
60
|
-
|
52
|
+
(complete_match_points if @result == 1 || @result == 2) || incomplete_match_points
|
61
53
|
end
|
62
54
|
|
63
55
|
private
|
@@ -76,7 +68,7 @@ class Tennis
|
|
76
68
|
end
|
77
69
|
end
|
78
70
|
# incomplete match e.g: 6-4,5-3
|
79
|
-
|
71
|
+
(set_results[0] if set_results[0] == set_results[1]) || :incomplete_match
|
80
72
|
end
|
81
73
|
|
82
74
|
# helper method: called by RESULT method for valid matches with 3 sets
|
@@ -95,7 +87,7 @@ class Tennis
|
|
95
87
|
# checks if the result has been decided in the first 2 sets
|
96
88
|
# but the 3rd set is also present in the input
|
97
89
|
return :error if set_results[0] == set_results[1]
|
98
|
-
|
90
|
+
set_results.count(1) == 2 ? 1 : 2
|
99
91
|
end
|
100
92
|
|
101
93
|
# helper method: called by POINTS for complete matches
|
@@ -106,7 +98,7 @@ class Tennis
|
|
106
98
|
runner_up = (@result == 1) ? 2 : 1
|
107
99
|
runner_up_points = player_points(runner_up)
|
108
100
|
points[runner_up - 1] = runner_up_points < 8 ? runner_up_points : 8
|
109
|
-
|
101
|
+
points
|
110
102
|
end
|
111
103
|
|
112
104
|
# helper method: called by POINTS for incomplete matches
|
@@ -116,7 +108,7 @@ class Tennis
|
|
116
108
|
player_2_points = player_points(2)
|
117
109
|
points[0] = player_1_points < 10 ? player_1_points : 10
|
118
110
|
points[1] = player_2_points < 10 ? player_2_points : 10
|
119
|
-
|
111
|
+
points
|
120
112
|
end
|
121
113
|
|
122
114
|
# helper method: returns the POINTS of a player given the player number
|
@@ -124,6 +116,6 @@ class Tennis
|
|
124
116
|
player_scores = []
|
125
117
|
@scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player - 1) }
|
126
118
|
player_scores = player_scores.sort! { |x, y| y <=> x }
|
127
|
-
|
119
|
+
player_scores[0] + player_scores[1]
|
128
120
|
end
|
129
121
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/tennis_spec.rb
CHANGED
@@ -101,9 +101,28 @@ describe Tennis, "#points" do
|
|
101
101
|
end
|
102
102
|
|
103
103
|
it "checks invalid score: blank score '' " do
|
104
|
-
pending
|
105
104
|
score = Tennis.new("")
|
106
|
-
expect(score.points).to eq
|
105
|
+
expect(score.points).to eq [0,0]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe Tennis, "#as_string" do
|
110
|
+
it "normalizes scores to a string properly formatted" do
|
111
|
+
scores = [["6-4,4-6, 6-4", "6-4, 4-6, 6-4"], [" 4-6, 4-6 ", "4-6, 4-6"],
|
112
|
+
[" 4 -6, 4- 6 ", "4-6, 4-6"], [" 0 - 6, 6 - 0, 3 - 6 ", "0-6, 6-0, 3-6"]]
|
113
|
+
scores.each do |s|
|
114
|
+
ts = Tennis.new(s[0])
|
115
|
+
expect(ts.to_s).to eq s[1]
|
116
|
+
end
|
107
117
|
end
|
118
|
+
end
|
108
119
|
|
120
|
+
describe Tennis, "#flipped" do
|
121
|
+
it "returns a normalized scores, but flipped" do
|
122
|
+
scores = [["6-4, 4-6, 6-4", "4-6, 6-4, 4-6"], ["6-4, 7-6", "4-6, 6-7"]]
|
123
|
+
scores.each do |s|
|
124
|
+
ts = Tennis.new(s[0])
|
125
|
+
expect(ts.flipped).to eq s[1]
|
126
|
+
end
|
127
|
+
end
|
109
128
|
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.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rohan Katyal
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-02-
|
12
|
+
date: 2015-02-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|