Class: Mjai::Hora::Candidate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hora, combination, taken_index) ⇒ Candidate

Returns a new instance of Candidate.



82
83
84
85
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/mjai/hora.rb', line 82

def initialize(hora, combination, taken_index)
  
  @hora = hora
  @combination = combination
  @all_pais = hora.all_pais.map(){ |pai| pai.remove_red() }
  
  @mentsus = []
  @janto = nil
  total_taken = 0
  if combination == :chitoitsu
    @machi = :tanki
    for pai in @all_pais.uniq()
      mentsu = Mentsu.new(:toitsu, :an, [pai, pai])
      if pai.same_symbol?(hora.taken)
        @janto = mentsu
      else
        @mentsus.push(mentsu)
      end
    end
  elsif combination == :kokushimuso
    @machi = :tanki
  else
    for mentsu_type, mentsu_pais in combination
      num_this_taken = mentsu_pais.select(){ |pai| pai.same_symbol?(hora.taken) }.size
      has_taken = taken_index >= total_taken && taken_index < total_taken + num_this_taken
      if mentsu_type == :toitsu
        raise("should not happen") if @janto
        @janto = Mentsu.new(:toitsu, nil, mentsu_pais)
      else
        @mentsus.push(Mentsu.new(
            mentsu_type,
            has_taken && hora.hora_type == :ron ? :min : :an,
            mentsu_pais))
      end
      if has_taken
        case mentsu_type
          when :toitsu
            @machi = :tanki
          when :kotsu
            @machi = :shanpon
          when :shuntsu
            if mentsu_pais[1].same_symbol?(@hora.taken)
              @machi = :kanchan
            elsif (mentsu_pais[0].number == 1 && @hora.taken.number == 3) ||
                (mentsu_pais[0].number == 7 && @hora.taken.number == 7)
              @machi = :penchan
            else
              @machi = :ryanmen
            end
        end
      end
      total_taken += num_this_taken
    end
  end
  for furo in hora.furos
    @mentsus.push(Mentsu.new(
        FURO_TYPE_TO_MENTSU_TYPE[furo.type],
        furo.type == :ankan ? :an : :min,
        furo.pais.map(){ |pai| pai.remove_red() }.sort()))
  end
  #p @mentsus
  #p @janto
  #p @machi
  
  get_yakus()
  #p @yakus
  @fan = @yakus.map(){ |y, f| f }.inject(0, :+)
  #p [:fan, @fan]
  @fu = get_fu()
  #p [:fu, @fu]
  
  datum = PointsDatum.new(@fu, @fan, @hora.oya, @hora.hora_type)
  @points = datum.points
  @oya_payment = datum.oya_payment
  @ko_payment = datum.ko_payment
  #p [:points, @points, @oya_payment, @ko_payment]
  
end

Instance Attribute Details

#fanObject (readonly)

Returns the value of attribute fan.



161
162
163
# File 'lib/mjai/hora.rb', line 161

def fan
  @fan
end

#fuObject (readonly)

Returns the value of attribute fu.



161
162
163
# File 'lib/mjai/hora.rb', line 161

def fu
  @fu
end

#ko_paymentObject (readonly)

Returns the value of attribute ko_payment.



161
162
163
# File 'lib/mjai/hora.rb', line 161

def ko_payment
  @ko_payment
end

#oya_paymentObject (readonly)

Returns the value of attribute oya_payment.



161
162
163
# File 'lib/mjai/hora.rb', line 161

def oya_payment
  @oya_payment
end

#pointsObject (readonly)

Returns the value of attribute points.



161
162
163
# File 'lib/mjai/hora.rb', line 161

def points
  @points
end

#yakusObject (readonly)

Returns the value of attribute yakus.



161
162
163
# File 'lib/mjai/hora.rb', line 161

def yakus
  @yakus
end

Instance Method Details

#add_yaku(name, menzen_fan, kui_fan) ⇒ Object



314
315
316
317
# File 'lib/mjai/hora.rb', line 314

def add_yaku(name, menzen_fan, kui_fan)
  fan = self.menzen? ? menzen_fan : kui_fan
  @yakus.push([name, fan]) if fan > 0
end

#bakaze?Boolean

Returns:

  • (Boolean)


395
396
397
# File 'lib/mjai/hora.rb', line 395

