Let's make draw poker with ruby-Preparation- ↓ Let's make draw poker with ruby-test-unit preparation- ↓ Let's make draw poker with ruby-Implementation 1 (card)- ↓ Let's make draw poker with ruby-Implementation 2 (role)- ↓ Try to make draw poker with ruby-Implementation 3 (player)- Followed by.
Source: https://github.com/rytkmt/ruby_poker
Yes, this time as well, we will start by organizing the requirements.
--First, create a deck using all 13 x 4 cards. --The order of the cards is different --Collect cards that are no longer needed after exchanging hands --Draw any number of cards from the deck --If you run out of cards to draw, mix and shuffle the cards you no longer need in your hand exchange and draw from there.
Is it like that?
I noticed here, but in the previous implementation of the player, it was implemented by simply deleting from the card in the hand by exchanging the hand. Modify to collect the cards to be discarded ...
ruby_poker/player.rb
    def change(indexes:, new_cards:)
       raise(ArgumentError) unless indexes.size == new_cards.size
       raise(ArgumentError) unless indexes.all? { |i| (0..4).include?(i) }
-      indexes.sort.reverse_each { |i| @cards.delete_at(i) }
+      trushed = indexes.sort.reverse.each_with_object([]) { |i, trushed| trushed << @cards.delete_at(i) }
       @cards += new_cards
       @hand = Hand.new(cards: @cards)
+      trushed
     end
ruby_poker/deck.rb
module RubyPoker
  class Deck
    def initialize
      @cards = init_cards
      @trushed = []
    end
  private
    def init_cards
      RubyPoker::SUITS.each_with_object([]) do |suit, cards|
        cards.concat(
          RubyPoker::NUMBERS.map { |number| Card.new(suit: suit, number: number) }
        )
      end.shuffle
    end
  end
end
At first, I implemented it using + = in each_with_object, but if you think about it carefully, this is a variable assignment of what was combined with + with =, so another object combined with the same variable It didn't work because I just stored it and didn't make any destructive changes.
Use concat to change cards destructively.
It's easy because all you have to do is collect and hold the unnecessary cards returned by the player's # change.
ruby_poker/deck.rb
    def trush(cards:)
      @trushed += cards
    end
--Draw any number of cards from the deck --If you run out of cards to draw, mix and shuffle the cards you no longer need in your hand exchange and draw from there.
ruby/ruby_poker/deck.rb
    def draw(count:)
      merge_trushed if @cards.size < count
      raise(ArgumentError, "No cards.") if @cards.size < count
      @cards.shift(count)
    end
  private
    def merge_trushed
      @cards += @trushed
      @cards.shuffle!
      @trushed = []
    end
I feel that the number of players is not set properly because it is not enough, so I tried to throw an error on the assumption that it will not happen.
The deck was also simple. Is this the next time to implement the game progress? I think.
It is nearing completion. I want to finish it in 4 consecutive holidays and I will do it to the end.
Recommended Posts