Skip to content

Instantly share code, notes, and snippets.

@dball
Created January 4, 2011 18:47
Show Gist options
  • Save dball/765194 to your computer and use it in GitHub Desktop.
Save dball/765194 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/spades'
require 'minitest/autorun'
describe Deal do
before do
@deal = Deal.new
end
it "should default to 13 clubs dealt to the first player" do
@deal.clubs.must_equal Array.new(13, 1)
end
describe "iterating over deals" do
it "should increment the player of the first club that doesn't belong to the last player" do
@deal.succ!
@deal.clubs.must_equal [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@deal.succ!
@deal.clubs.must_equal [3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
end
it "should reset and increment the next club not belonging to the last player" do
@deal.clubs = [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@deal.succ!.clubs.must_equal [1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@deal.clubs = [4, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@deal.succ!.clubs.must_equal [1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
end
it "should return nil when all clubs belong to the last player" do
@deal.clubs = [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
@deal.succ!.must_equal nil
end
end
describe "winning" do
it "should know the highest of the players' lowest clubs wins" do
@deal.winner.must_equal 1
@deal.clubs = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]
@deal.winner.must_equal 13
@deal.clubs = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]
@deal.winner.must_equal 12
@deal.clubs = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1]
@deal.winner.must_equal 4
end
end
describe "summary" do
it "should be able to generate and score all of the deals" do
total = 0
wins = Array.new(13, 0)
begin
wins[@deal.winner - 1] += 1
total += 1
end while @deal.succ!
subtotal = 0
wins.each_with_index do |number, i|
puts "#{i + 1}\t#{number}\t#{number.to_f / total}"
subtotal += number
end
total.must_equal 4 ** 13
subtotal.must_equal total
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment