shithead 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43a4294cda8e6bfced09f52023f6fe494480b625
4
+ data.tar.gz: 1a7cb6d44b951cd4486e7dd07c44273550b9e357
5
+ SHA512:
6
+ metadata.gz: 4286b5a1860d8d421767967677edb1cf994973fca6106dc7d15a3e54dc519515582981f8af93f3658405766dce71fa3dfcb2af76213a55d9dbc8580806701dfd
7
+ data.tar.gz: 999c647c7c2ea7064e67e7636ab585e1e2a0b07d0045afcc40d071bb3fe42b1dd860498467fb3693926bb9dc9f80f9e395ced856e76ca89b0430f56b665f458e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at pat@freelancing-gods.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in shithead.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Pat Allan
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.
@@ -0,0 +1,25 @@
1
+ # Shithead
2
+
3
+ A card game I learnt a long time ago. This gem encapsulates a lot of the game logic, and plays games automatically for you with very basic AI. There's not yet a way to play the games yourself in an interactive fashion.
4
+
5
+ ## Installation
6
+
7
+ $ gem install shithead
8
+
9
+ ## Usage
10
+
11
+ Use the command-line tool to see a game played between as many opponents as you care to name. If you have 4 opponents, two decks will be combined (else the games are a little short!).
12
+
13
+ $ shithead Clarke Dawe
14
+
15
+ ## Contributing
16
+
17
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pat/shithead. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
18
+
19
+ ## License
20
+
21
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
22
+
23
+ ## Code of Conduct
24
+
25
+ Everyone interacting in the Shithead project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pat/shithead/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shithead"
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__)
@@ -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,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "shithead"
4
+
5
+ players = ARGV.collect { |name| Shithead::Player.new name }
6
+
7
+ deck = Shithead::Deck.new players.length
8
+ stack = Shithead::Stack.new
9
+ discards = Shithead::Stack.new
10
+
11
+ Shithead::Dealer.new(deck, players).call
12
+
13
+ begin
14
+ loop do
15
+ puts "Status:"
16
+ puts " in deck: #{deck.card_count}"
17
+ puts " in stack: #{stack.size}"
18
+ puts " in discards: #{discards.size}"
19
+ puts ""
20
+
21
+ players.each do |player|
22
+ puts "#{player.name}:"
23
+ puts " in hand: #{player.hand.size} (#{player.hand.to_s})"
24
+ puts " visible: #{player.visible.to_s}"
25
+ puts " hidden: #{player.hidden_count}"
26
+ puts ""
27
+
28
+ puts "Top: #{stack.to_s}"
29
+ puts ""
30
+
31
+ Shithead::Turn.call deck, stack, discards, player
32
+ end
33
+
34
+ sleep 2
35
+ puts "\e[H\e[2J"
36
+ end
37
+ rescue Shithead::GameOverError => error
38
+ puts error.message
39
+ end
@@ -0,0 +1,19 @@
1
+ module Shithead
2
+ module AI
3
+ end
4
+
5
+ Error = Class.new StandardError
6
+ GameOverError = Class.new Shithead::Error
7
+ end
8
+
9
+ require "shithead/card"
10
+ require "shithead/dealer"
11
+ require "shithead/deck"
12
+ require "shithead/player"
13
+ require "shithead/set"
14
+ require "shithead/stack"
15
+ require "shithead/sorted_stack"
16
+ require "shithead/turn"
17
+
18
+ require "shithead/ai/deal_in"
19
+ require "shithead/ai/offer"
@@ -0,0 +1,28 @@
1
+ class Shithead::AI::DealIn
2
+ def self.call(player)
3
+ new(player).call
4
+ end
5
+
6
+ def initialize(player)
7
+ @player = player
8
+ end
9
+
10
+ def call
11
+ return if player.hand.empty?
12
+
13
+ 3.times { place ordered.shift }
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :player
19
+
20
+ def ordered
21
+ @ordered ||= player.hand.cards.sort_by(&:rank)
22
+ end
23
+
24
+ def place(card)
25
+ player.visible.add card
26
+ player.hand.delete card
27
+ end
28
+ end
@@ -0,0 +1,52 @@
1
+ class Shithead::AI::Offer
2
+ EMPTY_SET = Shithead::Set.new
3
+
4
+ def self.call(player, top)
5
+ new(player, top).call
6
+ end
7
+
8
+ def initialize(player, top)
9
+ @player = player
10
+ @top = top
11
+ end
12
+
13
+ def call
14
+ draw_from_hidden if empty_hand?
15
+ return EMPTY_SET if lowest_possible.nil?
16
+
17
+ available.take lowest_possible
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :player, :top
23
+
24
+ def available
25
+ @available ||= player.hand.empty? ? player.visible : player.hand
26
+ end
27
+
28
+ def draw_from_hidden
29
+ player.hidden.shuffle!
30
+ player.add player.hidden.shift
31
+ end
32
+
33
+ def empty_hand?
34
+ player.hand.empty? && player.visible.empty?
35
+ end
36
+
37
+ def play_lowest?
38
+ top.nil? || top.value == "2"
39
+ end
40
+
41
+ def lowest_possible
42
+ @lowest_possible ||= if play_lowest?
43
+ ordered.first
44
+ else
45
+ ordered.detect { |set| set.rank <= top.rank }
46
+ end
47
+ end
48
+
49
+ def ordered
50
+ @ordered ||= available.sets.sort_by(&:rank).reverse
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ class Shithead::Card
2
+ RANKS = {
3
+ "10" => 1,
4
+ "A" => 2,
5
+ "2" => 3,
6
+ "K" => 4,
7
+ "Q" => 5,
8
+ "J" => 6,
9
+ "9" => 8,
10
+ "8" => 9,
11
+ "7" => 10,
12
+ "6" => 11,
13
+ "5" => 12,
14
+ "4" => 13,
15
+ "3" => 14
16
+ }
17
+ SUITES = {
18
+ "Diamonds" => "♦️",
19
+ "Hearts" => "♥️",
20
+ "Spades" => "♠️",
21
+ "Clubs" => "♣️"
22
+ }
23
+
24
+ attr_reader :value, :suite
25
+
26
+ def initialize(suite, value)
27
+ @suite = suite
28
+ @value = value
29
+ end
30
+
31
+ def rank
32
+ RANKS[value]
33
+ end
34
+
35
+ def to_s
36
+ "#{SUITES[suite]}#{value}"
37
+ end
38
+ end
@@ -0,0 +1,34 @@
1
+ class Shithead::Dealer
2
+ def initialize(deck, players)
3
+ @deck = deck
4
+ @players = players
5
+ end
6
+
7
+ def call
8
+ hidden_cards = players.collect { [] }
9
+ hand_cards = players.collect { [] }
10
+
11
+ 3.times do
12
+ players.length.times do |index|
13
+ hidden_cards[index] << deck.draw
14
+ end
15
+ end
16
+
17
+ 6.times do
18
+ players.length.times do |index|
19
+ hand_cards[index] << deck.draw
20
+ end
21
+ end
22
+
23
+ players.each_with_index do |player, index|
24
+ player.hidden.replace hidden_cards[index]
25
+ hand_cards[index].each { |card| player.hand.add card }
26
+
27
+ Shithead::AI::DealIn.call player
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :deck, :players
34
+ end
@@ -0,0 +1,35 @@
1
+ class Shithead::Deck
2
+ SUITES = %w[ Clubs Diamonds Hearts Spades ].freeze
3
+ VALUES = %w[ A 2 3 4 5 6 7 8 9 10 J Q K ].freeze
4
+ PLAYERS_PER_DECK_RATIO = 3.5
5
+
6
+ def initialize(player_count = 1)
7
+ @cards = []
8
+
9
+ (player_count / PLAYERS_PER_DECK_RATIO).ceil.times do
10
+ SUITES.each do |suite|
11
+ VALUES.each do |value|
12
+ @cards << Shithead::Card.new(suite, value)
13
+ end
14
+ end
15
+ end
16
+
17
+ @cards.shuffle!
18
+ end
19
+
20
+ def card_count
21
+ cards.length
22
+ end
23
+
24
+ def draw
25
+ cards.shift
26
+ end
27
+
28
+ def empty?
29
+ cards.empty?
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :cards
35
+ end
@@ -0,0 +1,26 @@
1
+ class Shithead::Player
2
+ attr_reader :name, :hand, :visible, :hidden
3
+
4
+ def initialize(name)
5
+ @name = name
6
+ @hidden = []
7
+ @visible = Shithead::SortedStack.new
8
+ @hand = Shithead::SortedStack.new
9
+ end
10
+
11
+ def add(card)
12
+ hand.add card
13
+ end
14
+
15
+ def clear?
16
+ hidden.empty? && visible.empty? && hand.empty?
17
+ end
18
+
19
+ def hand_count
20
+ hand.length
21
+ end
22
+
23
+ def hidden_count
24
+ hidden.length
25
+ end
26
+ end
@@ -0,0 +1,41 @@
1
+ class Shithead::Set
2
+ attr_reader :value
3
+ attr_reader :cards
4
+
5
+ def initialize(cards = [])
6
+ @cards = cards
7
+ end
8
+
9
+ def add(card)
10
+ cards << card
11
+ end
12
+
13
+ def delete(card)
14
+ cards.delete card
15
+ end
16
+
17
+ def empty?
18
+ cards.empty?
19
+ end
20
+
21
+ def rank
22
+ cards.first && cards.first.rank
23
+ end
24
+
25
+ def release_to(object)
26
+ cards.each { |card| object.add card }
27
+ cards.clear
28
+ end
29
+
30
+ def size
31
+ cards.length
32
+ end
33
+
34
+ def to_s
35
+ cards.collect(&:to_s).join(" ")
36
+ end
37
+
38
+ def value
39
+ cards.first && cards.first.value
40
+ end
41
+ end
@@ -0,0 +1,32 @@
1
+ class Shithead::SortedStack < Shithead::Stack
2
+ def add(card)
3
+ if set_for(card.value)
4
+ set_for(card.value).add card
5
+ else
6
+ sets << Shithead::Set.new([card])
7
+ sets.replace sets.sort_by(&:rank).reverse
8
+ end
9
+ end
10
+
11
+ def delete(card)
12
+ set = set_for(card.value)
13
+ set.delete card
14
+
15
+ sets.delete set if set.empty?
16
+ end
17
+
18
+ def take(set)
19
+ sets.delete set
20
+ set
21
+ end
22
+
23
+ def to_s
24
+ sets.collect(&:to_s).join(" ")
25
+ end
26
+
27
+ private
28
+
29
+ def set_for(value)
30
+ sets.detect { |set| set.value == value }
31
+ end
32
+ end
@@ -0,0 +1,50 @@
1
+ class Shithead::Stack
2
+ attr_reader :sets
3
+
4
+ def initialize
5
+ @sets = []
6
+ end
7
+
8
+ def add(card)
9
+ if empty? || top.value != card.value
10
+ sets.unshift Shithead::Set.new([card])
11
+ else
12
+ top.add card
13
+ end
14
+ end
15
+
16
+ def cards
17
+ sets.collect(&:cards).flatten
18
+ end
19
+
20
+ def clearable?
21
+ return false if empty?
22
+
23
+ top.value == "10" || top.size >= 4
24
+ end
25
+
26
+ def delete(card)
27
+ sets.each { |set| set.delete card }
28
+ end
29
+
30
+ def empty?
31
+ sets.empty?
32
+ end
33
+
34
+ def release_to(stack)
35
+ sets.each { |set| set.release_to stack }
36
+ sets.clear
37
+ end
38
+
39
+ def size
40
+ sets.sum &:size
41
+ end
42
+
43
+ def top
44
+ sets.first
45
+ end
46
+
47
+ def to_s
48
+ sets.first && sets.first.to_s
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ class Shithead::Turn
2
+ def self.call(deck, stack, discards, player)
3
+ new(deck, stack, discards, player).call
4
+ end
5
+
6
+ def initialize(deck, stack, discards, player)
7
+ @deck = deck
8
+ @stack = stack
9
+ @player = player
10
+ @discards = discards
11
+ @offer = Shithead::AI::Offer.call player, stack.top
12
+ end
13
+
14
+ def call
15
+ if offer.empty?
16
+ puts "Picking up", ""
17
+ stack.release_to player
18
+ else
19
+ puts "Playing: #{offer.to_s}", ""
20
+ offer.release_to stack
21
+
22
+ while player.hand.size < 3 && !deck.empty?
23
+ player.add deck.draw
24
+ end
25
+ end
26
+
27
+ if player.clear?
28
+ raise Shithead::GameOverError, "Game over, #{player.name} has won."
29
+ end
30
+
31
+ if stack.clearable?
32
+ puts "> Clearing", ""
33
+
34
+ stack.release_to discards
35
+ Shithead::Turn.call deck, stack, discards, player
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :deck, :stack, :discards, :player, :offer
42
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "shithead"
4
+ spec.version = "0.0.1"
5
+ spec.authors = ["Pat Allan"]
6
+ spec.email = ["pat@freelancing-gods.com"]
7
+
8
+ spec.summary = %q{A card game}
9
+ spec.homepage = "https://github.com/pat/shithead"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
13
+ f.match(%r{^(test|spec|features)/})
14
+ end
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.15"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shithead
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-20 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
+ description:
42
+ email:
43
+ - pat@freelancing-gods.com
44
+ executables:
45
+ - shithead
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - exe/shithead
58
+ - lib/shithead.rb
59
+ - lib/shithead/ai/deal_in.rb
60
+ - lib/shithead/ai/offer.rb
61
+ - lib/shithead/card.rb
62
+ - lib/shithead/dealer.rb
63
+ - lib/shithead/deck.rb
64
+ - lib/shithead/player.rb
65
+ - lib/shithead/set.rb
66
+ - lib/shithead/sorted_stack.rb
67
+ - lib/shithead/stack.rb
68
+ - lib/shithead/turn.rb
69
+ - shithead.gemspec
70
+ homepage: https://github.com/pat/shithead
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.11
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: A card game
94
+ test_files: []