ventiuna 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class TestDeck < Test::Unit::TestCase
4
+ def test_init
5
+ assert deck = Ventiuna::Deck.new
6
+ assert_equal(52, deck.cards.size)
7
+ end
8
+
9
+ def test_shuffle
10
+ deck = Ventiuna::Deck.new
11
+ assert deck.shuffle
12
+
13
+ assert Ventiuna::Deck.new.cards == Ventiuna::Deck.new.cards, "two unsuffled decks should matchn"
14
+
15
+ # it's possible these two decks will be shuffled into the same order, but very unlikely
16
+ deck1 = Ventiuna::Deck.new.shuffle
17
+ deck2 = Ventiuna::Deck.new.shuffle
18
+ assert deck1 != deck2, "two shuffled decks should not match"
19
+ end
20
+
21
+ def test_pop
22
+ deck = Ventiuna::Deck.new.shuffle
23
+ assert card = deck.pop
24
+ assert card.class == Ventiuna::Card
25
+ assert_equal(51, deck.size)
26
+
27
+ card2 = deck.pop
28
+ assert card != card2
29
+ assert_equal(50, deck.size)
30
+
31
+ deck = Ventiuna::Deck.new.shuffle
32
+ 52.times do
33
+ card = deck.pop
34
+ assert card.class == Ventiuna::Card
35
+ end
36
+ assert_nil deck.pop
37
+ assert_equal(0, deck.size)
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class TestGame < Test::Unit::TestCase
4
+ def setup
5
+ #@player = Ventiuna::Player.new("TestPlayer")
6
+ #@game = Ventiuna::Game.new(@player)
7
+ end
8
+
9
+ end
@@ -0,0 +1,199 @@
1
+ require 'test_helper'
2
+
3
+ class TestHand < Test::Unit::TestCase
4
+ def setup
5
+ @duce = Ventiuna::Card.new(rank: "2", suit: "club")
6
+ @tres = Ventiuna::Card.new(rank: "3", suit: "diamond")
7
+ @quatro = Ventiuna::Card.new(rank: "4", suit: "heart")
8
+
9
+ @seven_of_diamonds = Ventiuna::Card.new(rank: "7", suit: "diamond")
10
+ @eight_of_spades = Ventiuna::Card.new(rank: "8", suit: "spade")
11
+ @eight_of_clubs = Ventiuna::Card.new(rank: "8", suit: "club")
12
+
13
+ @jack_of_clubs = Ventiuna::Card.new(rank: "J", suit: "club")
14
+ @king_of_hearts = Ventiuna::Card.new(rank: "K", suit: "heart")
15
+ @ace_of_diamonds = Ventiuna::Card.new(rank: "A", suit: "diamond")
16
+ @ace_of_spades = Ventiuna::Card.new(rank: "A", suit: "spade")
17
+ end
18
+
19
+ def test_init
20
+ assert Ventiuna::Hand.new
21
+ assert Ventiuna::Hand.new(@duce)
22
+ assert Ventiuna::Hand.new(@duce, @tres)
23
+ assert hand = Ventiuna::Hand.new(@duce, @tres, @quatro)
24
+
25
+ assert hand.cards.include?(@duce)
26
+ assert hand.cards.include?(@tres)
27
+ assert hand.cards.include?(@quatro)
28
+ end
29
+
30
+ def test_empty_hand
31
+ assert_equal(0, Ventiuna::Hand.new.value)
32
+ end
33
+
34
+ def test_add_cards
35
+ hand = Ventiuna::Hand.new
36
+ hand << @duce
37
+ hand << @tres
38
+ hand << @quatro
39
+
40
+ assert hand.cards.include?(@duce)
41
+ assert hand.cards.include?(@tres)
42
+ assert hand.cards.include?(@quatro)
43
+ end
44
+
45
+ def test_value
46
+ hand = Ventiuna::Hand.new
47
+ hand << @duce
48
+ assert_equal(2, hand.value)
49
+
50
+ hand << @king_of_hearts
51
+ assert_equal(12, hand.value)
52
+
53
+ hand << @ace_of_diamonds
54
+ assert_equal(13, hand.value)
55
+
56
+ hand << @quatro
57
+ assert_equal(17, hand.value)
58
+
59
+ hand << @tres
60
+ assert_equal(20, hand.value)
61
+
62
+ hand << @jack_of_clubs
63
+ assert_equal(30, hand.value)
64
+ end
65
+
66
+ def test_value_with_soft_hand
67
+ hand = Ventiuna::Hand.new
68
+ hand << @ace_of_diamonds
69
+ assert_equal(11, hand.value)
70
+
71
+ hand << @duce
72
+ assert_equal(13, hand.value)
73
+
74
+ hand << @king_of_hearts
75
+ assert_equal(13, hand.value)
76
+
77
+ hand << @ace_of_spades
78
+ assert_equal(14, hand.value)
79
+ end
80
+
81
+ def test_value_with_two_aces
82
+ hand = Ventiuna::Hand.new
83
+ hand << @tres
84
+ assert_equal(3, hand.value)
85
+
86
+ hand << @ace_of_spades
87
+ assert_equal(14, hand.value)
88
+
89
+ hand << @ace_of_diamonds
90
+ assert_equal(15, hand.value)
91
+
92
+ hand << @seven_of_diamonds
93
+ assert_equal(12, hand.value)
94
+ end
95
+
96
+ def test_value_hit_on_11
97
+ hand = Ventiuna::Hand.new
98
+ hand.hit @tres
99
+ assert_equal(3, hand.value)
100
+
101
+ hand.hit @eight_of_spades
102
+ assert_equal(11, hand.value)
103
+
104
+ hand.hit @ace_of_diamonds
105
+ assert_equal(12, hand.value)
106
+ end
107
+
108
+ def test_value_hit_on_10
109
+ hand = Ventiuna::Hand.new
110
+ hand.hit @eight_of_spades
111
+ assert_equal(8, hand.value)
112
+
113
+ hand.hit @duce
114
+ assert_equal(10, hand.value)
115
+
116
+ hand.hit @ace_of_diamonds
117
+ assert_equal(21, hand.value)
118
+ end
119
+
120
+ def test_blackjack?
121
+ hand = Ventiuna::Hand.new(@king_of_hearts, @ace_of_diamonds)
122
+ assert hand.blackjack?
123
+
124
+ hand = Ventiuna::Hand.new(@eight_of_spades, @duce, @ace_of_diamonds)
125
+ assert !hand.blackjack?
126
+ end
127
+
128
+ def test_pair?
129
+ hand = Ventiuna::Hand.new(@eight_of_spades, @eight_of_clubs)
130
+ assert hand.pair?
131
+
132
+ hand = Ventiuna::Hand.new(@king_of_hearts, @jack_of_clubs)
133
+ assert hand.pair?
134
+
135
+ hand = Ventiuna::Hand.new(@ace_of_diamonds, @ace_of_spades)
136
+ assert hand.pair?
137
+
138
+ hand = Ventiuna::Hand.new(@eight_of_spades, @ace_of_spades, @eight_of_clubs)
139
+ assert !hand.pair?
140
+ end
141
+
142
+ def test_soft?
143
+ hand = Ventiuna::Hand.new(@ace_of_spades, @duce)
144
+ assert hand.soft?
145
+
146
+ hand = Ventiuna::Hand.new(@tres, @ace_of_spades, @ace_of_diamonds)
147
+ assert hand.soft?
148
+
149
+ hand = Ventiuna::Hand.new(@tres, @ace_of_spades, @ace_of_diamonds, @seven_of_diamonds)
150
+ assert !hand.soft?
151
+
152
+ hand = Ventiuna::Hand.new(@ace_of_diamonds, @quatro)
153
+ assert hand.soft?
154
+ hand << @jack_of_clubs
155
+ hand << @king_of_hearts
156
+ assert !hand.soft?
157
+
158
+ end
159
+
160
+ def test_stand
161
+ hand = Ventiuna::Hand.new(@king_of_hearts, @jack_of_clubs)
162
+ hand.stand
163
+ assert !hand.active?
164
+ end
165
+
166
+ def test_split
167
+ hand = Ventiuna::Hand.new(@ace_of_diamonds, @ace_of_spades)
168
+
169
+ assert hand.split
170
+ assert hand.split.kind_of?(Array)
171
+ assert_equal(2, hand.split.size)
172
+ assert hand.split.collect{ |h| h.kind_of?(Ventiuna::Hand) }.all?
173
+ end
174
+
175
+ def test_bust?
176
+ hand = Ventiuna::Hand.new(@ace_of_diamonds, @quatro)
177
+ assert !hand.bust?
178
+
179
+ hand << @jack_of_clubs
180
+ assert !hand.bust?
181
+
182
+ hand << @king_of_hearts
183
+ assert hand.bust?
184
+ end
185
+
186
+ def test_to_db
187
+ hand = Ventiuna::Hand.new(@ace_of_diamonds, @duce)
188
+ assert_equal("13S", hand.to_db)
189
+
190
+ hand = Ventiuna::Hand.new(@seven_of_diamonds, @tres)
191
+ assert_equal("10", hand.to_db)
192
+
193
+ hand = Ventiuna::Hand.new(@ace_of_diamonds, @ace_of_spades)
194
+ assert_equal("12SP", hand.to_db)
195
+
196
+ hand = Ventiuna::Hand.new(@jack_of_clubs, @king_of_hearts)
197
+ assert_equal("20P", hand.to_db)
198
+ end
199
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'ventiuna'
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ class TestMove < Test::Unit::TestCase
4
+ def setup
5
+ @strategy = Ventiuna::Strategy.find_or_create_by_name("Test")
6
+ #@move = Ventiuna::Move.new
7
+ end
8
+
9
+ def test_save
10
+ m = Ventiuna::Move.new(strategy_id: @strategy.id, dealers_card_value: 11, hand: "12SP", decision: "s")
11
+ assert m.save
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class TestPlayer < Test::Unit::TestCase
4
+ def setup
5
+ @player = Ventiuna::Player.new("Player1")
6
+ @ace_of_spades = Ventiuna::Card.new(rank: "A", suit: "spade")
7
+ end
8
+
9
+ def test_init
10
+ player = Ventiuna::Player.new("TestPlayer")
11
+ assert_equal("TestPlayer", player.name)
12
+ assert player.hands
13
+ assert player.stats
14
+ assert player.balance.kind_of?(Numeric)
15
+ end
16
+
17
+ def test_place_bet
18
+ assert @player.place_bet(5, 1000).kind_of?(Numeric)
19
+ end
20
+
21
+ def test_active_hand
22
+ assert @player.active_hand.kind_of?(Ventiuna::Hand)
23
+ assert @player.hand.kind_of?(Ventiuna::Hand)
24
+ assert_equal(@player.hand, @player.active_hand)
25
+ end
26
+
27
+ def test_reset_hand
28
+ @player.active_hand << @ace_of_spades
29
+ assert @player.active_hand.value != 0
30
+
31
+ @player.reset_hand
32
+ assert_equal(0, @player.hand.value)
33
+ end
34
+
35
+ def test_decision
36
+ assert ["s", "h", "d", "sp"].include?(@player.decision(@ace_of_spades))
37
+ end
38
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class TestShoe < Test::Unit::TestCase
4
+ def test_init
5
+ assert one_deck_shoe = Ventiuna::Shoe.new(1)
6
+
7
+ two_deck_shoe = Ventiuna::Shoe.new(2)
8
+ assert_equal(104, two_deck_shoe.cards.size)
9
+ end
10
+
11
+ def test_shuffle
12
+ assert six_deck_shoe = Ventiuna::Shoe.new(6).shuffle
13
+ assert_equal(312, six_deck_shoe.size)
14
+
15
+ assert Ventiuna::Shoe.new(8).cards == Ventiuna::Shoe.new(8).cards, "two unsuffled shoes should match"
16
+
17
+ shoe1 = Ventiuna::Shoe.new(8).shuffle
18
+ shoe2 = Ventiuna::Shoe.new(8).shuffle
19
+ assert shoe1 != shoe2, "two shuffled shoes should not match"
20
+ end
21
+
22
+ def test_pop
23
+ eight_deck_shoe = Ventiuna::Shoe.new(8).shuffle
24
+ assert_equal(416, eight_deck_shoe.size)
25
+ assert eight_deck_shoe.pop.kind_of?(Ventiuna::Card)
26
+ assert_equal(415, eight_deck_shoe.size)
27
+ end
28
+
29
+ def test_needs_shuffling?
30
+ eight_deck_shoe = Ventiuna::Shoe.new(8)
31
+ assert eight_deck_shoe.needs_shuffling?
32
+
33
+ eight_deck_shoe.shuffle
34
+ assert !eight_deck_shoe.needs_shuffling?
35
+
36
+ eight_deck_shoe = Ventiuna::Shoe.new(8).shuffle
37
+ 104.times do
38
+ eight_deck_shoe.pop
39
+ end
40
+ assert !eight_deck_shoe.needs_shuffling?
41
+
42
+ eight_deck_shoe = Ventiuna::Shoe.new(8).shuffle
43
+ 312.times do
44
+ eight_deck_shoe.pop
45
+ end
46
+ assert eight_deck_shoe.needs_shuffling?
47
+
48
+ eight_deck_shoe = Ventiuna::Shoe.new(8).shuffle
49
+ 360.times do
50
+ eight_deck_shoe.pop
51
+ end
52
+ assert eight_deck_shoe.needs_shuffling?
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class TestStrategy < Test::Unit::TestCase
4
+ def setup
5
+ @strategy = Ventiuna::Strategy.find_or_create_by_name("Test")
6
+ #@strategy.moves.destroy_all
7
+ @move = Ventiuna::Move.new(dealers_card_value: 11, hand: "21S", decision: "s")
8
+ end
9
+
10
+ def test_save
11
+ @strategy.name = "Test"
12
+ #assert @strategy.save
13
+ end
14
+
15
+ def test_associations
16
+ assert @strategy.moves
17
+ assert @strategy.moves << @move
18
+ #assert_equal(1, @strategy.moves.size)
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ventiuna/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ventiuna"
8
+ spec.version = Ventiuna::VERSION
9
+ spec.authors = ["Dru Ibarra"]
10
+ spec.email = ["dibarra@apple.com"]
11
+ spec.description = %q{Blackjack game and simulator}
12
+ spec.summary = %q{Blackjack game and simulator}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord"
22
+ spec.add_dependency "sqlite3"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ventiuna
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dru Ibarra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Blackjack game and simulator
70
+ email:
71
+ - dibarra@apple.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - img/bender.jpg
82
+ - lib/ventiuna.rb
83
+ - lib/ventiuna/card.rb
84
+ - lib/ventiuna/database/connection.rb
85
+ - lib/ventiuna/database/schema.rb
86
+ - lib/ventiuna/database/ventiuna_strategy.db
87
+ - lib/ventiuna/dealer.rb
88
+ - lib/ventiuna/deck.rb
89
+ - lib/ventiuna/game.rb
90
+ - lib/ventiuna/hand.rb
91
+ - lib/ventiuna/player.rb
92
+ - lib/ventiuna/shoe.rb
93
+ - lib/ventiuna/strategies/move.rb
94
+ - lib/ventiuna/strategies/strategy.rb
95
+ - lib/ventiuna/version.rb
96
+ - test/test_card.rb
97
+ - test/test_dealer.rb
98
+ - test/test_deck.rb
99
+ - test/test_game.rb
100
+ - test/test_hand.rb
101
+ - test/test_helper.rb
102
+ - test/test_move.rb
103
+ - test/test_player.rb
104
+ - test/test_shoe.rb
105
+ - test/test_strategy.rb
106
+ - ventiuna.gemspec
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Blackjack game and simulator
131
+ test_files:
132
+ - test/test_card.rb
133
+ - test/test_dealer.rb
134
+ - test/test_deck.rb
135
+ - test/test_game.rb
136
+ - test/test_hand.rb
137
+ - test/test_helper.rb
138
+ - test/test_move.rb
139
+ - test/test_player.rb
140
+ - test/test_shoe.rb
141
+ - test/test_strategy.rb