First attempt at a ruby version of this kata

This commit is contained in:
emilybache 2012-12-07 19:10:05 +01:00
parent 7333fc844e
commit 1b30d88dbd
2 changed files with 296 additions and 0 deletions

223
Tennis/ruby/tennis.rb Normal file
View File

@ -0,0 +1,223 @@
class TennisGameDefactored1
def initialize(player1Name, player2Name)
@player1Name = player1Name
@player2Name = player2Name
@p1points = 0
@p2points = 0
end
def won_point(playerName)
if playerName == @player1Name
@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",
3 => "Forty-All",
}.fetch(@p1points, "Deuce")
elsif (@p1points>=4 or @p2points>=4)
minusResult = @p1points-@p2points
if (minusResult==1)
result ="Advantage " + @player1Name
elsif (minusResult ==-1)
result ="Advantage " + @player2Name
elsif (minusResult>=2)
result = "Win for " + @player1Name
else
result ="Win for " + @player2Name
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
class TennisGameDefactored2
def initialize(player1Name, player2Name)
@player1Name = player1Name
@player2Name = player2Name
@p1points = 0
@p2points = 0
end
def won_point(playerName)
if playerName == @player1Name
p1Score()
else
p2Score()
end
end
def score
result = ""
if (@p1points == @p2points and @p1points < 4)
if (@p1points==0)
result = "Love"
end
if (@p1points==1)
result = "Fifteen"
end
if (@p1points==2)
result = "Thirty"
end
if (@p1points==3)
result = "Forty"
end
result += "-All"
end
if (@p1points==@p2points and @p1points>3)
result = "Deuce"
end
p1res = ""
p2res = ""
if (@p1points > 0 and @p2points==0)
if (@p1points==1)
p1res = "Fifteen"
end
if (@p1points==2)
p1res = "Thirty"
end
if (@p1points==3)
p1res = "Forty"
end
p2res = "Love"
result = p1res + "-" + p2res
end
if (@p2points > 0 and @p1points==0)
if (@p2points==1)
p2res = "Fifteen"
end
if (@p2points==2)
p2res = "Thirty"
end
if (@p2points==3)
p2res = "Forty"
end
p1res = "Love"
result = p1res + "-" + p2res
end
if (@p1points>@p2points and @p1points < 4)
if (@p1points==2)
p1res="Thirty"
end
if (@p1points==3)
p1res="Forty"
end
if (@p2points==1)
p2res="Fifteen"
end
if (@p2points==2)
p2res="Thirty"
end
result = p1res + "-" + p2res
end
if (@p2points>@p1points and @p2points < 4)
if (@p2points==2)
p2res="Thirty"
end
if (@p2points==3)
p2res="Forty"
end
if (@p1points==1)
p1res="Fifteen"
end
if (@p1points==2)
p1res="Thirty"
end
result = p1res + "-" + p2res
end
if (@p1points > @p2points and @p2points >= 3)
result = "Advantage " + @player1Name
end
if (@p2points > @p1points and @p1points >= 3)
result = "Advantage " + @player2Name
end
if (@p1points>=4 and @p2points>=0 and (@p1points-@p2points)>=2)
result = "Win for " + @player1Name
end
if (@p2points>=4 and @p1points>=0 and (@p2points-@p1points)>=2)
result = "Win for " + @player2Name
end
result
end
def setp1Score(number)
(0..number).each do |i|
p1Score()
end
end
def setp2Score(number)
(0..number).each do |i|
p2Score()
end
end
def p1Score
@p1points +=1
end
def p2Score
@p2points +=1
end
end
class TennisGameDefactored3
def initialize(player1Name, player2Name)
@p1N = player1Name
@p2N = player2Name
@p1 = 0
@p2 = 0
end
def won_point(n)
if n == @p1N
@p1 += 1
else
@p2 += 1
end
end
def score
if (@p1 < 4 and @p2 < 4)
p = ["Love", "Fifteen", "Thirty", "Forty"]
s = p[@p1]
@p1 == @p2 ? s + "-All" : s + "-" + p[@p2]
else
if (@p1 == @p2)
"Deuce"
else
s = @p1 > @p2 ? @p1N : @p2N
(@p1-@p2)*(@p1-@p2) == 1 ? "Advantage " + s : "Win for " + s
end
end
end
end

View File

@ -0,0 +1,73 @@
require_relative("tennis")
require 'test/unit'
TEST_CASES = [
[0, 0, "Love-All", 'player1', 'player2'],
[1, 1, "Fifteen-All", 'player1', 'player2'],
[2, 2, "Thirty-All", 'player1', 'player2'],
[3, 3, "Forty-All", 'player1', 'player2'],
[4, 4, "Deuce", 'player1', 'player2'],
[1, 0, "Fifteen-Love", 'player1', 'player2'],
[0, 1, "Love-Fifteen", 'player1', 'player2'],
[2, 0, "Thirty-Love", 'player1', 'player2'],
[0, 2, "Love-Thirty", 'player1', 'player2'],
[3, 0, "Forty-Love", 'player1', 'player2'],
[0, 3, "Love-Forty", 'player1', 'player2'],
[4, 0, "Win for player1", 'player1', 'player2'],
[0, 4, "Win for player2", 'player1', 'player2'],
[2, 1, "Thirty-Fifteen", 'player1', 'player2'],
[1, 2, "Fifteen-Thirty", 'player1', 'player2'],
[3, 1, "Forty-Fifteen", 'player1', 'player2'],
[1, 3, "Fifteen-Forty", 'player1', 'player2'],
[4, 1, "Win for player1", 'player1', 'player2'],
[1, 4, "Win for player2", 'player1', 'player2'],
[3, 2, "Forty-Thirty", 'player1', 'player2'],
[2, 3, "Thirty-Forty", 'player1', 'player2'],
[4, 2, "Win for player1", 'player1', 'player2'],
[2, 4, "Win for player2", 'player1', 'player2'],
[4, 3, "Advantage player1", 'player1', 'player2'],
[3, 4, "Advantage player2", 'player1', 'player2'],
[5, 4, "Advantage player1", 'player1', 'player2'],
[4, 5, "Advantage player2", 'player1', 'player2'],
[15, 14, "Advantage player1", 'player1', 'player2'],
[14, 15, "Advantage player2", 'player1', 'player2'],
[6, 4, 'Win for player1', 'player1', 'player2'],
[4, 6, 'Win for player2', 'player1', 'player2'],
[16, 14, 'Win for player1', 'player1', 'player2'],
[14, 16, 'Win for player2', 'player1', 'player2'],
[6, 4, 'Win for One', 'One', 'player2'],
[4, 6, 'Win for Two', 'player1', 'Two'],
[6, 5, 'Advantage One', 'One', 'player2'],
[5, 6, 'Advantage Two', 'player1', 'Two']
]
class TestTennis < Test::Unit::TestCase
def play_game(p1Points, p2Points, p1Name, p2Name)
# NOTE: Change this to the class you want to work on!
game = TennisGameDefactored1.new(p1Name, p2Name)
(0..[p1Points, p2Points].max).each do |i|
if i < p1Points
game.won_point(p1Name)
end
if i < p2Points
game.won_point(p2Name)
end
end
game
end
def test_Score
TEST_CASES.each do |testcase|
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(p1Points, p2Points, p1Name, p2Name)
assert_equal(score, game.score())
end
end
end