Let's make draw poker with ruby-Implementation 3 (player)-

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

Followed by.

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

This time, I would like to start implementing players who handle cards and roles.

Player implementation

First of all, let's sort out the requirements.

――Get 5 cards at the beginning --Judge the role --Exchange any number of cards ――Judge the role again --Judge the player's victory or defeat ――In effect, determine the role of the player

File creation

ruby_poker/player.rb


module RubyPoker
  class Player
    def initialize(cards:)
      @cards = cards
      @hand = Hand.new(cards: cards)
    end
  end
end

This was done until the role was judged by the card I received.

ruby_poker.rb


require "ruby_poker/version"
require "active_support/all"
-require "ruby_poker/card"
-require "ruby_poker/hand"
+Dir[__dir__ + "/ruby_poker/*.rb"].each { |p| require p }

module RubyPoker

Don't forget to require, but I felt like adding one file at a time, so I changed it to read all.

Card exchange

Then implement the replacement of any card.

--Discard unnecessary cards --Get a new card --Judge the role again with a new card

When exchanging cards, I will get the element number and delete it. At this time, since it is deleted by the element number, if you delete from the smallest one, the element will be misaligned by that amount and it will be strange. Therefore, it is necessary to delete from the largest number, so implement it carefully. Verification)

x = %i[a b c d e]
# a & c & d    =>   [b, e]
indexes = [0, 2, 3]

indexes.each { |i| x.delete_at(i); p x }
# => [:b, :c, :d, :e]
# => [:b, :c, :e]
# => [:b, :c, :e]

x = %i[a b c d e]
indexes.sort.reverse_each { |i| x.delete_at(i); p x }
# => [:a, :b, :c, :e]
# => [:a, :b, :e]
# => [:b, :e]

Since the cards need to be obtained from the deck, I thought that the range of responsibilities of the player would be reduced if I got new cards as many as the number of cards to discard. So let's implement it that way (although we may change it later) k

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) }
      @cards += new_cards
      @hand = Hand.new(cards: @cards)
    end

Player win / loss judgment

We will use Comparable again. In the end, it will be decided by the role, so I will delegate it.

~~ delegate has delegate method extended to Module in ʻactive_support. ~~ ~~ Class is a subclass of Module, so you can use delegate` as well. ~~

ruby_poker/player.rb


    delegate :<=>, to: :@hand

↑ It was no good. .. If other of <=> is delegate as it is, it will be passed to hand, so I will compare my hand with other player.

ruby_poker/player.rb


    attr_reader :hand
    def <=>(other)
      @hand <=> other.hand
    end

I implemented it quietly and normally. ..

That's it. It's easy

At the end

This time, we implemented only the functional processing related to winning and losing. From here, considering the progress of the game, it is necessary to implement the console output, but I would like to add it at the timing of considering the next progress.

Continued

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

Recommended Posts

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