Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View tennis_game_kata.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
# 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.