Let's make draw poker with ruby-Implementation 4 (Deck)-

Overview

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

Implementation of deck

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?

Fixed player hand exchange

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

Creating a deck

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.

Card collection by hand exchange

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

Card draw

--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.

At the end

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

Let's make draw poker with ruby-Implementation 4 (Deck)-
Let's make draw poker with ruby-Implementation 1 (card)-
Let's make draw poker with ruby-Implementation 3 (player)-
Let's make draw poker with ruby-Implementation 2 (role)-
Let's make draw poker with ruby ~ Preparation ~
Let's make draw poker with ruby ~ test-unit preparation ~
Let's make a Christmas card with Processing!
Let's make a smart home with Ruby!
Let's make an error screen with Rails
Let's make a search function with Rails (ransack)
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[Java basics] Let's make a triangle with a for statement
Make Nginx of CentOS8 SSL compatible with Let's Encrypt
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Let's make draw poker with ruby ~ Preparation ~
Let's make Rails-like (View)
Let's scrape with Java! !!
Let's write how to make API with SpringBoot + Docker from 0
Let's make a simple API with EC2 + RDS + Spring boot ①