Skip to content

Instantly share code, notes, and snippets.

@haru01
Forked from kakutani/game_spec.rb
Created April 14, 2010 13:13
Show Gist options
  • Save haru01/365800 to your computer and use it in GitHub Desktop.
Save haru01/365800 to your computer and use it in GitHub Desktop.
$:.unshift(File.dirname(__FILE__))
require 'spec_helper'
module BowlongGameMacro
class BowlongGamePlayer
def initialize
@game = Game.new
end
def roll_spare
@game.roll(5)
@game.roll(5)
end
def roll_strike
@game.roll(10)
end
def roll_gutter
@game.roll(0)
end
def roll_on_frame(*pins)
pins.each do |pin|
@game.roll(pin)
end
end
alias :roll :roll_on_frame
def score
@game.score
end
end
def self.included(base)
base.extend(ClassMethods)
base.instance_eval do
alias :score :it
end
end
module ClassMethods
def play_game(desc = nil, &block)
before do
@__player = BowlongGamePlayer.new
@__player.instance_eval(&block)
end
subject { @__player.score }
end
end
end
describe Game, "#score" do
include BowlongGameMacro
context "すべてガターの場合" do
play_game { 20.times { roll_gutter } }
score { should == 0 }
end
context "すべて1ピンの場合" do
play_game { 20.times { roll_on_frame(1) } }
score { should == 20 }
end
context "スペアの場合" do
play_game do
roll_spare
roll_on_frame(4, 3)
16.times { roll_gutter }
# (10 + 4) + 4 + 3 => 21
end
describe "スペアの次の投球で倒したピンがボーナス加算される" do
score { should == 21 }
end
end
context "ストライクの場合" do
play_game do
roll_strike
roll_on_frame(4, 3)
16.times { roll_gutter }
# 10 + (4 + 3) + 4 + 3 => 24
end
describe "次のフレームの得点がボーナス加算される" do
score { should == 24 }
end
end
context "ストライク、ガター、スペアの場合" do
play_game do
roll_strike
roll_on_frame(0, 10)
roll_on_frame(7, 2)
(7 * 2).times { roll_gutter }
# 10 + (0 + 10) + 10 + (7) + 7 + 2 => 46
end
describe "ストライク後のフレームが加算されて、かつスペアの加算も成立する" do
score { should == 46 }
end
end
context "紆余曲折あったゲームの場合" do
play_game do
[1,4,4,5,6,4,5,5,10,0,1,7,3,6,4,10,2,8,6].each do |pin|
roll(pin)
end
end
describe "Bob Martinの受け入れケースのスコア通りになる" do
score { should == 133 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment