Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TennisGame1
- def initialize(player1Name, player2Name)
- @player1Name = player1Name
- @player2Name = player2Name
- @p1points = 0
- @p2points = 0
- end
- def won_point(playerName)
- if playerName == "player1"
- @p1points += 1
- else
- @p2points += 1
- end
- end
- def score
- result = ""
- tempScore=0
- if (@p1points==@p2points)
- result = {
- 0 => "Love-All",
- 1 => "Fifteen-All",
- 2 => "Thirty-All",
- }.fetch(@p1points, "Deuce")
- elsif (@p1points>=4 or @p2points>=4)
- minusResult = @p1points-@p2points
- if (minusResult==1)
- result ="Advantage player1"
- elsif (minusResult ==-1)
- result ="Advantage player2"
- elsif (minusResult>=2)
- result = "Win for player1"
- else
- result ="Win for player2"
- end
- else
- (1...3).each do |i|
- if (i==1)
- tempScore = @p1points
- else
- result+="-"
- tempScore = @p2points
- end
- result += {
- 0 => "Love",
- 1 => "Fifteen",
- 2 => "Thirty",
- 3 => "Forty",
- }[tempScore]
- end
- end
- result
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement