Created
February 18, 2014 03:46
-
-
Save benneuman/9064342 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
class Game | |
attr_reader :player1, :player2 | |
POINTS = [0, 15, 30, 40] | |
def initialize | |
@player1 = Player.new('player1') | |
@player2 = Player.new('player2') | |
@point = Point.new(@player1, @player2) | |
@score_view = ScoreView.new(@player1, @player2) | |
end | |
def score | |
@score_view.render | |
end | |
def play_point | |
@point.play | |
end | |
def play_game | |
until winner | |
puts score | |
puts "#{play_point.name} wins point" | |
end | |
puts winner.name | |
end | |
def winner | |
[@player1, @player2].find do |player| | |
player_won?(player) | |
end | |
end | |
def player_won?(player) | |
player.points >= 4 && | |
player.points - 2 >= [@player1, @player2] | |
.find { |p| p != player}.points | |
end | |
end | |
class ScoreView | |
POINTS = [0, 15, 30, 40] | |
def initialize(player1, player2) | |
@player1 = player1 | |
@player2 = player2 | |
end | |
def render | |
@player1.score <= 3 && @player2.score <= 3 ? regular_score : deuce_score | |
end | |
def player_in_lead | |
@player1.score > @player2.score ? "player 1" : "player 2" | |
end | |
def regular_score | |
"#{POINTS[@player1.score]} - #{POINTS[@player2.score]}" | |
end | |
def deuce_score | |
@player1.score == @player2.score ? "deuce" : "ad #{player_in_lead}" | |
end | |
end | |
class Point | |
def initialize(player1, player2) | |
@player1 = player1 | |
@player2 = player2 | |
end | |
def play | |
winner = [@player1, @player2].sample | |
winner.add_point | |
winner | |
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.log_point_winner(@game.player1) | |
@game.score.should == "15 - 0" | |
end | |
it "should be up 30 - 0 after player 1 wins first two points" do | |
2.times { @game.log_point_winner(@game.player1) } | |
@game.score.should == "30 - 0" | |
end | |
it "should be 40 - 0 after player 1 wins first three points" do | |
3.times { @game.log_point_winner(@game.player1) } | |
@game.score.should == "40 - 0" | |
end | |
it "should be 0 - 15 after player 2 wins first point" do | |
@game.log_point_winner(@game.player2) | |
@game.score.should == "0 - 15" | |
end | |
context 'if at least one player has more than 3 points' do | |
before(:each) do | |
3.times { @game.log_point_winner(@game.player1) } | |
3.times { @game.log_point_winner(@game.player2) } | |
end | |
it "should be deuce if players are tied" do | |
@game.log_point_winner(@game.player1) | |
@game.log_point_winner(@game.player2) | |
@game.score.should == "deuce" | |
end | |
it "should be 'ad player 1' if player 1 is ahead" do | |
@game.log_point_winner(@game.player1) | |
@game.score.should == "ad player 1" | |
end | |
it "should be 'ad player 2' if player 2 is ahead" do | |
@game.log_point_winner(@game.player2) | |
@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.log_point_winner(@game.player1) } | |
@game.winner.should == @game.player1 | |
end | |
it "should be player 2 after winning first four points" do | |
4.times { @game.log_point_winner(@game.player2) } | |
@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.log_point_winner(@game.player1) | |
@game.log_point_winner(@game.player2) | |
} | |
@game.log_point_winner(@game.player1) | |
@game.winner.should be_nil | |
end | |
it "should be declared after one player gets up by at least two" do | |
3.times { | |
@game.log_point_winner(@game.player1) | |
@game.log_point_winner(@game.player2) | |
} | |
@game.log_point_winner(@game.player2) | |
3.times { @game.log_point_winner(@game.player1) } | |
@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