Created
March 17, 2017 16:13
-
-
Save thimo/d4df63e3da3bfa491b76748e7fe0b111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TennisGame1 | |
POINTS_NAMES = { | |
0 => 'Love', | |
1 => 'Fifteen', | |
2 => 'Thirty', | |
3 => 'Forty' | |
}.freeze | |
DEUCE = 'Deuce'.freeze | |
ADVANTAGE = 'Advantage'.freeze | |
WIN_FOR = 'Win for'.freeze | |
ALL = 'All'.freeze | |
def initialize(player1_name, player2_name) | |
@players = [ | |
{ name: player1_name, points: 0 }, | |
{ name: player2_name, points: 0 } | |
] | |
end | |
def won_point(player_name) | |
(0..1).each do |i| | |
@players[i][:points] += 1 if player_name == @players[i][:name] | |
end | |
end | |
def score | |
if @players[0][:points] == @players[1][:points] | |
return result_for_equal_score | |
elsif (@players[0][:points] >= 4) || (@players[1][:points] >= 4) | |
difference_in_points = @players[0][:points] - @players[1][:points] | |
return (difference_in_points.abs == 1) ? result_for_advantage : result_for_win | |
else | |
return "#{POINTS_NAMES[@players[0][:points]]}-#{POINTS_NAMES[@players[1][:points]]}" | |
end | |
end | |
def result_for_equal_score | |
return DEUCE if @players[0][:points] > 2 | |
"#{POINTS_NAMES[@players[0][:points]]}-#{ALL}" | |
end | |
def result_for_win | |
WIN_FOR + ' ' + (@players[0][:points] > @players[1][:points] ? @players[0][:name] : @players[1][:name]) | |
end | |
def result_for_advantage | |
ADVANTAGE + ' ' + (@players[0][:points] > @players[1][:points] ? @players[0][:name] : @players[1][:name]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment