Oshuma-gambler 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/Manifest.txt +31 -0
- data/README +1 -0
- data/README.rdoc +1 -0
- data/README.txt +46 -0
- data/Rakefile +47 -0
- data/bin/gambler_client +214 -0
- data/lib/gambler/card.rb +163 -0
- data/lib/gambler/deck.rb +46 -0
- data/lib/gambler/exceptions.rb +26 -0
- data/lib/gambler/game/blackjack.rb +31 -0
- data/lib/gambler/game.rb +9 -0
- data/lib/gambler/player.rb +64 -0
- data/lib/gambler.rb +24 -0
- data/tasks/ditz.rake +43 -0
- data/tasks/docs.rake +70 -0
- data/tasks/git.rake +27 -0
- data/tasks/override_rake_task.rb +16 -0
- data/tasks/rcov.rake +48 -0
- data/tasks/site.rake +51 -0
- data/tasks/util.rb +32 -0
- data/test/game/test_basic_game.rb +32 -0
- data/test/game/test_blackjack.rb +26 -0
- data/test/helper.rb +6 -0
- data/test/suite.rb +5 -0
- data/test/test_card.rb +113 -0
- data/test/test_deck.rb +67 -0
- data/test/test_gambler.rb +14 -0
- data/test/test_player.rb +96 -0
- metadata +91 -0
data/lib/gambler.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Add the Gambler library path to the front of the Ruby load path.
|
2
|
+
$LOAD_PATH.unshift File.dirname(__FILE__)
|
3
|
+
|
4
|
+
# Third party libraries.
|
5
|
+
require 'rubygems'
|
6
|
+
|
7
|
+
# Gambler libraries.
|
8
|
+
require 'gambler/card'
|
9
|
+
require 'gambler/deck'
|
10
|
+
require 'gambler/exceptions'
|
11
|
+
require 'gambler/game'
|
12
|
+
require 'gambler/player'
|
13
|
+
|
14
|
+
module Gambler
|
15
|
+
include Exceptions
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :debug
|
19
|
+
end
|
20
|
+
|
21
|
+
self.debug = false
|
22
|
+
|
23
|
+
VERSION = '0.0.2'
|
24
|
+
end
|
data/tasks/ditz.rake
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Tasks that make it easier to deal with ditz.
|
2
|
+
|
3
|
+
DOC_ROOT = File.join(GAMBLER_ROOT, 'doc') unless defined? DOC_ROOT
|
4
|
+
ISSUE_DIR = "#{DOC_ROOT}/issues"
|
5
|
+
|
6
|
+
# 'rake issues' will just list them.
|
7
|
+
desc 'List current issues'
|
8
|
+
task :issues do
|
9
|
+
Rake::Task['issues:list'].invoke
|
10
|
+
end
|
11
|
+
|
12
|
+
# Rake tasks for dealing with ditz issues.
|
13
|
+
namespace :issues do
|
14
|
+
desc 'Add an issue'
|
15
|
+
task :add do
|
16
|
+
sh 'ditz add'
|
17
|
+
end
|
18
|
+
|
19
|
+
task :list do
|
20
|
+
# Use back-tick execution to prevent output of command being run.
|
21
|
+
# Returns a string of the command output.
|
22
|
+
puts `ditz todo`
|
23
|
+
end
|
24
|
+
|
25
|
+
# 'rake issues:report' will just generate the report.
|
26
|
+
desc 'Generate issue report'
|
27
|
+
task :report do
|
28
|
+
Rake::Task['issues:report:generate'].invoke
|
29
|
+
end
|
30
|
+
|
31
|
+
namespace :report do
|
32
|
+
task :generate => [ 'issues:report:clear' ] do
|
33
|
+
header "Generating issue report in #{ISSUE_DIR}"
|
34
|
+
sh "ditz html #{ISSUE_DIR}"
|
35
|
+
end
|
36
|
+
|
37
|
+
desc 'Clear the issue report'
|
38
|
+
task :clear do
|
39
|
+
header "Removing issue report from #{ISSUE_DIR}"
|
40
|
+
sh "rm -rf #{ISSUE_DIR}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/tasks/docs.rake
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Tasks to handle application docs.
|
2
|
+
DOC_TITLE = "Gambler v#{Gambler::VERSION} Documentation"
|
3
|
+
DOC_ROOT = File.join(GAMBLER_ROOT, 'doc') unless defined? DOC_ROOT
|
4
|
+
API_DOCS = File.join(DOC_ROOT, 'api')
|
5
|
+
|
6
|
+
# Remove the default Hoe documentation tasks.
|
7
|
+
remove_task 'clobber_docs'
|
8
|
+
remove_task 'docs'
|
9
|
+
remove_task 'docs/index.html'
|
10
|
+
remove_task 'publish_docs'
|
11
|
+
remove_task 'redocs'
|
12
|
+
remove_task 'ridocs'
|
13
|
+
|
14
|
+
# Override the default clobber_docs task.
|
15
|
+
task :clobber_docs do
|
16
|
+
Rake::Task['docs:clear'].invoke
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Generate the Gambler API docs'
|
20
|
+
task :docs do
|
21
|
+
Rake::Task['docs:api'].invoke
|
22
|
+
end
|
23
|
+
|
24
|
+
namespace :docs do
|
25
|
+
task :setup_rdoc do
|
26
|
+
@file_list = FileList[ "#{GAMBLER_ROOT}/README",
|
27
|
+
"#{GAMBLER_ROOT}/lib/**/*.rb",
|
28
|
+
"#{GAMBLER_ROOT}/bin/gambler_client" ]
|
29
|
+
@file_list.add "#{GAMBLER_ROOT}/test/**/*.rb" if ENV['TESTS']
|
30
|
+
# Substitute GAMBLER_ROOT with a dot. Makes for a better index in the generated docs.
|
31
|
+
@files = @file_list.collect {|f| f.gsub(/#{GAMBLER_ROOT}/, '.')}
|
32
|
+
@options = %W[
|
33
|
+
--all
|
34
|
+
--inline-source
|
35
|
+
--line-numbers
|
36
|
+
--main README
|
37
|
+
--op #{API_DOCS}
|
38
|
+
--title '#{DOC_TITLE}'
|
39
|
+
]
|
40
|
+
# Generate a diagram, yes/no?
|
41
|
+
@options << '-d' if RUBY_PLATFORM !~ /win32/ && `which dot` =~ /\/dot/ && !ENV['NODOT']
|
42
|
+
end
|
43
|
+
|
44
|
+
# desc 'Generate the Gambler API docs'
|
45
|
+
task :api => [ :setup_rdoc ] do
|
46
|
+
run_rdoc(@options, @files)
|
47
|
+
system("open #{API_DOCS}/index.html") if RUBY_PLATFORM =~ /darwin/ && ENV['OPEN']
|
48
|
+
end
|
49
|
+
|
50
|
+
desc 'Remove the Gambler API docs'
|
51
|
+
task :clear do
|
52
|
+
system("rm -rf #{API_DOCS}")
|
53
|
+
end
|
54
|
+
|
55
|
+
desc 'Remove and rebuild the Gambler API docs'
|
56
|
+
task :rebuild do
|
57
|
+
Rake::Task['docs:clear'].invoke
|
58
|
+
Rake::Task['docs:api'].invoke
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Runs rdoc with the given +options+ and +files+.
|
65
|
+
# Both arguments should be an Array, which is joined with a space.
|
66
|
+
def run_rdoc(options, files)
|
67
|
+
options = options.join(' ') if options.is_a? Array
|
68
|
+
files = files.join(' ') if files.is_a? Array
|
69
|
+
system("rdoc #{options} #{files}")
|
70
|
+
end
|
data/tasks/git.rake
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# A few helpful tasks for dealing with git.
|
2
|
+
|
3
|
+
# Some defaults.
|
4
|
+
REPOS = ENV['REPOS'] || 'github,rubyforge'
|
5
|
+
BRANCH = ENV['BRANCH'] || 'master'
|
6
|
+
|
7
|
+
namespace :git do
|
8
|
+
desc 'Mirror the local repo on REPOS (comma delimited)'
|
9
|
+
task :mirror do
|
10
|
+
repos.each do |repo|
|
11
|
+
sh "git push --mirror #{repo}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Push BRANCH to REPOS (comma delimited)'
|
16
|
+
task :push do
|
17
|
+
repos.each do |repo|
|
18
|
+
sh "git push #{repo} #{BRANCH}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def repos
|
26
|
+
REPOS.split(',')
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# OverrideRakeTask - http://tools.assembla.com/svn/rails_plugins/override_rake_task/
|
2
|
+
Rake::TaskManager.class_eval do
|
3
|
+
def remove_task(task_name)
|
4
|
+
@tasks.delete(task_name.to_s)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def remove_task(task_name)
|
9
|
+
Rake.application.remove_task(task_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def override_task(args, &block)
|
13
|
+
name, deps = Rake.application.resolve_args(args)
|
14
|
+
remove_task Rake.application[name].name
|
15
|
+
task(args, &block)
|
16
|
+
end
|
data/tasks/rcov.rake
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Tasks to handle rcov usage.
|
2
|
+
|
3
|
+
DOC_ROOT = File.join(GAMBLER_ROOT, 'doc') unless defined? DOC_ROOT
|
4
|
+
COVERAGE_DIR = File.join(DOC_ROOT, 'coverage')
|
5
|
+
|
6
|
+
desc 'Display coverage stats'
|
7
|
+
task :rcov do
|
8
|
+
Rake::Task['rcov:stats'].invoke
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :rcov do
|
12
|
+
task :setup_rcov do
|
13
|
+
@file_list = FileList["#{GAMBLER_ROOT}/test/**/test_*.rb"]
|
14
|
+
@test_files = @file_list.collect { |f| f.gsub(/#{GAMBLER_ROOT}/, '.')}
|
15
|
+
@options = [
|
16
|
+
# Remove rcov.rb from report. Escape the slash, because we need it in the string.
|
17
|
+
"-x 'rcov\\.rb'",
|
18
|
+
"-o #{COVERAGE_DIR}"
|
19
|
+
]
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Remove the generated coverage report'
|
23
|
+
task :clear do
|
24
|
+
system("rm -rf #{COVERAGE_DIR}")
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Generate coverage report'
|
28
|
+
task :html => [ :clear, :setup_rcov ] do
|
29
|
+
@options << '--text-summary'
|
30
|
+
run_rcov(@options, @test_files)
|
31
|
+
system("open #{COVERAGE_DIR}/index.html") if RUBY_PLATFORM =~ /darwin/ && ENV['OPEN']
|
32
|
+
end
|
33
|
+
|
34
|
+
task :stats => [ :setup_rcov ] do
|
35
|
+
@options << '--text-report --no-html'
|
36
|
+
run_rcov(@options, @test_files)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Runs rcov with the given +options+ and +test_files+.
|
43
|
+
# Both arguments should be an Array, which is joined with a space.
|
44
|
+
def run_rcov(options, test_files)
|
45
|
+
options = options.join(' ') if options.is_a? Array
|
46
|
+
test_files = test_files.join(' ') if test_files.is_a? Array
|
47
|
+
system("rcov #{options} #{test_files}")
|
48
|
+
end
|
data/tasks/site.rake
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Tasks for dealing with the RubyForge site.
|
2
|
+
|
3
|
+
# The local directories to store the website files.
|
4
|
+
DOC_ROOT = File.join(GAMBLER_ROOT, 'doc') unless defined? DOC_ROOT
|
5
|
+
LOCAL_SITE = File.join(DOC_ROOT, 'rubyforge.site')
|
6
|
+
SITE_API = File.join(LOCAL_SITE, 'api')
|
7
|
+
SITE_COVERAGE = File.join(LOCAL_SITE, 'coverage')
|
8
|
+
SITE_ISSUES = File.join(LOCAL_SITE, 'issues')
|
9
|
+
|
10
|
+
# The remote directory the files will be scp'ed to.
|
11
|
+
REMOTE_SITE = 'gambler.rubyforge.org:/var/www/gforge-projects/gambler'
|
12
|
+
|
13
|
+
# A list of static pages to upload.
|
14
|
+
PAGES = %w{
|
15
|
+
index.html
|
16
|
+
robots.txt
|
17
|
+
}
|
18
|
+
|
19
|
+
namespace :site do
|
20
|
+
desc 'Clear the local site directory of dynamic files'
|
21
|
+
task :clear_local do
|
22
|
+
header("Clearing local site directory: #{LOCAL_SITE}")
|
23
|
+
sh "rm -rf #{SITE_API}" if File.exists? SITE_API
|
24
|
+
sh "rm -rf #{SITE_COVERAGE}" if File.exists? SITE_COVERAGE
|
25
|
+
sh "rm -rf #{SITE_ISSUES}" if File.exists? SITE_ISSUES
|
26
|
+
end
|
27
|
+
|
28
|
+
# Copies the issue and docs to the site directory.
|
29
|
+
task :setup_dir => [ :clear_local, :run_external_tasks ] do
|
30
|
+
header('Copying coverage and API docs to the site directory.')
|
31
|
+
FileUtils.cp_r(COVERAGE_DIR, LOCAL_SITE)
|
32
|
+
FileUtils.cp_r(API_DOCS, LOCAL_SITE)
|
33
|
+
FileUtils.cp_r(ISSUE_DIR, LOCAL_SITE)
|
34
|
+
end
|
35
|
+
|
36
|
+
desc 'Upload the issues and docs to the website'
|
37
|
+
task :upload => :setup_dir do
|
38
|
+
header('Uploading local site to remote site.')
|
39
|
+
sh "cd #{LOCAL_SITE} && scp -r ./* #{REMOTE_SITE}"
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Used to shorten the setup_site_dir task definition.
|
45
|
+
task :run_external_tasks do
|
46
|
+
ENV['NODOT'] = 'TRUE' # Don't generate dot diagrams for the site docs.
|
47
|
+
Rake::Task['docs:rebuild'].invoke
|
48
|
+
Rake::Task['issues:report'].invoke
|
49
|
+
Rake::Task['rcov:html'].invoke
|
50
|
+
end
|
51
|
+
end
|
data/tasks/util.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Utility methods.
|
2
|
+
|
3
|
+
# Ask for confirmation. Returns true/false
|
4
|
+
def confirm(message, options = {})
|
5
|
+
confirm_message = options[:confirm_message] || 'Are you sure?'
|
6
|
+
banner = options[:banner] || false
|
7
|
+
if banner
|
8
|
+
header(message) # print with header
|
9
|
+
print "#{confirm_message} (yes/no) "
|
10
|
+
choice = STDIN.gets.chomp
|
11
|
+
else
|
12
|
+
puts message
|
13
|
+
print "#{confirm_message} (yes/no) "
|
14
|
+
choice = STDIN.gets.chomp
|
15
|
+
end
|
16
|
+
|
17
|
+
case choice
|
18
|
+
when 'yes'
|
19
|
+
return true
|
20
|
+
else
|
21
|
+
puts 'Aborted'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Displays +message+ inside a formatted header.
|
26
|
+
def header(message = nil)
|
27
|
+
raise ArgumentError, 'No message passed to header.' unless message
|
28
|
+
puts "\n"
|
29
|
+
puts '+---'
|
30
|
+
puts "| #{message}"
|
31
|
+
puts '+---'
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../helper'
|
2
|
+
|
3
|
+
# Test the BasicGame class.
|
4
|
+
class TestBasicGame < Test::Unit::TestCase
|
5
|
+
include Game
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@dale = Player.new('Dale')
|
9
|
+
@kenny = Player.new('Kenny')
|
10
|
+
@game = BasicGame.new(:players => [@dale, @kenny])
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_basic_game_defaults
|
14
|
+
assert_kind_of(BasicGame, @game)
|
15
|
+
assert_equal(10, @game.ante)
|
16
|
+
assert_equal(0, @game.pot)
|
17
|
+
assert_kind_of(Deck, @game.deck)
|
18
|
+
assert(@game.deck.shuffled, "#{@game.deck} is not shuffled!")
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_no_players
|
22
|
+
assert_raise(NoPlayers) do
|
23
|
+
BasicGame.new(:players => []) # empty array
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_invalid_players
|
28
|
+
assert_raise(InvalidPlayers) do
|
29
|
+
BasicGame.new(:players => [@dale, 'WTF'])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../helper'
|
2
|
+
|
3
|
+
# Test the Blackjack class.
|
4
|
+
class TestBlackjack < Test::Unit::TestCase
|
5
|
+
include Game
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@dale = Player.new('Dale')
|
9
|
+
@kenny = Player.new('Kenny')
|
10
|
+
@game = Blackjack.new(:players => [@dale, @kenny])
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_new_blackjack_game
|
14
|
+
assert_kind_of(Blackjack, @game)
|
15
|
+
assert_equal(10, @game.ante)
|
16
|
+
assert_equal(0, @game.pot)
|
17
|
+
assert_kind_of(Deck, @game.deck)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_invalid_player_size
|
21
|
+
assert_raise(InvalidPlayerSize) do
|
22
|
+
Blackjack.new(:players => [])
|
23
|
+
Blackjack.new(:players => [@dale])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/helper.rb
ADDED
data/test/suite.rb
ADDED
data/test/test_card.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
# Tests the Card class.
|
4
|
+
class TestCard < Test::Unit::TestCase
|
5
|
+
def test_all_cards
|
6
|
+
assert_equal(52, Card.all.size)
|
7
|
+
assert_kind_of(Array, Card.all)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_all_iterator
|
11
|
+
Card.all do |card|
|
12
|
+
assert_kind_of(Card, card)
|
13
|
+
assert(Card::FACES.include?(card.face), "#{card.face} was not found in FACES.")
|
14
|
+
assert(Card::SUITS.include?(card.suit), "#{card.suit} was not found in SUITS.")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_order_of_all_cards
|
19
|
+
cards = Card.all
|
20
|
+
assert_equal('Ace of Clubs', cards.first.to_pretty_s)
|
21
|
+
assert_equal('Two of Spades', cards.last.to_pretty_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_each_face
|
25
|
+
Card.each_face do |face|
|
26
|
+
assert(Card::FACES.include?(face), "#{face} was not found in FACES.")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_each_suit
|
31
|
+
Card.each_suit do |suit|
|
32
|
+
assert(Card::SUITS.include?(suit), "#{suit} was not found in SUITS.")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_clubs_class_method
|
37
|
+
clubs = Card.clubs
|
38
|
+
assert_kind_of(Array, clubs)
|
39
|
+
clubs.each do |card|
|
40
|
+
assert_kind_of(Card, card)
|
41
|
+
assert_equal('c', card.suit)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_diamonds_class_method
|
46
|
+
diamonds = Card.diamonds
|
47
|
+
assert_kind_of(Array, diamonds)
|
48
|
+
diamonds.each do |card|
|
49
|
+
assert_kind_of(Card, card)
|
50
|
+
assert_equal('d', card.suit)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_hearts_class_method
|
55
|
+
hearts = Card.hearts
|
56
|
+
assert_kind_of(Array, hearts)
|
57
|
+
hearts.each do |card|
|
58
|
+
assert_kind_of(Card, card)
|
59
|
+
assert_equal('h', card.suit)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_spades_class_method
|
64
|
+
spades = Card.spades
|
65
|
+
assert_kind_of(Array, spades)
|
66
|
+
spades.each do |card|
|
67
|
+
assert_kind_of(Card, card)
|
68
|
+
assert_equal('s', card.suit)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_face_value
|
73
|
+
card = Card.new(:face => 'K', :suit => 'd')
|
74
|
+
assert_equal(13, card.face_value)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_to_s
|
78
|
+
card = Card.new(:face => 'K', :suit => 'd')
|
79
|
+
assert_equal('Kd', card.to_s)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_to_pretty_s
|
83
|
+
card = Card.new(:face => 'K', :suit => 'd')
|
84
|
+
assert_equal('King of Diamonds', card.to_pretty_s)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_new_card_from_array
|
88
|
+
card = %w{ K d }
|
89
|
+
assert_kind_of(Card, Card.new(card))
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_new_card_from_hash
|
93
|
+
card = { :face => 'K', :suit => 'd' }
|
94
|
+
assert_kind_of(Card, Card.new(card))
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_new_card_from_string
|
98
|
+
card = 'Kd'
|
99
|
+
assert_kind_of(Card, Card.new(card))
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_invalid_card_initializer
|
103
|
+
assert_raise(InvalidCardType) { Card.new(420) }
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_invalid_card_from_hash
|
107
|
+
assert_raise(InvalidCardType) { Card.new(:wtf => 'ZOMG') }
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_invalid_card_from_string
|
111
|
+
assert_raise(InvalidCardType) { Card.new('WTF') }
|
112
|
+
end
|
113
|
+
end
|
data/test/test_deck.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
# Tests the Deck class.
|
4
|
+
class TestDeck < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@deck = Deck.new
|
7
|
+
@dale = Player.new('Dale')
|
8
|
+
@kenny = Player.new('Kenny')
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_new_deck
|
12
|
+
new_deck = Deck.new
|
13
|
+
assert_kind_of(Deck, new_deck)
|
14
|
+
assert_equal(false, new_deck.shuffled)
|
15
|
+
assert_equal(52, new_deck.size)
|
16
|
+
assert_kind_of(Array, new_deck.cards)
|
17
|
+
new_deck.cards.each do |card|
|
18
|
+
assert_kind_of(Card, card)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_cards_option
|
23
|
+
new_deck = Deck.new(:cards => Card.diamonds)
|
24
|
+
assert_kind_of(Deck, new_deck)
|
25
|
+
assert_equal(Card.diamonds.size, new_deck.size)
|
26
|
+
new_deck.cards.each do |card|
|
27
|
+
assert_equal('d', card.suit)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_invalid_cards
|
32
|
+
assert_raise(InvalidDeck) do
|
33
|
+
Deck.new(:cards => 'WTF')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_shuffle!
|
38
|
+
first_card = @deck.cards.first
|
39
|
+
last_card = @deck.cards.last
|
40
|
+
@deck.shuffle!
|
41
|
+
assert(@deck.shuffled, "#{@deck} not shuffled!")
|
42
|
+
assert_not_equal(first_card, @deck.cards.first)
|
43
|
+
assert_not_equal(last_card, @deck.cards.last)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_deal_to
|
47
|
+
assert_equal(52, @deck.size)
|
48
|
+
assert(@dale.hand.empty?, "#{@dale}'s hand is not empty!")
|
49
|
+
assert(@kenny.hand.empty?, "#{@kenny}'s hand is not empty!")
|
50
|
+
2.times do
|
51
|
+
@deck.deal_to @dale
|
52
|
+
@deck.deal_to @kenny
|
53
|
+
end
|
54
|
+
assert_equal(48, @deck.size)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_deck_empty
|
58
|
+
# Deal out all the Cards first.
|
59
|
+
@deck.size.times do
|
60
|
+
@deck.deal_to(@dale)
|
61
|
+
end
|
62
|
+
|
63
|
+
assert_raise(DeckEmpty) do
|
64
|
+
@deck.deal_to(@kenny)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
# Tests the Gambler module.
|
4
|
+
class TestGambler < Test::Unit::TestCase
|
5
|
+
def test_version_string
|
6
|
+
assert_equal(Gambler::VERSION, '0.0.2')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_debug
|
10
|
+
assert_equal(Gambler.debug, false)
|
11
|
+
Gambler.debug = true
|
12
|
+
assert_equal(Gambler.debug, true)
|
13
|
+
end
|
14
|
+
end
|
data/test/test_player.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
# Tests the Player class.
|
4
|
+
class TestPlayer < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@player = Player.new('Dale')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_pretty_inspect
|
10
|
+
assert_equal("#<Gambler::Player 'Dale'>", @player.inspect)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_to_s
|
14
|
+
assert_equal('Dale ($100)', @player.to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_new_player
|
18
|
+
dale = Player.new('Dale', :chips => 10) # broke :(
|
19
|
+
assert_kind_of(Player, dale)
|
20
|
+
assert_equal('Dale', dale.name)
|
21
|
+
assert_equal(10, dale.chips)
|
22
|
+
assert(dale.hand.empty?)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_player_name
|
26
|
+
@player = Player.new('Dale')
|
27
|
+
assert_equal('Dale', @player.name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_no_player_name
|
31
|
+
assert_raise(ArgumentError) { Player.new }
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_change_name
|
35
|
+
@player.name = 'Not Dale'
|
36
|
+
assert_equal('Not Dale', @player.name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_player_hand
|
40
|
+
assert(@player.hand.empty?)
|
41
|
+
@player = Player.new( 'Cheaty McGee', # has blackjack from the start!
|
42
|
+
:hand => [Card.new('Ad'), Card.new('Kd')] )
|
43
|
+
assert_equal(2, @player.hand.size)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_empty_hand!
|
47
|
+
@player = Player.new('Dale', :hand => [Card.new('Ad'), Card.new('Kd')])
|
48
|
+
assert_equal(2, @player.hand.size)
|
49
|
+
@player.empty_hand!
|
50
|
+
assert_equal(0, @player.hand.size)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_view_hand_as_array
|
54
|
+
@player = Player.new('Dale', :hand => [Card.new('Ad'), Card.new('Kd')])
|
55
|
+
assert_equal(['Ad', 'Kd'], @player.view_hand) # defaults to non-pretty array.
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_view_hand_as_pretty_array
|
59
|
+
@player = Player.new('Dale', :hand => [Card.new('Ad'), Card.new('Kd')])
|
60
|
+
pretty_hand = ['Ace of Diamonds', 'King of Diamonds']
|
61
|
+
assert_equal(pretty_hand, @player.view_hand(:format => :array, :pretty => true))
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_view_hand_as_string
|
65
|
+
@player = Player.new('Dale', :hand => [Card.new('Ad'), Card.new('Kd')])
|
66
|
+
assert_equal('Ad Kd', @player.view_hand(:format => :string))
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_view_hand_as_pretty_string
|
70
|
+
@player = Player.new('Dale', :hand => [Card.new('Ad'), Card.new('Kd')])
|
71
|
+
pretty_hand = 'Ace of Diamonds, King of Diamonds'
|
72
|
+
assert_equal(pretty_hand, @player.view_hand(:format => :string, :pretty => true))
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_default_chip_stack
|
76
|
+
assert_equal(100, @player.chips)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_player_chips
|
80
|
+
@player = Player.new('Dale', :chips => 500)
|
81
|
+
assert_equal(500, @player.chips)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_invalid_chip_count
|
85
|
+
assert_raise(InvalidChipCount) do
|
86
|
+
Player.new('Dale', :chips => 'WTF')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_change_chip_count
|
91
|
+
pot = 1_000_000_000
|
92
|
+
chips = @player.chips
|
93
|
+
@player.chips += pot # You win!
|
94
|
+
assert_equal(pot + chips, @player.chips)
|
95
|
+
end
|
96
|
+
end
|