[Ruby] Making Draw Poker with ruby-Mounting 1 (Card)-

3 minute read

Overview

Try making draw poker with ruby ~Preparation~Try making draw poker with ruby ~test-unit preparation~

Followed by.

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

I will finally start implementing this time.

Card mounting

I think that the single most moving end of poker is a card, so I will make it from cards. Let’s organize the information.

  • Numbers
  • Suit (symbol)

First of all, all cards have it, so we will implement this.

generate

  • Since all cards are generated, in that case, numbers and suits are always passed, so it is an argument of initialize
  • Numbers and suits can be referenced from outside, but cannot be changed

ruby_poker/card.rb


module RubyPoker
  class Card
    attr_reader :suit, :number

    def initialize(suit:, number:)
      @suit = suit
      @number = number
    end
  end
end

And read for new file

ruby_poker.rb


require "ruby_poker/card"

I think this is the end, and think about the next time you use the card.

comparison

Cards use numbers and suits to make a “hand”, and the hand is a decision, so is it sufficient to refer to the card? I thought, If you check the explanation of the role in detail, it is said that there are strengths between the cards, such as judging based on the strength of the card used in the role in the case of the same role.

For example, in the case of “two cards”

  • Bigger number wins
  • If the numbers are the same, the stronger suit wins

As confirmed in the preparation section, the order is as follows.

  • If two players make the same hand, the card with the strongest part of the hand wins -Strong ranking of cards is A, K, Q, J, 10~2 -The strong ranking of suits is Spade Heart Diamond Clover

Since it can be determined by the element number of the array, define it (definition is arranged in strong order, so if you take index, reverse order)

ruby_poker.rb


module RubyPoker
  NUMBERS = ([1] + [*2..13].reverse).freeze
  SUITS = %i[spade heart diamond club].freeze
end

Also, since it is a big deal, it is also used for argument checking on the card.rb side.

ruby_poker/card.rb


    def initialize(suit:, number:)
+ raise(ArgumentError) unless RubyPoker::SUITS.include?(suit)
+ raise(ArgumentError) unless RubyPoker::NUMBERS.include?(number)

In addition, regarding the use case to judge the strength of the element, the following was considered.

  1. Which card is the strongest in your hand (for straight comparisons, etc.)
  2. Comparing the strongest cards with each other when comparing with the enemy role

As for 1, since you want to take the strongest one out of multiple, max Regarding 2, compare big and small > etc.

Therefore, it is solved by using Comparable and implementing <=> (max is determined by <=> Array#max)

If you compare numbers with simple numbers, 1 will be the weakest, so compare properly based on index

ruby_poker/card.rb


module RubyPoker
  class Card
+ include Comparable
    attr_reader :suit, :number

ruby_poker/card.rb


    def suit_level
      RubyPoker::SUITS.reverse.index(@suit)
    end

    def number_level
      RubyPoker::NUMBERS.reverse.index(@number)
    end

    def <=>(other)
      number_comparision = number_level <=> other.number_level
      number_comparision.zero?? suit_level <=> other.suit_level :number_comparision
    end

test case

Easily implement test cases *Test-unit is the first time so please let me know if there is a good way to describe it

require "test_helper"

module RubyPoker
  class CardTest <Test::Unit::TestCase
    sub_test_case "#initialize" do
      test "correct arguments" do
        assert_nothing_raised do
          Card.new(suit: :heart, number: 3)
        end
      end

      test "wrong suit" do
        assert_raise_kind_of(ArgumentError) do
          Card.new(suit: :test, number: 3)
        end
      end

      test "wrong number" do
        assert_raise_kind_of(ArgumentError) do
          Card.new(suite: :heart, number: 14)
        end
      end
    end

    sub_test_case "#<=>" do
      sub_test_case "compare number" do
        test "simple numbers" do
          a = Card.new(suit: :heart, number: 8)
          b = Card.new(suit: :heart, number: 2)
          assert(a> b)
        end

        test "compare ace" do
          a = Card.new(suit: :heart, number: 13)
          b = Card.new(suit: :heart, number: 1)
          assert(a <b)
        end

        test "max number" do
          cards = [*1..5]
            .shuffle
            .map {|i| Card.new(suit: :heart, number: i)}

          assert_equal(1, cards.max.number)
        end
      end

      sub_test_case "compare suit(same number)" do
        test "spade and heart" do
          a = Card.new(suit: :spade, number: 1)
          b = Card.new(suit: :heart, number: 1)
          assert(a> b)
        end

        test "heart and club" do
          a = Card.new(suit: :club, number: 1)
          b = Card.new(suit: :heart, number: 1)
          assert(a <b)
        end

        test "spade and diamond" do
          a = Card.new(suit: :spade, number: 1)
          b = Card.new(suit: :diamond, number: 1)
          assert(a> b)
        end
      end
    end
  end
end

Is this the completion of the card for now?

continued

Try to make draw poker with ruby-Implementation 2 (role)-

Tags:

Updated: