Skip to content

@omerisimo /tennis_game_kata.rb
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# Kata description: http://codingdojo.org/cgi-bin/index.pl?KataTennis
module Tennis
class Player
attr_reader :name, :points_won
def initialize(name)
@name = name
@points_won = 0
end
def win_point
@points_won += 1
end
end
class Game
SCORE_NAMES = %w(Love Fifteen Thirty Forty)
def initialize(player_1, player_2)
@player_1 = player_1
@player_2 = player_2
end
def score
if @player_1.points_won >= 3 && @player_1.points_won == @player_2.points_won
return "Deuce"
end
if @player_1.points_won >= 4 || @player_2.points_won >= 4
if (@player_1.points_won - @player_2.points_won).abs >=2
return "#{leader.name} won the game!"
else
return "Advantage #{leader.name}"
end
end
"#{SCORE_NAMES[@player_1.points_won]}, #{SCORE_NAMES[@player_2.points_won]}"
end
private
def leader
@player_1.points_won > @player_2.points_won ? @player_1 : @player_2
end
end
end
describe Tennis::Player do
subject { Tennis::Player.new("Pete Sampras") }
its (:name) { should eq "Pete Sampras" }
its (:points_won) { should eq 0 }
describe "#win_point" do
before do
subject.win_point
end
its (:points_won) { should eq 1 }
context "after winning two more points" do
before do
2.times { subject.win_point }
end
its (:points_won) { should eq 3 }
end
end
end
describe Tennis::Game do
subject { Tennis::Game.new(@player_1, @player_2) }
before do
@player_1 = Tennis::Player.new("Pete Sampras")
@player_2 = Tennis::Player.new("Andre Agassi")
end
its (:score) { should eq "Love, Love" }
context "when player 1 wins one point" do
before do
@player_1.win_point
end
its (:score) { should eq "Fifteen, Love" }
end
context "when player 2 wins three point" do
before do
3.times { @player_2.win_point }
end
its (:score) { should eq "Love, Forty" }
end
context "when player 1 wins 4 straight point" do
before do
4.times { @player_1.win_point }
end
its (:score) { should eq "Pete Sampras won the game!" }
end
context "when player 2 wins 4 straight point" do
before do
4.times { @player_2.win_point }
end
its (:score) { should eq "Andre Agassi won the game!" }
end
context "when each player wins three points" do
before do
3.times { @player_1.win_point }
3.times { @player_2.win_point }
end
its (:score) { should eq "Deuce" }
end
context "when player 1 wins 3 points and player 2 wins 4 points" do
before do
3.times { @player_1.win_point }
4.times { @player_2.win_point }
end
its (:score) { should eq "Advantage Andre Agassi" }
context "after player 2 wins one more point" do
before do
@player_2.win_point
end
its (:score) { should eq "Andre Agassi won the game!" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.