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.
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
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.
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
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
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.
↓ Let's make draw poker with ruby-Implementation 4 (Deck)-
Recommended Posts