tennis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f75cc2750ec34ca68ed936d8f66aac0da45e1b10
4
+ data.tar.gz: 4d3cb1e0fcbc5fc0b98a4a733345891fa003f15f
5
+ SHA512:
6
+ metadata.gz: 5330df6992790cd772a33510a090892c59a8593a496356a80844a2318d5181b4e95701a9ca70ad540e53ce2283386ceddcf8c9de2b35128337a4f30c7de0721b
7
+ data.tar.gz: 92aeab0c4938a7c19512696a1ddef67821d824d94752a603a1915da5537f89b63f6e393129544d7a9d6d72f89f6518ae9010e70afc92af3e8883ec62da6b8b7d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tennis.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
166
+
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Tennis
2
+
3
+ Ruby gem with utilities to manage, print and validate tennis scores
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tennis'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tennis
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/racketlogger/tennis/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module Tennis
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tennis.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "tennis/version"
2
+
3
+ module Tennis
4
+ class << self
5
+ def initialize(scores)
6
+ @scores = scores != 'default-1' && scores != 'default-2' ? scores.split(/[-,,]/).map(&:to_i) : scores
7
+ @result = (1 if scores == 'default-1') || (2 if scores == 'default-2') || :default
8
+ if @result == :default
9
+ # to check if score for only 1 set has been input
10
+ validation_1 = @scores.length == 2
11
+ # to check if any input > 7
12
+ validation_2 = @scores.any? { |score| score > 7 }
13
+ @result = :error if validation_1 || validation_2
14
+ end
15
+ end
16
+
17
+ # returns who won the match
18
+ # :incomplete_match (bad input/incomplete match)
19
+ # :error (bad input for sure)
20
+ # 1 (player-1 won)
21
+ # 2 (player-2 won)
22
+ def result
23
+ return @result if @result != :default
24
+ return @result = (@scores.length == 4) ? two_sets : three_sets
25
+ end
26
+
27
+ # returns an array of points
28
+ # returns (points_player_1 , points_player_2)
29
+ # returns (0,0) for bad input
30
+ def points
31
+ @result = self.result
32
+ (return [0, 0]) if @result == :error
33
+ return (complete_match_points if @result == 1 || @result == 2) || incomplete_match_points
34
+ end
35
+
36
+ private
37
+
38
+ # helper method: called by RESULT method for valid matches with 2 sets
39
+ def two_sets
40
+ set_results = []
41
+ [0, 2].each do |i|
42
+ # tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
43
+ if @scores[i] == 7 || @scores[i+1] == 7
44
+ set_results << (@scores[i] == 7 ? 1 : 2)
45
+ # regular set victory - 6 games with a margin of 2
46
+ else
47
+ return :incomplete_match if ( @scores[i] - @scores[i + 1] ).abs < 2
48
+ set_results << (@scores[i] == 6 ? 1 : 2)
49
+ end
50
+ end
51
+ # incomplete match e.g: 6-4,5-3
52
+ return (set_results[0] if set_results[0] == set_results[1]) || :incomplete_match
53
+ end
54
+
55
+ # helper method: called by RESULT method for valid matches with 3 sets
56
+ def three_sets
57
+ set_results = []
58
+ [0, 2, 4].each do |i|
59
+ # tie breaker (assuming a 7 point tie breaker) or a 7-5 score
60
+ if @scores[i] == 7 || @scores[i + 1] == 7
61
+ set_results << (@scores[i] == 7 ? 1 : 2)
62
+ # regular set victory - 6 games with a margin of 2
63
+ else
64
+ return :incomplete_match if (@scores[i] - @scores[i + 1]).abs < 2
65
+ set_results << (@scores[i] == 6 ? 1 : 2)
66
+ end
67
+ end
68
+ # checks if the result has been decided in the first 2 sets
69
+ # but the 3rd set is also present in the input
70
+ return :error if set_results[0] == set_results[1]
71
+ return set_results.count(1) ? 1 : 2
72
+ end
73
+
74
+ # helper method: called by POINTS for complete matches
75
+ def complete_match_points
76
+ points = [0, 0]
77
+ @result = self.result
78
+ points[@result - 1] = (@scores.length == 6) ? 12 : 14
79
+ runner_up = (@result == 1) ? 2 : 1
80
+ runner_up_points = player_points(runner_up)
81
+ points[runner_up - 1] = runner_up_points < 8 ? runner_up_points : 8
82
+ return points
83
+ end
84
+
85
+ # helper method: called by POINTS for incomplete matches
86
+ def incomplete_match_points
87
+ points = [0, 0]
88
+ player_1_points = player_points(1)
89
+ player_2_points = player_points(2)
90
+ points[0] = player_1_points < 10 ? player_1_points : 10
91
+ points[1] = player_2_points < 10 ? player_2_points : 10
92
+ return points
93
+ end
94
+
95
+ # helper method: returns the POINTS of a player given the player number
96
+ def player_points(player)
97
+ player_scores = []
98
+ @scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player - 1) }
99
+ player_scores = player_scores.sort! { |x, y| y <=> x }
100
+ return player_scores[0] + player_scores[1]
101
+ end
102
+ end
103
+ end
data/tennis.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tennis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tennis"
8
+ spec.version = Tennis::VERSION
9
+ spec.authors = ["Carlos Puchol"]
10
+ spec.email = ["cpg+git@amahi.org"]
11
+ spec.summary = %q{A gem to manage tennis scores.}
12
+ spec.description = %q{A gem to manage and validate tennis scores.}
13
+ spec.homepage = ""
14
+ spec.license = "GPL"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tennis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Puchol
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A gem to manage and validate tennis scores.
42
+ email:
43
+ - cpg+git@amahi.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/tennis.rb
54
+ - lib/tennis/version.rb
55
+ - tennis.gemspec
56
+ homepage: ''
57
+ licenses:
58
+ - GPL
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.5
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: A gem to manage tennis scores.
80
+ test_files: []