def bakaze?
  @mentsus.any?(){ |m| [:kotsu, :kantsu].include?(m.type) && m.pais[0] == @hora.bakaze }
end

#chiniso?Boolean

Returns:

  • (Boolean)


458
459
460
461
462
# File 'lib/mjai/hora.rb', line 458

def chiniso?
  return ["m", "p", "s"].any?() do |t|
    @all_pais.all?(){ |pai| pai.type == t }
  end
end

#chinroto?Boolean

Returns:

  • (Boolean)


354
355
356
# File 'lib/mjai/hora.rb', line 354

def chinroto?
  return @all_pais.all?(){ |pai| pai.type != "t" && [1, 9].include?(pai.number) }
end

#churenpoton?Boolean

Returns:

  • (Boolean)


368
369
370
371
372
373
374
# File 'lib/mjai/hora.rb', line 368

def churenpoton?
  return false if !self.chiniso?
  all_numbers = @all_pais.map(){ |pai| pai.number }.sort()
  return (1..9).any?() do |i|
    all_numbers == (CHURENPOTON_NUMBERS + [i]).sort()
  end
end

#daisushi?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/mjai/hora.rb', line 358

def daisushi?
  return @mentsus.all?(){ |m| [:kotsu, :kantsu].include?(m.type) && m.pais[0].fonpai? }
end

#delete_yaku(name) ⇒ Object



319
320
321
# File 'lib/mjai/hora.rb', line 319

def delete_yaku(name)
  @yakus.delete_if(){ |n, f| n == name }
end

#fanpai_fan(pai) ⇒ Object



470
471
472
473
474
475
476
477
478
479
# File 'lib/mjai/hora.rb', line 470

def fanpai_fan(pai)
  if pai.sangenpai?
    return 1
  else
    fan = 0
    fan += 1 if pai == @hora.bakaze
    fan += 1 if pai == @hora.jikaze
    return fan
  end
end

#get_fuObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/mjai/hora.rb', line 323

def get_fu()
  case @combination
    when :chitoitsu
      return 25
    when :kokushimuso
      return 20
    else
      fu = 20
      fu += 10 if self.menzen? && @hora.hora_type == :ron
      fu += 2 if @hora.hora_type == :tsumo && !pinfu?
      for mentsu in @mentsus
        mfu = BASE_FU_MAP[mentsu.type]
        mfu *= 2 if mentsu.pais[0].yaochu?
        mfu *= 2 if mentsu.visibility == :an
        fu += mfu
      end
      fu += fanpai_fan(@janto.pais[0]) * 2
      fu += 2 if [:kanchan, :penchan, :tanki].include?(@machi)
      #p [:raw_fu, fu]
      return (fu / 10.0).ceil * 10
  end
end

#get_yakusObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/mjai/hora.rb', line 168

