tennis 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/lib/tennis/version.rb +1 -1
- data/lib/tennis.rb +82 -39
- data/spec/tennis_spec.rb +24 -4
- 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: ab97c6b1bdde123d2b06a05e91db465b8d55dd35
|
4
|
+
data.tar.gz: 4fae93c7c709ee1dcf9a38bae35585a93caff9a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98b611d18f1758c632f6686d092d4f42d9845875cfa25c731b47339bc7c592141f45101ffcce3cd946251a01031eef7ed4a11335c18766c90a45b8424259db97
|
7
|
+
data.tar.gz: fb611bd4896d45f25290f857f55afeea4f3b85c284c80df1363d0aba782822865027e28ad6c9172521381b2fd53557cf66b8fa4f66f0aa3f670b0fdb25da8832
|
data/.gitignore
CHANGED
data/lib/tennis/version.rb
CHANGED
data/lib/tennis.rb
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
require "tennis/version"
|
2
2
|
|
3
3
|
class Tennis
|
4
|
+
# player 1 is the home player(0) , player 2 is the away player(1)
|
5
|
+
# winner is either 0 or 1
|
6
|
+
# points is an array: [points for 0 , points for 1]
|
7
|
+
# set_count is an array: [sets won by 0, sets won by 1]
|
8
|
+
# game_count is an array: [games won by 0, games won by 1]
|
9
|
+
|
10
|
+
attr_reader :winner, :points, :set_count, :game_count
|
11
|
+
|
4
12
|
def initialize(scores)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# to check if one of the input is 7 and the other is not 6
|
15
|
-
# bad tie break input
|
16
|
-
validation_3 = false
|
17
|
-
@scores.each_slice(2).each {|r| validation_3 = true if r.any? {|score| score == 7} && !r.any? {|score| score == 6} }
|
18
|
-
@result = :error if validation_1 || validation_2 || validation_3
|
19
|
-
# if set score is not complete eg: 4-6,7-6,4-1
|
20
|
-
@scores.each_slice(2).each {|r| @result = :incomplete_match if r[0] < 6 && r[1] < 6 } if @result != :error
|
13
|
+
# dfh -> default win for home player(0)
|
14
|
+
# dfa -> default win for away player(1)
|
15
|
+
@winner = :default
|
16
|
+
@scores = validate_score(scores)
|
17
|
+
@winner = match_winner if @winner == :default
|
18
|
+
@points = match_points
|
19
|
+
unless @scores.is_a? String
|
20
|
+
@set_count = sets_won
|
21
|
+
@game_count = games_won
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
@@ -33,38 +34,81 @@ class Tennis
|
|
33
34
|
(0...@scores.length).step(2).map{ |i| [@scores[i+1], @scores[i]].join('-') }.join(', ')
|
34
35
|
end
|
35
36
|
|
37
|
+
private
|
38
|
+
|
39
|
+
# helper method: to check score validation
|
40
|
+
def validate_score(scores_string)
|
41
|
+
set_scores = scores_string.split(/[-,,]/).map(&:to_i)
|
42
|
+
if set_scores == [0]
|
43
|
+
# checks bad default string value reported
|
44
|
+
@winner = (0 if scores == 'dfh') || (1 if scores == 'dfa') || :error
|
45
|
+
scores_string
|
46
|
+
else
|
47
|
+
# check blank input ''
|
48
|
+
validation_1 = set_scores.any? {|score| score.nil?}
|
49
|
+
# to check if score for only 1 set has been input
|
50
|
+
validation_2 = set_scores.length == 2
|
51
|
+
# to check if any input > 7
|
52
|
+
validation_3 = set_scores.any? {|score| score > 7}
|
53
|
+
# to check if one of the input is 7 and the other is not 6
|
54
|
+
# bad tie break input
|
55
|
+
validation_4 = false
|
56
|
+
set_scores.each_slice(2).each {|r| validation_4 = true if r.any? {|score| score == 7} && !r.any? {|score| score == 6} }
|
57
|
+
@winner = :error if validation_1 || validation_2 || validation_3 || validation_4
|
58
|
+
# if set score is not complete eg: 4-6,7-6,4-1
|
59
|
+
set_scores.each_slice(2).each {|r| @winner = :incomplete_match if r[0] < 6 && r[1] < 6 } unless @winner == :error
|
60
|
+
set_scores
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
36
64
|
# returns who won the match
|
37
65
|
# :incomplete_match (bad input/incomplete match)
|
38
66
|
# :error (bad input for sure)
|
39
|
-
#
|
40
|
-
#
|
41
|
-
def
|
42
|
-
|
43
|
-
@result = (@scores.length == 4) ? two_sets : three_sets
|
67
|
+
# 0 (player-1 won)
|
68
|
+
# 1 (player-2 won)
|
69
|
+
def match_winner
|
70
|
+
@scores.length == 4 ? two_sets : three_sets
|
44
71
|
end
|
45
72
|
|
46
73
|
# returns an array of points
|
47
74
|
# returns (points_player_1 , points_player_2)
|
48
75
|
# returns (0,0) for bad input
|
49
|
-
def
|
50
|
-
@
|
51
|
-
|
52
|
-
|
76
|
+
def match_points
|
77
|
+
return [0, 0] if @winner == :error
|
78
|
+
return [@scores == 'dfh' ? 12 : 0 , @scores == 'dfa' ? 12 : 0] if @scores.is_a? String
|
79
|
+
@winner == 0 || @winner == 1 ? complete_match_points : incomplete_match_points
|
53
80
|
end
|
54
81
|
|
55
|
-
|
82
|
+
# returns the number of sets won by the given player (0 or 1)
|
83
|
+
def sets_won
|
84
|
+
sets = [0, 0]
|
85
|
+
(0...@scores.length).step(2).each do |i|
|
86
|
+
@scores[0 + i] > @scores[1 + i] ? sets[0] += 1 : sets[1] += 1
|
87
|
+
end
|
88
|
+
sets
|
89
|
+
end
|
90
|
+
|
91
|
+
# returns the number of won by the given player (0 or 1)
|
92
|
+
def games_won
|
93
|
+
games = [0, 0]
|
94
|
+
(0...@scores.length).step(2).each do |i|
|
95
|
+
games[0] += @scores[0 + i]
|
96
|
+
games[1] += @scores[1 + i]
|
97
|
+
end
|
98
|
+
games
|
99
|
+
end
|
56
100
|
|
57
101
|
# helper method: called by RESULT method for valid matches with 2 sets
|
58
102
|
def two_sets
|
59
103
|
set_results = []
|
60
104
|
(0...@scores.length).step(2).each do |i|
|
61
105
|
# tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
|
62
|
-
if @scores[i] == 7 || @scores[i+1] == 7
|
63
|
-
set_results << (@scores[i] == 7 ?
|
106
|
+
if @scores[i] == 7 || @scores[i + 1] == 7
|
107
|
+
set_results << (@scores[i] == 7 ? 0 : 1)
|
64
108
|
# regular set victory - 6 games with a margin of 2
|
65
109
|
else
|
66
110
|
return :incomplete_match if ( @scores[i] - @scores[i + 1] ).abs < 2
|
67
|
-
set_results << (@scores[i] == 6 ?
|
111
|
+
set_results << (@scores[i] == 6 ? 0 : 1)
|
68
112
|
end
|
69
113
|
end
|
70
114
|
# incomplete match e.g: 6-4,5-3
|
@@ -77,35 +121,34 @@ class Tennis
|
|
77
121
|
(0...@scores.length).step(2).each do |i|
|
78
122
|
# tie breaker (assuming a 7 point tie breaker) or a 7-5 score
|
79
123
|
if @scores[i] == 7 || @scores[i + 1] == 7
|
80
|
-
set_results << (@scores[i] == 7 ?
|
124
|
+
set_results << (@scores[i] == 7 ? 0 : 1)
|
81
125
|
# regular set victory - 6 games with a margin of 2
|
82
126
|
else
|
83
127
|
return :incomplete_match if (@scores[i] - @scores[i + 1]).abs < 2
|
84
|
-
set_results << (@scores[i] == 6 ?
|
128
|
+
set_results << (@scores[i] == 6 ? 0 : 1)
|
85
129
|
end
|
86
130
|
end
|
87
131
|
# checks if the result has been decided in the first 2 sets
|
88
132
|
# but the 3rd set is also present in the input
|
89
133
|
return :error if set_results[0] == set_results[1]
|
90
|
-
set_results.count(
|
134
|
+
set_results.count(0) == 2 ? 0 : 1
|
91
135
|
end
|
92
136
|
|
93
137
|
# helper method: called by POINTS for complete matches
|
94
138
|
def complete_match_points
|
95
139
|
points = [0, 0]
|
96
|
-
@
|
97
|
-
|
98
|
-
runner_up = (@result == 1) ? 2 : 1
|
140
|
+
points[@winner] = (@scores.length == 6) ? 12 : 14
|
141
|
+
runner_up = 1 - @winner
|
99
142
|
runner_up_points = player_points(runner_up)
|
100
|
-
points[runner_up
|
143
|
+
points[runner_up] = runner_up_points < 8 ? runner_up_points : 8
|
101
144
|
points
|
102
145
|
end
|
103
146
|
|
104
147
|
# helper method: called by POINTS for incomplete matches
|
105
148
|
def incomplete_match_points
|
106
149
|
points = [0, 0]
|
107
|
-
player_1_points = player_points(
|
108
|
-
player_2_points = player_points(
|
150
|
+
player_1_points = player_points(0)
|
151
|
+
player_2_points = player_points(1)
|
109
152
|
points[0] = player_1_points < 10 ? player_1_points : 10
|
110
153
|
points[1] = player_2_points < 10 ? player_2_points : 10
|
111
154
|
points
|
@@ -114,7 +157,7 @@ class Tennis
|
|
114
157
|
# helper method: returns the POINTS of a player given the player number
|
115
158
|
def player_points(player)
|
116
159
|
player_scores = []
|
117
|
-
@scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player
|
160
|
+
@scores.each_with_index { |score, index| (player_scores << score; player += 2) if index == (player) }
|
118
161
|
player_scores = player_scores.sort! { |x, y| y <=> x }
|
119
162
|
player_scores[0] + player_scores[1]
|
120
163
|
end
|
data/spec/tennis_spec.rb
CHANGED
@@ -9,22 +9,22 @@ end
|
|
9
9
|
describe Tennis, "#scores" do
|
10
10
|
it "finds the winner properly in two sets" do
|
11
11
|
score = Tennis.new("6-4, 6-4")
|
12
|
-
expect(score.winner).to eq
|
12
|
+
expect(score.winner).to eq 0
|
13
13
|
end
|
14
14
|
|
15
15
|
it "finds the winner properly in three sets" do
|
16
16
|
score = Tennis.new("4-6, 6-2, 3-6")
|
17
|
-
expect(score.winner).to eq
|
17
|
+
expect(score.winner).to eq 1
|
18
18
|
end
|
19
19
|
|
20
20
|
it "finds the winner properly in two sets with tie break" do
|
21
21
|
score = Tennis.new("6-4, 7-6")
|
22
|
-
expect(score.winner).to eq
|
22
|
+
expect(score.winner).to eq 0
|
23
23
|
end
|
24
24
|
|
25
25
|
it "finds the winner properly in three sets with tie break" do
|
26
26
|
score = Tennis.new("6-4, 6-7, 7-6")
|
27
|
-
expect(score.winner).to eq
|
27
|
+
expect(score.winner).to eq 0
|
28
28
|
end
|
29
29
|
|
30
30
|
it "reports incomplete match score (set 1-1)" do
|
@@ -126,3 +126,23 @@ describe Tennis, "#flipped" do
|
|
126
126
|
end
|
127
127
|
end
|
128
128
|
end
|
129
|
+
|
130
|
+
describe Tennis, "#set_count" do
|
131
|
+
it "returns the sets won by each player" do
|
132
|
+
scores = [["6-4, 4-6, 6-4", [2,1]],["6-2,6-1", [2,0]], ["7-6,4-6,6-4", [2,1]], ["6-4", [1,0]]]
|
133
|
+
scores.each do |s|
|
134
|
+
ts = Tennis.new(s[0])
|
135
|
+
expect(ts.set_count).to eq s[1]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe Tennis, "#game_count" do
|
141
|
+
it "returns the games won by each player" do
|
142
|
+
scores = [["6-4, 4-6, 6-4", [16,14]],["6-2,6-1", [12,3]], ["7-6,4-6,6-4", [17,16]], ["6-4", [6,4]]]
|
143
|
+
scores.each do |s|
|
144
|
+
ts = Tennis.new(s[0])
|
145
|
+
expect(ts.game_count).to eq s[1]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
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.5
|
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-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|