brigitte 0.1.3

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
+ SHA256:
3
+ metadata.gz: 4266ff58a7441dbcebac688a4b9b56e1dc53736a1fba5fd22186a818e2b9db82
4
+ data.tar.gz: f6155c8f89ad5be629079392e66a26c9ab4dec2ff55fadeed17a06f6337501d1
5
+ SHA512:
6
+ metadata.gz: c2401ceb525bff993d2f8a2e36f4081ace09555436d6600b89041b64122bd2507ae4abbc402448a9661c940fb264e685b0d09deb69271be93c2093a802730d66
7
+ data.tar.gz: 634d235a8f56207f9fbbb4d0188a85e288cb4f3f5f024c00aa98ef9248d32b265f17d2905ddbbd3df158ceb21d315214c1363ab6d892b1e200748bbc3a80f388
@@ -0,0 +1,13 @@
1
+ # EditorConfig helps developers define and maintain consistent
2
+ # coding styles between different editors and IDEs
3
+ # editorconfig.org
4
+
5
+ root = true
6
+
7
+ [*]
8
+ indent_style = space
9
+ indent_size = 2
10
+ end_of_line = lf
11
+ charset = utf-8
12
+ trim_trailing_whitespace = true
13
+ insert_final_newline = true
@@ -0,0 +1,29 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*'
7
+
8
+ jobs:
9
+ build:
10
+ name: Build + Publish
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby
16
+ uses: actions/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.7.2
19
+
20
+ - name: Publish to RubyGems
21
+ env:
22
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
23
+ run: |
24
+ mkdir -p $HOME/.gem
25
+ touch $HOME/.gem/credentials
26
+ chmod 0600 $HOME/.gem/credentials
27
+ echo "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}" > $HOME/.gem/credentials
28
+ gem build brigitte.gemspec
29
+ gem push brigitte-*.gem
@@ -0,0 +1,27 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ develop, master ]
6
+ pull_request:
7
+ branches: [ develop, master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby
16
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
17
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
18
+ # uses: ruby/setup-ruby@v1
19
+ uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: 2.7.2
22
+ - name: Install dependencies
23
+ run: bundle install
24
+ - name: Run tests
25
+ run: bundle exec rspec --force-color
26
+ - name: Run rubocop
27
+ run: bundle exec rubocop
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # IJ IDE
14
+ .idea
15
+
16
+ # local ENVS
17
+ exports
18
+
19
+ # Coverage logs
20
+ coverage
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,48 @@
1
+ ---
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ TargetRubyVersion: 2.7
6
+ Exclude:
7
+ - 'db/**/*'
8
+ - 'node_modules/**/*'
9
+ - 'bin/*'
10
+ Style/Documentation:
11
+ Enabled: true
12
+ Metrics/ModuleLength:
13
+ Enabled: true
14
+ Metrics/ClassLength:
15
+ Enabled: false
16
+ Metrics/MethodLength:
17
+ Enabled: true
18
+ Metrics/BlockLength:
19
+ Enabled: true
20
+ Exclude:
21
+ - 'config/initializers/inflections.rb'
22
+ - 'config/routes.rb'
23
+ - 'lib/tasks/**/*'
24
+ - 'Rakefile'
25
+ - 'spec/**/*'
26
+ Layout/LineLength:
27
+ Enabled: true
28
+ Max: 80
29
+ Exclude:
30
+ - "**/*_spec.rb"
31
+ - "**/*_spec.rb"
32
+ - "brigitte.gemspec"
33
+ Metrics/AbcSize:
34
+ Enabled: true
35
+ Metrics/PerceivedComplexity:
36
+ Enabled: true
37
+ Metrics/CyclomaticComplexity:
38
+ Enabled: true
39
+ Lint/RaiseException:
40
+ Enabled: true
41
+ Lint/StructNewOverride:
42
+ Enabled: true
43
+ Style/HashEachMethods:
44
+ Enabled: true
45
+ Style/HashTransformKeys:
46
+ Enabled: true
47
+ Style/HashTransformValues:
48
+ Enabled: true
@@ -0,0 +1,29 @@
1
+ # Changelog
2
+
3
+ ## [0.1.3] 2020-11-26
4
+
5
+ ### Changed
6
+
7
+ - gem-push flow cleanup and tryout
8
+
9
+ ## [0.1.2] 2020-11-26
10
+
11
+ ### Changed
12
+
13
+ - gem-push flow fix
14
+
15
+ ## [0.1.1] 2020-11-26
16
+
17
+ ### Changed
18
+
19
+ - gem-push flow
20
+
21
+ ## [0.1.0] 2020-11-26
22
+
23
+ Initial release
24
+
25
+ ### Added
26
+
27
+ - full logic Brigitte
28
+ - rubygems support
29
+ - serialization
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in brigitte.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+
10
+ group :test do
11
+ gem 'rspec', '~> 3.0'
12
+ gem 'rubocop', '~> 1.4', require: false
13
+ gem 'simplecov', '~> 0.19', require: false
14
+ end
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ brigitte (0.1.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ diff-lcs (1.4.4)
11
+ docile (1.3.2)
12
+ parallel (1.20.1)
13
+ parser (2.7.2.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.0.0)
16
+ rake (12.3.3)
17
+ regexp_parser (1.8.2)
18
+ rexml (3.2.4)
19
+ rspec (3.9.0)
20
+ rspec-core (~> 3.9.0)
21
+ rspec-expectations (~> 3.9.0)
22
+ rspec-mocks (~> 3.9.0)
23
+ rspec-core (3.9.2)
24
+ rspec-support (~> 3.9.3)
25
+ rspec-expectations (3.9.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-mocks (3.9.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-support (3.9.3)
32
+ rubocop (1.4.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.7.1.5)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8)
37
+ rexml
38
+ rubocop-ast (>= 1.1.1)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 2.0)
41
+ rubocop-ast (1.1.1)
42
+ parser (>= 2.7.1.5)
43
+ ruby-progressbar (1.10.1)
44
+ simplecov (0.19.1)
45
+ docile (~> 1.1)
46
+ simplecov-html (~> 0.11)
47
+ simplecov-html (0.12.3)
48
+ unicode-display_width (1.7.0)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ brigitte!
55
+ rake (~> 12.0)
56
+ rspec (~> 3.0)
57
+ rubocop (~> 1.4)
58
+ simplecov (~> 0.19)
59
+
60
+ BUNDLED WITH
61
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 youszef
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,40 @@
1
+ # Brigitte
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/brigitte`. 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 'brigitte'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install brigitte
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/youszef/brigitte.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "brigitte"
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,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/brigitte/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'brigitte'
7
+ spec.version = Brigitte::VERSION
8
+ spec.authors = ['youszef']
9
+ spec.email = ['zouhariy@gmail.com']
10
+
11
+ spec.summary = 'Card game based on Shithead'
12
+ spec.description = "Card game where player need to lose all of it's cards"
13
+ spec.homepage = 'https://github.com/youszef/brigitte'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'brigitte/version'
4
+ require 'brigitte/game'
5
+
6
+ module Brigitte
7
+ class Error < StandardError; end
8
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Brigitte
6
+ #
7
+ # A Card has a value and sign defined separately.
8
+ #
9
+ # Note:
10
+ # Compare cards' value with their +weight+.
11
+ # Cards are only equal if their id's are the same.
12
+ class Card
13
+ include Comparable
14
+ attr_reader :id, :value, :sign
15
+
16
+ def initialize(value, sign, id = nil)
17
+ @id = id || SecureRandom.uuid
18
+ @value = value
19
+ @sign = sign
20
+ end
21
+
22
+ def ==(other)
23
+ id == other.id
24
+ end
25
+
26
+ def to_s
27
+ value + sign
28
+ end
29
+
30
+ def weight
31
+ return 11 if @value == 'J'
32
+ return 12 if @value == 'Q'
33
+ return 13 if @value == 'K'
34
+ return 14 if @value == 'A'
35
+
36
+ value.to_i
37
+ end
38
+
39
+ def to_h
40
+ {
41
+ id: id,
42
+ value: value,
43
+ sign: sign
44
+ }
45
+ end
46
+
47
+ def self.from_h(card_hash)
48
+ return if card_hash.empty?
49
+
50
+ new(
51
+ card_hash[:value],
52
+ card_hash[:sign],
53
+ card_hash[:id]
54
+ )
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brigitte
4
+ module Commands
5
+ module Pile
6
+ #
7
+ # Command that evaluates and adds cards on pile
8
+ class AddCards
9
+ class << self
10
+ def process(player, cards, pile, removed_cards = [])
11
+ return false unless valid?(player, cards, pile)
12
+
13
+ pile.push(*cards.dup.map { |c| player.hand.delete(c) })
14
+ removed_cards.push(*pile.pop(pile.count)) if clear_pile?(pile)
15
+
16
+ true
17
+ end
18
+
19
+ def valid?(player, cards, pile)
20
+ # player has cards
21
+ return false unless (cards - player.hand).empty?
22
+ # all cards are equal
23
+ return false unless cards.uniq(&:weight).count == 1
24
+ return true if pile.empty?
25
+ # wild cards
26
+ return true if [2, 10].include?(cards.first.weight)
27
+
28
+ can_put_on_card?(cards.first, pile.last)
29
+ end
30
+
31
+ private
32
+
33
+ def can_put_on_card?(top_card, down_card)
34
+ operator = down_card.weight == 7 ? '<=' : '>='
35
+ top_card.weight.send(operator, down_card.weight)
36
+ end
37
+
38
+ def clear_pile?(pile)
39
+ return true if pile.last.weight == 10
40
+ return false unless pile.count >= 4
41
+
42
+ pile.last(4).uniq(&:weight).count == 1
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brigitte
4
+ #
5
+ # A Deck generates and shuffles all cards except Joker.
6
+ # Joker is not used in Brigitte
7
+ class Deck
8
+ attr_accessor :cards
9
+
10
+ SIGNS = %w[♣ ♦ ♥ ♠].freeze
11
+ PREFIX = %w[2 3 4 5 6 7 8 9 10 J Q K A].freeze
12
+
13
+ def initialize
14
+ @cards = PREFIX.map do |prefix|
15
+ SIGNS.map { |sign| Card.new(prefix, sign) }
16
+ end.flatten.shuffle
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'player'
4
+ require_relative 'card'
5
+ require_relative 'deck'
6
+ require_relative 'commands/pile'
7
+
8
+ module Brigitte
9
+ #
10
+ # A Game has maximum 4 players (+active_players+)
11
+ # and can be started with +play+ method when all players are ready.
12
+ class Game
13
+ attr_accessor :current_player, :game_over
14
+ attr_reader :active_players, :cards, :pile, :removed_cards, :winners
15
+
16
+ def initialize
17
+ @active_players = []
18
+ @cards = []
19
+ @pile = []
20
+ @removed_cards = []
21
+ @winners = []
22
+ @game_over = false
23
+ end
24
+
25
+ #
26
+ # Starts the game with the provided +players+.
27
+ #
28
+ # Returns this Game instance.
29
+ #
30
+ # ==== Arguments
31
+ # +players+ - (Array) containing player names. Default strings of names.
32
+ #
33
+ # ==== Optional arguments
34
+ # When +players+ is an array of hashes.
35
+ #
36
+ # +player_name_key+: - The key of name in player hash.
37
+ #
38
+ # +player_id_key+: - The key of id in player hash.
39
+ #
40
+ # ===== Examples
41
+ # start_new_game(['Bell', 'Biv', 'Devoe'])
42
+ # or
43
+ # start_new_game(
44
+ # [{ name: 'Bell', id: 1 },
45
+ # { name: 'Biv', id: 2 },
46
+ # { name: 'Devoe', id: 3 }],
47
+ # player_name_key: :name,
48
+ # player_id_key: :id
49
+ # )
50
+ def start_new_game(players, player_name_key: nil, player_id_key: nil)
51
+ if player_name_key && player_id_key
52
+ players.each do |p|
53
+ @active_players << Player.new(p[player_name_key], p[player_id_key])
54
+ end
55
+ else
56
+ players.each { |pn| @active_players << Player.new(pn) }
57
+ end
58
+ @cards = Deck.new.cards
59
+ deal_cards
60
+
61
+ self
62
+ end
63
+
64
+ def play
65
+ return false unless @active_players.all?(&:ready)
66
+ return @current_player if @current_player
67
+
68
+ @current_player = @active_players.min do |player1, player2|
69
+ player1.hand.map(&:weight).min <=> player2.hand.map(&:weight).min
70
+ end
71
+ end
72
+
73
+ def throw_cards(player, *thrown_cards)
74
+ return false unless player == @current_player
75
+ return false unless Commands::Pile::AddCards.process(
76
+ player, thrown_cards, @pile, @removed_cards
77
+ )
78
+
79
+ take_cards(player)
80
+ take_visible_cards(player)
81
+ player_won(player)
82
+ select_next_player
83
+
84
+ true
85
+ end
86
+
87
+ def take_cards_from_pot(player)
88
+ return false unless player == @current_player
89
+
90
+ player.hand.push(*@pile.pop(@pile.count))
91
+ player.sort_hand!
92
+
93
+ select_next_player(force: true) unless @game_over
94
+ end
95
+
96
+ def take_hidden_card(player, hidden_card_index)
97
+ return false if player != @current_player
98
+ return false if @cards.any?
99
+ return false if player.visible_cards.any?
100
+ return false if player.hand.any?
101
+
102
+ player.pull_hidden_card(hidden_card_index)
103
+ end
104
+
105
+ def to_h
106
+ {
107
+ active_players: active_players.map(&:to_h),
108
+ cards: cards.map(&:to_h),
109
+ pile: pile.map(&:to_h),
110
+ removed_cards: removed_cards.map(&:to_h),
111
+ current_player: current_player.to_h,
112
+ winners: winners.map(&:to_h),
113
+ game_over: game_over
114
+ }
115
+ end
116
+
117
+ def self.from_h(hash) # rubocop:disable Metrics/AbcSize
118
+ game = new
119
+ hash[:active_players].each { |h| game.active_players << Player.from_h(h) }
120
+ hash[:cards].each { |h| game.cards << Card.from_h(h) }
121
+ hash[:pile].each { |h| game.pile << Card.from_h(h) }
122
+ hash[:removed_cards].each { |h| game.removed_cards << Card.from_h(h) }
123
+ game.current_player = Player.from_h(hash[:current_player])
124
+ hash[:winners].each { |h| game.winners << Player.from_h(h) }
125
+ game.game_over = hash[:game_over]
126
+
127
+ game
128
+ end
129
+
130
+ private
131
+
132
+ def deal_cards
133
+ @active_players.each do |player|
134
+ 3.times { player.hidden_cards << @cards.pop }
135
+ 3.times { player.visible_cards << @cards.pop }
136
+ 3.times { player.hand << @cards.pop }
137
+ end
138
+ end
139
+
140
+ def take_cards(player)
141
+ return if @cards.empty?
142
+ return if player.hand.count >= 3
143
+
144
+ player.hand.push(*@cards.pop(3 - player.hand.count))
145
+ player.sort_hand!
146
+ end
147
+
148
+ def take_visible_cards(player)
149
+ return if @cards.any?
150
+ return if player.hand.any?
151
+ return if player.visible_cards.empty?
152
+
153
+ player.hand.push(*player.visible_cards.pop(player.visible_cards.count))
154
+ player.sort_hand!
155
+ end
156
+
157
+ def select_next_player(force: false)
158
+ return if @game_over
159
+ return if !force && player_can_throw_again?(@current_player)
160
+
161
+ current_player_index = @active_players.index(@current_player)
162
+ next_player_index = (current_player_index + 1) % @active_players.count
163
+
164
+ @current_player = @active_players[next_player_index]
165
+ select_next_player if @winners.include?(@current_player)
166
+ end
167
+
168
+ def player_won(player)
169
+ return if @winners.include? player
170
+ return if player.hidden_cards.compact.any?
171
+ return if player.hand.any?
172
+
173
+ @winners << player
174
+ verify_and_set_game_over
175
+ end
176
+
177
+ def verify_and_set_game_over
178
+ remaining_players = @active_players.reject do |active_player|
179
+ @winners.include? active_player
180
+ end
181
+ return if remaining_players.count > 1
182
+
183
+ @winners << remaining_players.first if remaining_players.first
184
+ @game_over = true
185
+ end
186
+
187
+ def player_can_throw_again?(player)
188
+ return false if @winners.include?(player)
189
+
190
+ @pile.empty?
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Brigitte
6
+ #
7
+ # A Player in Brigitte has a:
8
+ # +hand+ where from player can only throw cards from
9
+ # +visible_cards+ where from player can draw from if hands are empty
10
+ # +hidden_cards+ cards that are face down where from player can take only one
11
+ # if all cards are played
12
+ #
13
+ # A player is ready if cards are swapped between it's +hand+
14
+ # and +visible_cards+
15
+ class Player
16
+ attr_accessor :name, :hand, :hidden_cards, :visible_cards, :ready
17
+ attr_reader :id
18
+
19
+ def initialize(name, id = nil)
20
+ @id = id || SecureRandom.uuid
21
+ @name = name
22
+ @hand = []
23
+ @hidden_cards = []
24
+ @visible_cards = []
25
+ @ready = false
26
+
27
+ yield self if block_given?
28
+ end
29
+
30
+ def ready!
31
+ sort_hand!
32
+ @ready = true
33
+ end
34
+
35
+ def ready?
36
+ @ready
37
+ end
38
+
39
+ def ==(other)
40
+ id == other&.id
41
+ end
42
+
43
+ def swap(hand_card, visible_card)
44
+ return if @ready
45
+
46
+ hand_card_index = hand.find_index(hand_card)
47
+ visible_card_index = visible_cards.find_index(visible_card)
48
+ return unless hand_card_index
49
+ return unless visible_card_index
50
+
51
+ visible_cards[visible_card_index] = hand_card
52
+ hand[hand_card_index] = visible_card
53
+ end
54
+
55
+ def pull_hidden_card(index)
56
+ return false if hand.any?
57
+ return false if visible_cards.any?
58
+
59
+ hidden_card = hidden_cards[index]
60
+ return false unless hidden_card
61
+
62
+ hidden_cards[index] = nil
63
+ hand << hidden_card
64
+ sort_hand!
65
+ true
66
+ end
67
+
68
+ def throw(card)
69
+ hand.delete(card)
70
+ end
71
+
72
+ def sort_hand!
73
+ hand.sort_by!(&:weight).reverse!
74
+ end
75
+
76
+ def to_h
77
+ {
78
+ id: id,
79
+ name: name,
80
+ hand: hand.map(&:to_h),
81
+ hidden_cards: hidden_cards.map(&:to_h),
82
+ visible_cards: visible_cards.map(&:to_h),
83
+ ready: ready
84
+ }
85
+ end
86
+
87
+ def self.from_h(hash) # rubocop:disable Metrics/AbcSize
88
+ return if hash.empty?
89
+
90
+ new(hash[:name], hash[:id]) do |p|
91
+ p.hand = hash[:hand].map { |h| Card.from_h(h) }
92
+ p.hidden_cards = hash[:hidden_cards].map { |h| Card.from_h(h) }
93
+ p.visible_cards = hash[:visible_cards].map { |h| Card.from_h(h) }
94
+ p.ready = hash[:ready]
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brigitte
4
+ VERSION = '0.1.3'
5
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brigitte
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - youszef
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Card game where player need to lose all of it's cards
14
+ email:
15
+ - zouhariy@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".editorconfig"
21
+ - ".github/workflows/gem-push.yml"
22
+ - ".github/workflows/ruby.yml"
23
+ - ".gitignore"
24
+ - ".rspec"
25
+ - ".rubocop.yml"
26
+ - CHANGELOG.md
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/console
33
+ - bin/setup
34
+ - brigitte.gemspec
35
+ - lib/brigitte.rb
36
+ - lib/brigitte/card.rb
37
+ - lib/brigitte/commands/pile.rb
38
+ - lib/brigitte/deck.rb
39
+ - lib/brigitte/game.rb
40
+ - lib/brigitte/player.rb
41
+ - lib/brigitte/version.rb
42
+ homepage: https://github.com/youszef/brigitte
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ homepage_uri: https://github.com/youszef/brigitte
47
+ source_code_uri: https://github.com/youszef/brigitte
48
+ changelog_uri: https://github.com/youszef/brigitte/CHANGELOG.md
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.7.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.1.4
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Card game based on Shithead
68
+ test_files: []