Class: Rypto::Hand

Inherits:
Object
  • Object
show all
Defined in:
lib/rypto.rb

Overview

A individual hand of Krypto

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(krypto_cards = nil, target_card = nil) ⇒ Hand

May be called with 0 or 2 arguments. If called with 0 arguments, will return a random deal from a full Deck. If called with 2 arguments, will create a hand from the specified values.

Parameters:

  • krypto_cards (Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)) (defaults to: nil)

    Array of five krypto card values

  • target_card (Fixnum) (defaults to: nil)

    Target card value



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rypto.rb', line 27

def initialize(krypto_cards = nil, target_card = nil)
  if !krypto_cards.nil? and target_card.nil?
    raise ArgumentError, "Rypto::Hand#new must be called with 0 or 2 arguments"
  end

  if krypto_cards.nil?
    cards = Deck.new.deal_cards
    @krypto_cards = cards[0, 5]
    @target_card  = cards[5]
  else
    unless krypto_cards.is_a?(Array) and krypto_cards.size == 5
      raise ArgumentError, "Expected first argument to be an array of 5 integers"
    end

    tmp_deck = Deck.new
    krypto_cards.each {|card| tmp_deck.draw_card card}
    tmp_deck.draw_card target_card

    @krypto_cards = krypto_cards
    @target_card  = target_card
  end
end

Instance Attribute Details

#krypto_cardsArray(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum) (readonly)

Returns the five krypto cards to evaluate.

Returns:

  • (Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum))

    the five krypto cards to evaluate



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rypto.rb', line 15

class Hand
  attr_reader :krypto_cards, :target_card

  # May be called with 0 or 2 arguments.  If called with 0
  # arguments, will return a random deal from a full {Rypto::Deck}.  If
  # called with 2 arguments, will create a hand from the specified
  # values.
  # 
  # @param krypto_cards [Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)]  
  #                     Array of five krypto card values
  # @param target_card  [Fixnum]
  #                     Target card value
  def initialize(krypto_cards = nil, target_card = nil)
    if !krypto_cards.nil? and target_card.nil?
      raise ArgumentError, "Rypto::Hand#new must be called with 0 or 2 arguments"
    end

    if krypto_cards.nil?
      cards = Deck.new.deal_cards
      @krypto_cards = cards[0, 5]
      @target_card  = cards[5]
    else
      unless krypto_cards.is_a?(Array) and krypto_cards.size == 5
        raise ArgumentError, "Expected first argument to be an array of 5 integers"
      end

      tmp_deck = Deck.new
      krypto_cards.each {|card| tmp_deck.draw_card card}
      tmp_deck.draw_card target_card

      @krypto_cards = krypto_cards
      @target_card  = target_card
    end
  end

  # Get all possible solutions for the hand
  # @return [Solution]
  def solve
    BruteForceSolver.new(self).solve
  end
end

#target_cardFixnum (readonly)

Returns the target card that the expression made from the five krypto_cards should equal.

Returns:

  • (Fixnum)

    the target card that the expression made from the five krypto_cards should equal



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rypto.rb', line 15

class Hand
  attr_reader :krypto_cards, :target_card

  # May be called with 0 or 2 arguments.  If called with 0
  # arguments, will return a random deal from a full {Rypto::Deck}.  If
  # called with 2 arguments, will create a hand from the specified
  # values.
  # 
  # @param krypto_cards [Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)]  
  #                     Array of five krypto card values
  # @param target_card  [Fixnum]
  #                     Target card value
  def initialize(krypto_cards = nil, target_card = nil)
    if !krypto_cards.nil? and target_card.nil?
      raise ArgumentError, "Rypto::Hand#new must be called with 0 or 2 arguments"
    end

    if krypto_cards.nil?
      cards = Deck.new.deal_cards
      @krypto_cards = cards[0, 5]
      @target_card  = cards[5]
    else
      unless krypto_cards.is_a?(Array) and krypto_cards.size == 5
        raise ArgumentError, "Expected first argument to be an array of 5 integers"
      end

      tmp_deck = Deck.new
      krypto_cards.each {|card| tmp_deck.draw_card card}
      tmp_deck.draw_card target_card

      @krypto_cards = krypto_cards
      @target_card  = target_card
    end
  end

  # Get all possible solutions for the hand
  # @return [Solution]
  def solve
    BruteForceSolver.new(self).solve
  end
end

Instance Method Details

#solveSolution

Get all possible solutions for the hand

Returns:



52
53
54
# File 'lib/rypto.rb', line 52

def solve
  BruteForceSolver.new(self).solve
end