Chiling at my dojo w/Anika
This file contains 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 | |
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