Skip to content

Instantly share code, notes, and snippets.

@benneuman
Created February 18, 2014 04:33
Show Gist options
  • Save benneuman/9064679 to your computer and use it in GitHub Desktop.
Save benneuman/9064679 to your computer and use it in GitHub Desktop.
require 'rspec'
class Game
attr_reader :player1, :player2
def initialize
@player1 = Player.new('player 1')
@player2 = Player.new('player 2')
@score_view = ScoreView.new(self)
end
def score
@score_view.render
end
def play_point
winner = [@player1, @player2].sample
winner.add_point
winner
end
def play_game
until has_winner
puts score
puts "#{play_point.name} wins point"
end
puts winner.name
end
def get_advantage
[@player1, @player2].max_by(&:score)
end
def winner
get_advantage if has_winner
end
def has_winner
(@player1.score >= 4 && @player1.score - 2 >= @player2.score) ||
(@player2.score >= 4 && @player2.score - 2 >= @player1.score)
end
end
class ScoreView
POINTS = [0, 15, 30, 40]
def initialize(game)
@game = game
@player1 = game.player1
@player2 = game.player2
end
def render
!(deuce_play?) ? regular_score : deuce_score
end
def regular_score
"#{POINTS[@player1.score]} - #{POINTS[@player2.score]}"
end
def deuce_score
@player1.score == @player2.score ? "deuce" : "ad #{get_advantage.name}"
end
def deuce_play?
@player1.score >= 3 && @player2.score >= 3 &&
(@player1.score > 3 || @player2.score > 3)
end
def get_advantage
@game.get_advantage
end
end
class Point
attr_reader :winner
def initialize(player1, player2)
@player1 = player1
@player2 = player2
play
end
def play
@winner = [@player1, @player2].sample
@winner.add_point
end
end
class Player
attr_reader :points, :name
def initialize(name)
@name = name
@points = 0
end
def add_point
@points += 1
end
def score
@points
end
end
describe Game do
before(:each) do
@game = Game.new
end
context "before game starts" do
it "score should be 0 - 0" do
@game.score.should == "0 - 0"
end
it "winner should be nil" do
@game.winner.should be_nil
end
end
context "the score" do
it "should be 15-0 after player 1 wins first point" do
@game.player1.add_point
@game.score.should == "15 - 0"
end
it "should be up 30 - 0 after player 1 wins first two points" do
2.times { @game.player1.add_point }
@game.score.should == "30 - 0"
end
it "should be 40 - 0 after player 1 wins first three points" do
3.times { @game.player1.add_point }
@game.score.should == "40 - 0"
end
it "should be 0 - 15 after player 2 wins first point" do
@game.player2.add_point
@game.score.should == "0 - 15"
end
context 'if at least one player has more than 3 points' do
before(:each) do
3.times { @game.player1.add_point }
3.times { @game.player2.add_point }
end
it "should be deuce if players are tied" do
@game.player1.add_point
@game.player2.add_point
@game.score.should == "deuce"
end
it "should be 'ad player 1' if player 1 is ahead" do
@game.player1.add_point
@game.score.should == "ad player 1"
end
it "should be 'ad player 2' if player 2 is ahead" do
@game.player2.add_point
@game.score.should == "ad player 2"
end
end
end
context "winner" do
it "should be player 1 after winning first four points" do
4.times { @game.player1.add_point }
@game.winner.should == @game.player1
end
it "should be player 2 after winning first four points" do
4.times { @game.player2.add_point }
@game.winner.should == @game.player2
end
it "should not be declared after four points if opponent is less than two points behind" do
3.times {
@game.player1.add_point
@game.player2.add_point
}
@game.player1.add_point
@game.winner.should be_nil
end
it "should be declared after one player gets up by at least two" do
3.times {
@game.player1.add_point
@game.player2.add_point
}
@game.player2.add_point
3.times { @game.player1.add_point }
@game.winner.should == @game.player1
end
end
end
Game.new.play_game
# context "when a player has scored" do
# it "zero times, she has a score of 0" do
# @game.player1.score.should == 0
# end
# it "one time, she has a score of 15" do
# @game.player1.add_point
# @game.player1.score.should == 15
# end
# it "two times, she has a score of 30" do
# @game.player1.add_point
# @game.player1.add_point
# @game.player1.score.should == 30
# end
# it "three times, she has a score of 40" do
# 3.times { @game.player1.add_point }
# @game.player1.score.should == 40
# end
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment