Skip to content

Instantly share code, notes, and snippets.

@leefreemanxyz
Created March 17, 2017 16:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save leefreemanxyz/f073271936385ab4dd4724fa0734556f to your computer and use it in GitHub Desktop.
Chiling at my dojo w/Anika
class TennisGame1
SCORES = ["Love", "Fifteen", "Thirty", "Forty"]
def initialize(player1Name, player2Name)
@player1Name = player1Name
@player2Name = player2Name
@playerScores = [0,0]
@lastPointWinner = ""
end
def won_point(playerName)
if playerName == @player1Name
@playerScores[0] += 1
@lastPointWinner = @player1Name
else
@playerScores[1] += 1
@lastPointWinner = @player2Name
end
end
def is_equal?
@playerScores[0] == @playerScores[1]
end
def is_win_or_advantage?
@playerScores[0] >= 4 || @playerScores[1] >= 4
end
def scoreDifference
(@playerScores[0] - @playerScores[1]).abs
end
def score
if is_equal?
result = @playerScores[0] < 3 ? "#{SCORES[@playerScores[0]]}-All" : "Deuce"
elsif is_win_or_advantage?
result = scoreDifference >= 2 ? "Win for #{@lastPointWinner}" : "Advantage #{@lastPointWinner}"
else
result = "#{SCORES[@playerScores[0]]}-#{SCORES[@playerScores[1]]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment