black_jack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 221b3a47928f44b705f1ba3f1425a01d979a5c56
4
+ data.tar.gz: d63aa0d8926c337ec805c6e1d7d6b44ad470486f
5
+ SHA512:
6
+ metadata.gz: 0db01a561f519b39721da8497492893b6f41f31648d72eb0bf300b0cc839420de73660cb2d78b363d44bb38789a69e82147fb614f2eb835a524753e641b0384c
7
+ data.tar.gz: f60488644975da7522e6c63d8cbd05e9fb027b07fbf64eb7f1732290f066929d11805bec19bf91b954ea1902f0a7dfde926ca95af72768853f381aa2353163b0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blackjack.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Blackjack
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/blackjack`. 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 'blackjack'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install blackjack
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 `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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/blackjack/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blackjack"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
data/blackjack.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blackjack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "black_jack"
8
+ spec.version = Blackjack::VERSION
9
+ spec.authors = ["Benjamin Pearce"]
10
+ spec.email = ["hawk.git@bcpearce.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
14
+ end
15
+
16
+ spec.summary = "A gem containing Blackjack libraries"
17
+ spec.homepage = "https://github.com/bcpearce/blackjack"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,42 @@
1
+
2
+ class Card
3
+
4
+ SUITS = [:spade, :club, :heart, :diamond]
5
+ ICONS = { spade:"\u2660", club:"\u2663", heart:"\u2665", diamond:"\u2666" }
6
+ VIEW = { 1 => "A", 11 => "J", 12 => "Q", 13 => "K", 14 => "A" }
7
+
8
+ def initialize(suit, rank)
9
+ @suit = suit
10
+ @rank = rank
11
+ @view = self.view()
12
+ end
13
+
14
+ def suit
15
+ @suit
16
+ end
17
+
18
+ def rank
19
+ @rank
20
+ end
21
+
22
+ def value(ace=:high)
23
+ val =
24
+ if @rank == 14 or @rank == 1
25
+ ace == :high ? 11 : 1
26
+ elsif @rank >= 11 and @rank <= 13
27
+ 10
28
+ else
29
+ @rank
30
+ end
31
+ end
32
+
33
+ def view()
34
+ view = VIEW[@rank]
35
+ view ||= @rank
36
+ end
37
+
38
+ def to_s()
39
+ "#{ICONS[@suit]}#{@view}"
40
+ end
41
+
42
+ end
@@ -0,0 +1,43 @@
1
+ require_relative "card"
2
+
3
+ class Deck
4
+
5
+ def initialize(shuffle=true)
6
+ @cards =
7
+ (2..14).map do |rank|
8
+ Card::SUITS.map do |suit|
9
+ Card.new(suit, rank)
10
+ end
11
+ end
12
+ @cards = @cards.flatten()
13
+ self.shuffle! if shuffle
14
+ end
15
+
16
+ def cards
17
+ @cards
18
+ end
19
+
20
+ def shuffle
21
+ @cards.shuffle
22
+ end
23
+
24
+ def shuffle!
25
+ @cards.shuffle!
26
+ end
27
+
28
+ def to_s
29
+ @cards.map(&:to_s).join(',')
30
+ end
31
+
32
+ def draw
33
+ @cards.pop
34
+ end
35
+
36
+ def cards_left
37
+ @cards.length
38
+ end
39
+
40
+ alias size cards_left
41
+ alias length cards_left
42
+ end
43
+
@@ -0,0 +1,56 @@
1
+ require_relative "deck"
2
+
3
+ class Player
4
+
5
+ def initialize
6
+ @cards = []
7
+ end
8
+
9
+ def hit(deck)
10
+ @cards.push(deck.draw)
11
+ end
12
+
13
+ def deal(deck)
14
+ 2.times { self.hit(deck) }
15
+ end
16
+
17
+ def card_sum(ace=:high)
18
+ sum = 0
19
+ @cards.each { |c| sum += c.value(ace) }
20
+ sum
21
+ end
22
+
23
+ #returns a best-possible score invariable of aces
24
+ def score
25
+ score = self.card_sum(ace=:high)
26
+ self.send(:aces).times do
27
+ score -= 10
28
+ return score if score <= 21
29
+ end
30
+ return score
31
+ end
32
+
33
+ def hit_or_stay?
34
+ if self.card_sum(:high) < 11 or self.card_sum(:low) < 11
35
+ return true
36
+ end
37
+ return false
38
+ end
39
+
40
+ def cards
41
+ @cards
42
+ end
43
+
44
+ # for debug
45
+ def cards=(new_hand)
46
+ @cards = new_hand
47
+ end
48
+
49
+ private
50
+
51
+ def aces
52
+ @cards.count { |x| x.value(:low) == 1 }
53
+ end
54
+
55
+ end
56
+
@@ -0,0 +1,12 @@
1
+ require_relative 'deck'
2
+
3
+ class Shoe < Deck
4
+
5
+ def initialize(decks=4, shuffle=true)
6
+ @cards = []
7
+ decks.times { @cards = @cards + Deck.new().cards }
8
+ self.shuffle! if shuffle
9
+ end
10
+
11
+ end
12
+
@@ -0,0 +1,3 @@
1
+ module Blackjack
2
+ VERSION = "0.1.0"
3
+ end
data/lib/blackjack.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "blackjack/version"
2
+ require "blackjack/shoe"
3
+ require "blackjack/player"
4
+
5
+ module Blackjack
6
+ # Your code goes here...
7
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: black_jack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Pearce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-05 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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
+ description:
42
+ email:
43
+ - hawk.git@bcpearce.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - Gemfile
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - blackjack-0.1.0.gem
56
+ - blackjack.gemspec
57
+ - lib/blackjack.rb
58
+ - lib/blackjack/card.rb
59
+ - lib/blackjack/deck.rb
60
+ - lib/blackjack/player.rb
61
+ - lib/blackjack/shoe.rb
62
+ - lib/blackjack/version.rb
63
+ homepage: https://github.com/bcpearce/blackjack
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A gem containing Blackjack libraries
88
+ test_files: []