Advertisement
Guest User

initial_code

a guest
Jan 22nd, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. class TennisGame1
  2.  
  3. def initialize(player1Name, player2Name)
  4. @player1Name = player1Name
  5. @player2Name = player2Name
  6. @p1points = 0
  7. @p2points = 0
  8. end
  9.  
  10. def won_point(playerName)
  11. if playerName == "player1"
  12. @p1points += 1
  13. else
  14. @p2points += 1
  15. end
  16. end
  17.  
  18. def score
  19. result = ""
  20. tempScore=0
  21. if (@p1points==@p2points)
  22. result = {
  23. 0 => "Love-All",
  24. 1 => "Fifteen-All",
  25. 2 => "Thirty-All",
  26. }.fetch(@p1points, "Deuce")
  27. elsif (@p1points>=4 or @p2points>=4)
  28. minusResult = @p1points-@p2points
  29. if (minusResult==1)
  30. result ="Advantage player1"
  31. elsif (minusResult ==-1)
  32. result ="Advantage player2"
  33. elsif (minusResult>=2)
  34. result = "Win for player1"
  35. else
  36. result ="Win for player2"
  37. end
  38. else
  39. (1...3).each do |i|
  40. if (i==1)
  41. tempScore = @p1points
  42. else
  43. result+="-"
  44. tempScore = @p2points
  45. end
  46. result += {
  47. 0 => "Love",
  48. 1 => "Fifteen",
  49. 2 => "Thirty",
  50. 3 => "Forty",
  51. }[tempScore]
  52. end
  53. end
  54. result
  55. end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement