[Ruby] Making Draw Poker with ruby-Mounting Edition 4 (Draft)-

2 minute read


Try making draw poker with ruby ~Preparation~Try making draw poker with ruby ~test-unit preparation~Try to make a draw poker with ruby ~ Implementation 1 (card) ~Try to make draw poker with ruby-Implementation 2 (role)- ↓ [Try making draw poker with ruby-implementation 3 (player)]](https://qiita.com/r12tkmt/items/ae44086d9bf7d01e7572) Followed by.

Source: https://github.com/rytkmt/ruby_poker

Mounting the deck

Yes, so again this time, we will first organize the requirements.

  • Initially create a deck using all 13 x 4 cards -Cards are arranged in different order
  • Collect cards that are no longer needed after hand exchange
  • Draw any number from the deck -If you run out of draws, mix cards you no longer need in your hand, shuffle and draw from there

Is it like that?

Fixed player hand exchange

As I noticed here, because I implemented it in the previous player’s implementation by simply exchanging from the cards in my hand by hand exchange. Correct it so that the cards you discard will be collected…


    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

Creating a deck


module RubyPoker
  class Deck
    def initialize
      @cards = init_cards
      @trushed = []

    def init_cards
      RubyPoker::SUITS.each_with_object([]) do |suit, cards|
          RubyPoker::NUMBERS.map {|number| Card.new(suit: suit, number: number)}

At first, I implemented it using += in each_with_object, but if you think carefully, this is because the one combined with + is assigned a variable with =, so another object combined with the same variable is It just didn’t work because I just stored it and not made any breaking changes.

Use concat to change cards destructively.

Collecting cards by exchanging cards

It’s easy because you just collect and retain the unnecessary cards returned by the player’s #change


    def trush(cards:)
      @trushed += cards

Card draw

-Draw any number from the deck -If you run out of draws, mix cards you no longer need in your hand, shuffle and draw from there


    def draw(count:)
      merge_trushed if @cards.size <count
      raise(ArgumentError, "No cards.") if @cards.size <count


    def merge_trushed
      @cards += @trushed
      @trushed = []

I feel that the number of players is not set properly because there is not enough, so I made an error on the assumption that it does not happen.

At the end

The deck was also simple. Will this be the next step in the implementation of the game? I think.

The completion is approaching. I want to finish it in 4 consecutive holidays and I will finish it until the end.