Class: Planner

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

Instance Method Summary collapse

Instance Method Details

#bit_select(cards, bit) ⇒ Object



161
162
163
164
165
# File 'lib/manasimu/planner.rb', line 161

def bit_select(cards, bit)
  cards.length.times
        .map { |i| cards[i] if (bit & (1 << i) > 0) }
        .select { |o| o }
end

#dfs(n, spells, lands, bit_spells, bit_lands, price, total_land_symbols) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/manasimu/planner.rb', line 86

def dfs(n, spells, lands, bit_spells, bit_lands, price, total_land_symbols)
  index = n - 1

  # exit
  return [price, bit_spells, bit_lands, total_land_symbols] if n > spells.length

  spell = spells[index]
  used_lands = bit_lands.to_s(2).chars
  capas = lands.length.times.to_a.map do |i|
    if used_lands[i] == "1"
      "0"
    else
      lands[i].tapped? ? "0" : "1"
    end
  end

  # shrink
  # lands_available = []
  # lands.length.times do |i|
  #   next if used_lands[i] == "1"
  #   lands_available << lands[i]
  # end
  # capas = ("1" * lands_available.length).chars

  # cast case
  is_playable, casted_lands, land_symbols = 
    spell.playable?(lands, capas)

  # expand
  # used_lands_ = []
  # land_symbols_ = []
  # j = 0
  # lands.length.times do |i|
  #   if used_lands[i] == "1" or not casted_lands
  #     used_lands_ << "1"
  #     land_symbols_ << total_land_symbols[i]
  #   else
  #     used_lands_ << casted_lands[j]
  #     land_symbols_ << land_symbols[j]
  #     j += 1
  #   end
  # end if lands

  a_price, a_bit_spells, a_bit_lands, a_land_symbols = 
    if is_playable

      bit_spells = bit_spells | 1 << ( n - 1 )
      bit_lands_ = casted_lands
        .reverse
        .map {|i| i.to_s}
        .join('')
        .to_i(2)

      land_symbols_ = lands.length.times.to_a.map do |i|
        land_symbols[i] ? land_symbols[i] : total_land_symbols[i]
      end

      # dfs
      dfs(n + 1 , spells, lands, bit_spells, bit_lands_, 
          price + spell.price, land_symbols_)
    else
      [nil, nil, nil, nil]
    end

  # not cast case
  b_price, b_bit_spells, b_bit_lands, b_land_symbols = 
    dfs(n + 1 , spells, lands, bit_spells, bit_lands, price, total_land_symbols)

  if (a_price and a_price >= b_price)
    [a_price, a_bit_spells, a_bit_lands, a_land_symbols]
  else
    [b_price, b_bit_spells, b_bit_lands, b_land_symbols]
  end
end

#lands(list) ⇒ Object



167
168
169
170
171
# File 'lib/manasimu/planner.rb', line 167

def lands(list)
  list.select do |card|
    card.types.include? "Land"
  end
end

#plan(hands, fields, deck) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
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
56
57
# File 'lib/manasimu/planner.rb', line 3

def plan(hands, fields, deck)
  lands_in_hand = lands(hands)

  max_price =  0
  max_spells = nil
  max_land = nil
  max_symbols = nil
  max_lands = nil

  if not lands_in_hand.empty?
    lands_in_hand.each do |play_land|
      # dup
      current_hands = hands.dup
      current_fields = fields.dup
      current_deck = deck.dup

      # play the land
      current_hands.delete play_land
      current_fields << play_land
      play_land.resolve(nil, current_hands, current_fields, current_deck)

      # search_opt_spells
      price, spells, symbols, lands = 
        search_opt_spells(current_hands, current_fields)
      if price >= max_price and not spells.empty?
        max_price = price
        max_spells = spells
        max_land = play_land
        max_symbols = symbols
        max_lands = lands
      end

      play_land.reset
    end
  else
    # search_opt_spells
    max_price, max_spells, max_symbols, max_lands = search_opt_spells(hands, fields)
  end

  if not max_spells and not lands_in_hand.empty?
    max_land = lands_in_hand[0]
  end

  deck = nil
  if max_lands
    max_lands.each_with_index do |land, i|
      if not land.mana_produced? and max_symbols[i]
        land.first_produce_symbol = max_symbols[i]
        deck = land.deck if land.respond_to? :deck
      end
    end
  end

  [[max_land, max_spells].select {|a| a}.flatten, deck]
end

#search_opt_spells(hands, fields) ⇒ Object

on conditional playing land, search most high price spells combinations return price, spells



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/manasimu/planner.rb', line 64

def search_opt_spells(hands, fields)
  spells = spells(hands)
  lands = lands(fields)

  # sort spells desc converted_mana_cost
  spells.sort! do |a, b|
    b.converted_mana_cost <=> a.converted_mana_cost
  end

  lands.sort! do |a, b|
    b.mana_source_size <=> a.mana_source_size
  end

  price = 0
  bit_lands = 0
  bit_spells = 0
  # search playable spell comibantion
  cost, bit_spells, bit_lands, land_symbols =
    dfs(1, spells, lands, bit_spells, bit_lands, price, [])
  [price, bit_select(spells, bit_spells), land_symbols, lands]
end

#spells(list) ⇒ Object



173
174
175
176
177
# File 'lib/manasimu/planner.rb', line 173

def spells(list)
  list.select do |card|
    not card.types.include? "Land"
  end
end