def get_yakus()
  
  @yakus = []
  
  # 役満
  if @hora.first_turn && @hora.hora_type == :tsumo && @hora.oya
    add_yaku(:tenho, YAKUMAN_FAN, 0)
  end
  if @hora.first_turn && @hora.hora_type == :tsumo && !@hora.oya
    add_yaku(:chiho, YAKUMAN_FAN, 0)
  end
  if @combination == :kokushimuso
    add_yaku(:kokushimuso, YAKUMAN_FAN, 0)
    return
  end
  if self.num_sangenpais == 3
    add_yaku(:daisangen, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.n_anko?(4)
    add_yaku(:suanko, YAKUMAN_FAN, 0)
  end
  if @all_pais.all?(){ |pai| pai.type == "t" }
    add_yaku(:tsuiso, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.ryuiso?
    add_yaku(:ryuiso, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.chinroto?
    add_yaku(:chinroto, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.daisushi?
    add_yaku(:daisushi, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.shosushi?
    add_yaku(:shosushi, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.n_kantsu?(4)
    add_yaku(:sukantsu, YAKUMAN_FAN, YAKUMAN_FAN)
  end
  if self.churenpoton?
    add_yaku(:churenpoton, YAKUMAN_FAN, 0)
  end
  return if !@yakus.empty?
  
  # ドラ
  add_yaku(:dora, @hora.num_doras, @hora.num_doras)
  add_yaku(:uradora, @hora.num_uradoras, @hora.num_uradoras)
  add_yaku(:akadora, @hora.num_akadoras, @hora.num_akadoras)
  
  # 一飜
  if @hora.reach
    add_yaku(:reach, 1, 0)
  end
  if @hora.ippatsu
    add_yaku(:ippatsu, 1, 0)
  end
  if self.menzen? && @hora.hora_type == :tsumo
    add_yaku(:menzenchin_tsumoho, 1, 0)
  end
  if @all_pais.all?(){ |pai| !pai.yaochu? }
    add_yaku(:tanyaochu, 1, 1)
  end
  if self.pinfu?
    add_yaku(:pinfu, 1, 0)
  end
  if self.ipeko?
    add_yaku(:ipeko, 1, 0)
  end
  add_yaku(:sangenpai, self.num_sangenpais, self.num_sangenpais)
  if self.bakaze?
    add_yaku(:bakaze, 1, 1)
  end
  if self.jikaze?
    add_yaku(:jikaze, 1, 1)
  end
  if @hora.rinshan
    add_yaku(:rinshankaiho, 1, 1)
  end
  if @hora.chankan
    add_yaku(:chankan, 1, 1)
  end
  if @hora.haitei && @hora.hora_type == :tsumo
    add_yaku(:haiteiraoyue, 1, 1)
  end
  if @hora.haitei && @hora.hora_type == :ron
    add_yaku(:hoteiraoyui, 1, 1)
  end
  
  # 二飜
  if self.sanshoku?([:shuntsu])
    add_yaku(:sanshokudojun, 2, 1)
  end
  if self.ikkitsukan?
    add_yaku(:ikkitsukan, 2, 1)
  end
  if self.honchantaiyao?
    add_yaku(:honchantaiyao, 2, 1)
  end
  if @combination == :chitoitsu
    add_yaku(:chitoitsu, 2, 0)
  end
  if @mentsus.all?(){ |m| [:kotsu, :kantsu].include?(m.type) }
    add_yaku(:toitoiho, 2, 2)
  end
  if self.n_anko?(3)
    add_yaku(:sananko, 2, 2)
  end
  if @all_pais.all?(){ |pai| pai.yaochu? }
    add_yaku(:honroto, 2, 2)
    delete_yaku(:honchantaiyao)
  end
  if self.sanshoku?([:kotsu, :kantsu])
    add_yaku(:sanshokudoko, 2, 2)
  end
  if self.n_kantsu?(3)
    add_yaku(:sankantsu, 2, 2)
  end
  if self.shosangen?
    add_yaku(:shosangen, 2, 2)
  end
  if @hora.double_reach
    add_yaku(:double_reach, 2, 0)
    delete_yaku(:reach)
  end
  
  # 三飜
  if self.honiso?
    add_yaku(:honiso, 3, 2)
  end
  if self.junchantaiyao?
    add_yaku(:junchantaiyao, 3, 2)
    delete_yaku(:honchantaiyao)
  end
  if self.ryanpeko?
    add_yaku(:ryanpeko, 3, 0)
    delete_yaku(:ipeko)
  end
  
  # 六飜
  if self.chiniso?
    add_yaku(:chiniso, 6, 5)
    delete_yaku(:honiso)
  end
  
end

#honchantaiyao?Boolean

Returns:

  • (Boolean)


418
419
420
# File 'lib/mjai/hora.rb', line 418

def honchantaiyao?
  return (@mentsus + [@janto]).all?(){ |m| m.pais.any?(){ |pai| pai.yaochu? } }
end

#honiso?Boolean

Returns:

  • (Boolean)


437
438
439
440
441
# File 'lib/mjai/hora.rb', line 437

def honiso?
  return ["m", "p", "s"].any?() do |t|
    @all_pais.all?(){ |pai| [t, "t"].include?(pai.type) }
  end
end

#ikkitsukan?Boolean

Returns:

  • (Boolean)


410
411
412
413
414
415
416
# File 'lib/mjai/hora.rb', line 410

def ikkitsukan?
  return ["m", "p", "s"].any?() do |t|
    [1, 4, 7].all?() do |n|
      @mentsus.any?(){ |m| m.type == :shuntsu && m.pais[0].same_symbol?(Pai.new(t, n)) }
    end
  end
end

#ipeko?Boolean

Returns:

  • (Boolean)


382
383
384
385
386
387
388
389
# File 'lib/mjai/hora.rb', line 382

def ipeko?
  return @mentsus.any?() do |m1|
    m1.type == :shuntsu &&
        @mentsus.any?() do |m2|
          !m2.equal?(m1) && m2.type == :shuntsu && m2.pais[0].same_symbol?(m1.pais[0])
        end
  end
end

#jikaze?Boolean

Returns:

  • (Boolean)


391
392
393
# File 'lib/mjai/hora.rb', line 391

def jikaze?
  @mentsus.any?(){ |m| [:kotsu, :kantsu].include?(m.type) && m.pais[0] == @hora.jikaze }
end

#junchantaiyao?Boolean

Returns:

  • (Boolean)


443
444
445
446
447
# File 'lib/mjai/hora.rb', line 443

def junchantaiyao?
  return (@mentsus + [@janto]).all?() do |m|
    m.pais.any?(){ |pai| pai.type != "t" && [1, 9].include?(pai.number) }
  end
end

#menzen?Boolean

Returns:

  • (Boolean)


346
347
348
# File 'lib/mjai/hora.rb', line 346

def menzen?
  return @hora.furos.select(){ |f| f.type != :ankan }.empty?
end

#n_anko?(n) ⇒ Boolean

Returns:

  • (Boolean)


422
423
424
425
426
427
# File 'lib/mjai/hora.rb', line 422

def n_anko?(n)
  ankos = @mentsus.select() do |m|
    [:kotsu, :kantsu].include?(m.type) && m.visibility == :an
  end
  return ankos.size == n
end

#n_kantsu?(n) ⇒ Boolean

Returns:

  • (Boolean)


429
430
431
# File 'lib/mjai/hora.rb', line 429

def n_kantsu?(n)
  return @mentsus.select(){ |m| m.type == :kantsu }.size == n
end

#num_sangenpaisObject



464
465
466
467
468
# File 'lib/mjai/hora.rb', line 464

def num_sangenpais
  return @mentsus.
      select(){ |m| m.pais[0].sangenpai? && [:kotsu, :kantsu].include?(m.type) }.
      size
end

#pinfu?Boolean

Returns:

  • (Boolean)


376
377
378
379
380
# File 'lib/mjai/hora.rb', line 376

def pinfu?
  return @mentsus.all?(){ |m| m.type == :shuntsu } &&
      @machi == :ryanmen &&
      fanpai_fan(@janto.pais[0]) == 0
end

#ryanpeko?Boolean

Returns:

  • (Boolean)


449
450
451
452
453
454
455
456
# File 'lib/mjai/hora.rb', line 449

def ryanpeko?
  return @mentsus.all?() do |m1|
    m1.type == :shuntsu &&
        @mentsus.any?() do |m2|
          !m2.equal?(m1) && m2.type == :shuntsu && m2.pais[0].same_symbol?(m1.pais[0])
        end
  end
end

#ryuiso?Boolean

Returns:

  • (Boolean)


350
351
352
# File 'lib/mjai/hora.rb', line 350

def ryuiso?
  return @all_pais.all?(){ |pai| GREEN_PAIS.include?(pai) }
end

#sanshoku?(types) ⇒ Boolean

Returns:

  • (Boolean)


399
400
401
402
403
404
405
406
407
408
# File 'lib/mjai/hora.rb', line 399

def sanshoku?(types)
  return @mentsus.any?() do |m1|
    types.include?(m1.type) &&
        ["m", "p", "s"].all?() do |t|
          @mentsus.any?() do |m2|
            types.include?(m2.type) && m2.pais[0].same_symbol?(Pai.new(t, m1.pais[0].number))
          end
        end
  end
end

#shosangen?Boolean

Returns:

  • (Boolean)


433
434
435
# File 'lib/mjai/hora.rb', line 433

def shosangen?
  return self.num_sangenpais == 2 && @janto.pais[0].sangenpai?
end

#shosushi?Boolean

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/mjai/hora.rb', line 362

def shosushi?
  fonpai_kotsus = @mentsus.
      select(){ |m| [:kotsu, :kantsu].include?(m.type) && m.pais[0].fonpai? }
  return fonpai_kotsus.size == 3 && @janto.pais[0].fonpai?
end

#valid?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/mjai/hora.rb', line 163

def valid?
  return !@yakus.select(){ |n, f| ![:dora, :uradora, :akadora].include?(n) }.empty?
end