mblackjack 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 81cbb9acc9fe93245cbd3da034013b02ff6e6422
4
+ data.tar.gz: 39101523373da1e84d590a0d8f9faf143cd77637
5
+ SHA512:
6
+ metadata.gz: fb4b4d836f695a4c0e66d07979cab49b1391e3602c436a2c4bd5e12a9de96017e1661cd0df6e66597ace2588ab251dd72f8b3c9908e1f45774d44700cbdc12e3
7
+ data.tar.gz: dd5f4229eeaadcf29d348002eef958045cc1418c9c611a685fed0d9be03e81d08f0d483ec501a0688eebad3243482514cc1ffd03e6cf05783f0b93c191d42d16
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mblackjack.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 bellvat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Mblackjack
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mblackjack`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mblackjack'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mblackjack
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mblackjack.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mblackjack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/m_blackjack ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+ bin_file = Pathname.new(__FILE__).realpath
4
+
5
+ # add self to libpath
6
+ $LOAD_PATH.unshift File.expand_path('../../lib', bin_file)
7
+
8
+ require 'mblackjack'
9
+ Mblackjack::CLI.run
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ class Dealer
2
+ attr_accessor :dealer_hand
3
+
4
+ def initialize
5
+ @dealer_hand = Hand.new
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ class Deck
2
+ attr_accessor :unique_cards, :cards, :ace
3
+
4
+ def initialize
5
+ @unique_cards = ['ace',2,3,4,5,6,7,8,9,10,'J','Q','K']
6
+
7
+ @cards = []
8
+ @@card_values = {}
9
+
10
+ make_deck
11
+ assign_value
12
+ end
13
+
14
+ def make_deck
15
+ @cards = []
16
+ @cards << unique_cards * 4
17
+ @cards = @cards.flatten
18
+ end
19
+
20
+ def self.card_values
21
+ @@card_values
22
+ end
23
+
24
+ def assign_value
25
+ v = 1
26
+ unique_cards.each do |c|
27
+ @@card_values.store(c, v)
28
+ v += 1
29
+ end
30
+ @@card_values['ace'] = [1,11]
31
+ @@card_values['J'] = 10
32
+ @@card_values['Q'] = 10
33
+ @@card_values['K'] = 10
34
+ end
35
+ end
@@ -0,0 +1,226 @@
1
+ class Game
2
+ attr_accessor :deck, :dealer, :no_of_players, :player_arr, :hand_arr
3
+
4
+ def initialize(player_count=1)
5
+ @deck = Deck.new
6
+ @dealer = Dealer.new
7
+ @no_of_players = 0
8
+ @player_arr = [@player1, @player2, @player3, @player4]
9
+ @hand_arr = [@hand1, @hand2, @hand3, @hand4, @hand5, @hand6]
10
+ end
11
+
12
+
13
+
14
+ def record_no_of_players
15
+ puts "How many players are playing today?"
16
+ @no_of_players = gets.chomp.to_i
17
+
18
+ @no_of_players.times do |c|
19
+ @player_arr[c] = Player.new(100)
20
+ @player_arr[c].player_no = (c+1)
21
+ end
22
+ end
23
+
24
+ def start
25
+ if no_of_players < 1
26
+ record_no_of_players
27
+ end
28
+ assign_hand_and_bet
29
+ assign_first_two_round_cards
30
+ show_player_cards
31
+ check_for_two_card_blackjack
32
+ show_one_dealer_card
33
+ player_choose_action
34
+ dealer_draws
35
+ compare_with_dealer
36
+ ask_if_play_again
37
+ end
38
+
39
+ private
40
+
41
+ def ask_if_play_again
42
+ p "Would you like to play again? (y/n)"
43
+ answer = gets.chomp
44
+ if answer == "y"
45
+ reshuffle_cards
46
+ start
47
+ else
48
+ p "Thanks for playing!"
49
+ end
50
+ end
51
+
52
+ def deal_cards
53
+ @dealt_cards = deck.cards.sample(1)
54
+ end
55
+
56
+ def remove_card_from_deck
57
+ ind = deck.cards.find_index(@dealt_cards[0])
58
+ deck.cards.delete_at(ind)
59
+ end
60
+
61
+ def assign_hand_and_bet
62
+ no_of_players.times do |n|
63
+ puts "Player #{@player_arr[n].player_no}, how many hands would you like to play?"
64
+ h = gets.chomp.to_i
65
+ @player_arr[n].no_of_hands = h
66
+ h.times do |h|
67
+ @hand_arr[h] = Hand.new
68
+ @hand_arr[h].hand_no = (h+1)
69
+ puts "How much would you like to bet for hand #{@hand_arr[h].hand_no}? "
70
+ b = gets.chomp.to_i
71
+ @hand_arr[h].bet = b
72
+ @player_arr[n].hand << hand_arr[h]
73
+ end
74
+ end
75
+ end
76
+
77
+ def assign_first_two_round_cards
78
+ assign_card
79
+ assign_card
80
+ end
81
+
82
+ def assign_card
83
+ #Assign to players
84
+ no_of_players.times do |n|
85
+ @player_arr[n].no_of_hands.times do |h|
86
+ @player_arr[n].hand[h].cards_in_hand << deal_cards[0]
87
+ remove_card_from_deck
88
+ end
89
+ end
90
+
91
+ #Assign to dealer (WILL HAVE TO FLIP ONE CARD)
92
+ dealer.dealer_hand.cards_in_hand << deal_cards[0]
93
+ remove_card_from_deck
94
+ end
95
+
96
+ def show_player_cards
97
+ no_of_players.times do |n|
98
+ @player_arr[n].no_of_hands.times do |h|
99
+ puts "Player #{@player_arr[n].player_no}: Hand #{@player_arr[n].hand[h].hand_no} cards: #{@player_arr[n].hand[h].cards_in_hand}."
100
+ @player_arr[n].hand[h].calc_hand_value
101
+ end
102
+ end
103
+ end
104
+
105
+ def show_one_dealer_card
106
+ puts "Dealer one card show: #{dealer.dealer_hand.cards_in_hand[0]}"
107
+ end
108
+
109
+ def bust? (card_v)
110
+ card_v > 21
111
+ end
112
+
113
+ def player_choose_action
114
+ no_of_players.times do |n|
115
+ hands_left = @player_arr[n].hand - @player_arr[n].hand_done
116
+ while hands_left != []
117
+ hands_left.each do |hand|
118
+ hand_finished = 0
119
+ while hand_finished == 0
120
+ puts "Player #{@player_arr[n].player_no}, for hand #{hand.hand_no} cards: #{hand.cards_in_hand}, would you like to stand(s), hit(h), double down(d) or split(sp)?"
121
+ choice = gets.chomp
122
+ hand.choices << choice
123
+
124
+ if choice == "h"
125
+ hand.cards_in_hand << deal_cards[0]
126
+ puts "Player cards: #{hand.cards_in_hand}."
127
+ remove_card_from_deck
128
+ hand.calc_hand_value
129
+ if bust? (hand.hand_value)
130
+ puts "Cards: #{hand.cards_in_hand}. Busted!"
131
+ @player_arr[n].hand_done << hand
132
+ hand_finished = 1
133
+ elsif hand.choices[-2] == "d"
134
+ @player_arr[n].hand_done << hand
135
+ hand_finished = 1
136
+ end
137
+ elsif choice == "d"
138
+ new_bet = (hand.bet *= 2)
139
+ puts "New bet is #{new_bet} dollars."
140
+ elsif choice == "sp"
141
+ popped = hand.cards_in_hand.pop
142
+ popped
143
+ hand.calc_hand_value
144
+ new_hand_no = @player_arr[n].no_of_hands + 1
145
+ @hand_arr[new_hand_no] = Hand.new
146
+ @hand_arr[new_hand_no].cards_in_hand << popped
147
+ @hand_arr[new_hand_no].hand_no = new_hand_no
148
+ @hand_arr[new_hand_no].calc_hand_value
149
+ @hand_arr[new_hand_no].bet = hand.bet
150
+ @player_arr[n].hand << @hand_arr[new_hand_no]
151
+ @player_arr[n].no_of_hands += 1
152
+ elsif choice == "s"
153
+ p "Alright, you stand."
154
+ @player_arr[n].hand_done << hand
155
+ hand_finished = 1
156
+ end
157
+ hands_left = @player_arr[n].hand - @player_arr[n].hand_done
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ def blackjack(card_value)
165
+ card_value == 21
166
+ end
167
+
168
+ def check_for_two_card_blackjack
169
+ no_of_players.times do |n|
170
+ @player_arr[n].no_of_hands.times do |h|
171
+ if @player_arr[n].hand[h].hand_value == 21
172
+ puts "Player #{@player_arr[n].player_no}, you got BLACKJACK for hand #{@player_arr[n].hand[h].hand_no}."
173
+ @player_arr[n].hand_done << @player_arr[n].hand[h]
174
+ end
175
+ end
176
+ end
177
+ end
178
+
179
+ def dealer_draws
180
+ dealer.dealer_hand.calc_hand_value
181
+ while dealer.dealer_hand.hand_value < 17
182
+ dealer.dealer_hand.cards_in_hand<<deal_cards[0]
183
+ remove_card_from_deck
184
+ dealer.dealer_hand.calc_hand_value
185
+ end
186
+ puts "Dealer cards: #{dealer.dealer_hand.cards_in_hand}"
187
+ end
188
+
189
+ def compare_with_dealer
190
+ no_of_players.times do |n|
191
+ @player_arr[n].no_of_hands.times do |h|
192
+ if dealer.dealer_hand.hand_value > 21
193
+ if @player_arr[n].hand[h].hand_value <= 21
194
+ puts "Player #{@player_arr[n].player_no}, hand #{@player_arr[n].hand[h].hand_no}: #{@player_arr[n].hand[h].cards_in_hand} - WON"
195
+ @player_arr[n].money_left += @player_arr[n].hand[h].bet
196
+ else
197
+ puts "Player #{@player_arr[n].player_no}, hand #{@player_arr[n].hand[h].hand_no}: #{@player_arr[n].hand[h].cards_in_hand} - Also bust."
198
+ end
199
+ elsif dealer.dealer_hand.hand_value <= 21
200
+ if (@player_arr[n].hand[h].hand_value > dealer.dealer_hand.hand_value) && (@player_arr[n].hand[h].hand_value <=21)
201
+ puts "Player #{@player_arr[n].player_no}, hand #{@player_arr[n].hand[h].hand_no}: #{@player_arr[n].hand[h].cards_in_hand} - WON"
202
+ @player_arr[n].money_left += @player_arr[n].hand[h].bet
203
+ elsif (@player_arr[n].hand[h].hand_value == dealer.dealer_hand.hand_value)
204
+ puts "Player #{@player_arr[n].player_no}, hand #{@player_arr[n].hand[h].hand_no}: #{@player_arr[n].hand[h].cards_in_hand} - EVEN"
205
+ else
206
+ puts "Player #{@player_arr[n].player_no}, hand #{@player_arr[n].hand[h].hand_no}: #{@player_arr[n].hand[h].cards_in_hand} - LOST"
207
+ @player_arr[n].money_left -= @player_arr[n].hand[h].bet
208
+ end
209
+ end
210
+ end
211
+ end
212
+ no_of_players.times do |n|
213
+ puts "Player #{@player_arr[n].player_no} your have #{@player_arr[n].money_left} dollars left."
214
+ end
215
+ end
216
+
217
+ def reshuffle_cards
218
+ deck.make_deck
219
+ @no_of_players.times do |c|
220
+ @player_arr[c].hand = []
221
+ @player_arr[c].hand_done = []
222
+ @player_arr[c].no_of_hands = 0
223
+ end
224
+ dealer.dealer_hand.cards_in_hand = []
225
+ end
226
+ end
@@ -0,0 +1,31 @@
1
+ class Hand
2
+ attr_accessor :hand_no, :cards_in_hand, :bet, :choices, :hand_value
3
+
4
+ def initialize
5
+ @hand_no = hand_no
6
+ @cards_in_hand = []
7
+ @bet = 0
8
+ @choices = []
9
+ @hand_value = 0
10
+ end
11
+
12
+ #Could be improved
13
+ def calc_hand_value
14
+ @hand_value = 0
15
+ cards_in_hand.each_with_index do |c,ind|
16
+ if c != "ace"
17
+ value = Deck.card_values.fetch(c)
18
+ @hand_value += value
19
+ elsif c == "ace" && cards_in_hand[ind -1] == "ace"
20
+ value = Deck.card_values.fetch(c)[0]
21
+ @hand_value += value
22
+ elsif c == "ace"
23
+ value = Deck.card_values.fetch(c)[1]
24
+ @hand_value += value
25
+ end
26
+ end
27
+ if @hand_value > 21 and cards_in_hand.include?("ace")
28
+ @hand_value -= 10
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ class Player
2
+ attr_accessor :hand, :hand_done, :no_of_hands, :player_no, :money_left
3
+
4
+ @@instance_collector = []
5
+
6
+ def initialize(no_of_hands = 1, money_left)
7
+ @hand = []
8
+ @hand_done = []
9
+ @no_of_hands = 0
10
+ @player_no = player_no
11
+ @money_left = money_left
12
+ @@instance_collector << self
13
+ end
14
+
15
+ def self.instance_collector
16
+ @@instance_collector
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Mblackjack
2
+ VERSION = "0.1.1"
3
+ end
data/lib/mblackjack.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'mblackjack/version'
2
+ require 'mblackjack/deck'
3
+ require 'mblackjack/player'
4
+ require 'mblackjack/hand'
5
+ require 'mblackjack/dealer'
6
+ require 'mblackjack/game'
7
+
8
+ module Mblackjack
9
+ class CLI
10
+ def self.run
11
+ game = Game.new
12
+ game.record_no_of_players
13
+ game.start
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mblackjack/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mblackjack"
8
+ spec.version = Mblackjack::VERSION
9
+ spec.authors = ["bellvat"]
10
+ spec.email = ["msirathanarun@gmail.com"]
11
+
12
+ spec.summary = "Blackjack game"
13
+ spec.description = "Blackjack game"
14
+ spec.homepage = "https://github.com/bellvat"
15
+ spec.license = "MIT"
16
+
17
+
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "bin"
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mblackjack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - bellvat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Blackjack game
56
+ email:
57
+ - msirathanarun@gmail.com
58
+ executables:
59
+ - console
60
+ - m_blackjack
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/m_blackjack
74
+ - bin/setup
75
+ - lib/mblackjack.rb
76
+ - lib/mblackjack/dealer.rb
77
+ - lib/mblackjack/deck.rb
78
+ - lib/mblackjack/game.rb
79
+ - lib/mblackjack/hand.rb
80
+ - lib/mblackjack/player.rb
81
+ - lib/mblackjack/version.rb
82
+ - mblackjack.gemspec
83
+ homepage: https://github.com/bellvat
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.11
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Blackjack game
107
+ test_files: []