Let's make draw poker with ruby-Implementation 1 (card)-

Overview

Let's make draw poker with ruby-Preparation-Let's make draw poker with ruby-test-unit preparation-

Followed by.

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

From this time, I will finally start implementing it.

Card mounting

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

--Numbers --Soothe (symbol)

First of all, all the cards hold it, so implement this.

Generate

--Because all cards are generated, in that case, numbers and suits must be passed, so use ʻinitialize` as an argument. --Although numbers and suits can be referenced from the outside, they 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 a new file

ruby_poker.rb


require "ruby_poker/card"

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

Comparison

Since the card makes a "role" using numbers and suits and the role is judged, is it enough to refer to the card? I also thought, If you check the explanation of the role in detail, it is said that there are strengths and weaknesses between the cards, such as judging based on the strength of the cards used in the role in the case of the same role.

For example, in the case of "two cards" ――The higher the number, the better ――If the numbers are the same, the one with the stronger suit wins.

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

――If two players make the same role, the stronger card that makes up the role wins. --The strong ranking of cards is A, K, Q, J, 10 ~ 2. ――The strong ranking of the suit is Spade Heart Diamond Clover

Since it seems that it can be judged by the element number of the array, define it (the definitions are arranged in order of strength, so if you take the index, reverse the order)

ruby_poker.rb


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

Also, since it's a big deal, I also use it for argument check 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, the following are considered as use cases for determining the strength of elements.

  1. Which of the strongest cards in your hand was used (for example, when comparing straights)
  2. Comparison between the strongest cards when comparing with the role of the enemy

Regarding 1, I want to take the strongest one from multiple, so max Regarding 2, large and small comparison > etc.

Therefore, it can be solved by implementing <=> using Comparable (max is determined by <=> Array # max. ja / latest / method / Array / i / max.html)))

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 tell me 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 end of the card for the time being?

Continued

Let's make draw poker with ruby-Implementation 2 (role)-

Recommended Posts

Let's make draw poker with ruby-Implementation 1 (card)-
Let's make draw poker with ruby-Implementation 4 (Deck)-
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 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 ①