Class: Mjai::Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



19
20
21
# File 'lib/mjai/player.rb', line 19

def attributes
  @attributes
end

#extra_anpaisObject (readonly)

sutehais以外のこのプレーヤに対する安牌



16
17
18
# File 'lib/mjai/player.rb', line 16

def extra_anpais
  @extra_anpais
end

#furosObject (readonly)

副露



13
14
15
# File 'lib/mjai/player.rb', line 13

def furos
  @furos
end

#gameObject

Returns the value of attribute game.



21
22
23
# File 'lib/mjai/player.rb', line 21

def game
  @game
end

#hoObject (readonly)

河 (鳴かれた牌を含まない)



14
15
16
# File 'lib/mjai/player.rb', line 14

def ho
  @ho
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/mjai/player.rb', line 11

def id
  @id
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/mjai/player.rb', line 20

def name
  @name
end

#reach_ho_indexObject (readonly)

Returns the value of attribute reach_ho_index.



18
19
20
# File 'lib/mjai/player.rb', line 18

def reach_ho_index
  @reach_ho_index
end

#reach_stateObject (readonly)

Returns the value of attribute reach_state.



17
18
19
# File 'lib/mjai/player.rb', line 17

def reach_state
  @reach_state
end

#scoreObject

Returns the value of attribute score.



22
23
24
# File 'lib/mjai/player.rb', line 22

def score
  @score
end

#sutehaisObject (readonly)

捨牌 (鳴かれた牌を含む)



15
16
17
# File 'lib/mjai/player.rb', line 15

def sutehais
  @sutehais
end

#tehaisObject (readonly)

手牌



12
13
14
# File 'lib/mjai/player.rb', line 12

def tehais
  @tehais
end

Instance Method Details

#anpaisObject



24
25
26
# File 'lib/mjai/player.rb', line 24

def anpais
  return @sutehais + @extra_anpais
end

#can_hora?(shanten_analysis = nil) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/mjai/player.rb', line 177

def can_hora?(shanten_analysis = nil)
  action = @game.current_action
  if action.type == :tsumo && action.actor == self
    hora_type = :tsumo
    pais = @tehais
  elsif [:dahai, :kakan].include?(action.type) && action.actor != self
    hora_type = :ron
    pais = @tehais + [action.pai]
  else
    return false
  end
  shanten_analysis ||= ShantenAnalysis.new(pais, -1)
  hora_action =
      create_action({:type => :hora, :target => action.actor, :pai => pais[-1]})
  return shanten_analysis.shanten == -1 &&
      @game.get_hora(hora_action, {:previous_action => action}).valid? &&
      (hora_type == :tsumo || !self.furiten?)
end

#can_reach?(shanten_analysis = nil) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
# File 'lib/mjai/player.rb', line 166

def can_reach?(shanten_analysis = nil)
  shanten_analysis ||= ShantenAnalysis.new(@tehais, 0)
  return @game.current_action.type == :tsumo &&
      @game.current_action.actor == self &&
      shanten_analysis.shanten <= 0 &&
      @furos.empty? &&
      !self.reach? &&
      self.game.num_pipais >= 4 &&
      @score >= 1000
end

#can_ryukyoku?Boolean

Returns:

  • (Boolean)


196
197
198
199
200
201
# File 'lib/mjai/player.rb', line 196

def can_ryukyoku?
  return @game.current_action.type == :tsumo &&
      @game.current_action.actor == self &&
      @game.first_turn? &&
      @tehais.select(){ |pai| pai.yaochu? }.uniq().size >= 9
end

#contextObject



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/mjai/player.rb', line 379

def context
  return Context.new({
    :oya => self == self.game.oya,
    :bakaze => self.game.bakaze,
    :jikaze => self.jikaze,
    :doras => self.game.doras,
    :uradoras => [],  # TODO
    :reach => self.reach?,
    :double_reach => false,  # TODO
    :ippatsu => false,  # TODO
    :rinshan => false,  # TODO
    :haitei => self.game.num_pipais == 0,
    :first_turn => false,  # TODO
    :chankan => false,  # TODO
  })
end

#create_action(params = {}) ⇒ Object



402
403
404
# File 'lib/mjai/player.rb', line 402

def create_action(params = {})
  return Action.new({:actor => self}.merge(params))
end

#delete_tehai(pai) ⇒ Object



396
397
398
399
400
# File 'lib/mjai/player.rb', line 396

def delete_tehai(pai)
  pai_index = @tehais.index(pai) || @tehais.index(Pai::UNKNOWN)
  raise("trying to delete %p which is not in tehais: %p" % [pai, @tehais]) if !pai_index
  @tehais.delete_at(pai_index)
end

#double_reach?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/mjai/player.rb', line 32

def double_reach?
  return @double_reach
end

#furiten?Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
# File 'lib/mjai/player.rb', line 157

def furiten?
  return false if @tehais.size % 3 != 1
  return false if @tehais.include?(Pai::UNKNOWN)
  tenpai_info = TenpaiAnalysis.new(@tehais)
  return false if !tenpai_info.tenpai?
  anpais = self.anpais
  return tenpai_info.waited_pais.any?(){ |pai| anpais.include?(pai) }
end

#get_pais_combinations(target_pais, source_pais) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/mjai/player.rb', line 305

def get_pais_combinations(target_pais, source_pais)
  return Set.new([[]]) if target_pais.empty?
  result = Set.new()
  for pai in source_pais.select(){ |pai| target_pais[0].same_symbol?(pai) }.uniq
    new_source_pais = source_pais.dup()
    new_source_pais.delete_at(new_source_pais.index(pai))
    for cdr_pais in get_pais_combinations(target_pais[1..-1], new_source_pais)
      result.add(([pai] + cdr_pais).sort())
    end
  end
  return result
end

#inspectObject



410
411
412
# File 'lib/mjai/player.rb', line 410

def inspect
  return "\#<%p:%p>" % [self.class, self.id]
end

#ippatsu_chance?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/mjai/player.rb', line 36

def ippatsu_chance?
  return @ippatsu_chance
end

#jikazeObject



145
146
147
148
149
150
151
# File 'lib/mjai/player.rb', line 145

def jikaze
  if @game.oya
    return Pai.new("t", 1 + (4 + @id - @game.oya.id) % 4)
  else
    return nil
  end
end

#kuikae_dahais(action = @game.current_action, tehais = @tehais) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/mjai/player.rb', line 347

def kuikae_dahais(action = @game.current_action, tehais = @tehais)
  consumed = action.consumed ? action.consumed.sort() : nil
  if action.type == :chi && action.actor == self
    if consumed[1].number == consumed[0].number + 1
      forbidden_rnums = [-1, 2]
    else
      forbidden_rnums = [1]
    end
  elsif action.type == :pon && action.actor == self
    forbidden_rnums = [0]
  else
    forbidden_rnums = []
  end
  if forbidden_rnums.empty?
    return []
  else
    key_pai = consumed[0]
    return tehais.uniq().select() do |pai|
      pai.type == key_pai.type &&
          forbidden_rnums.any?(){ |rn| key_pai.number + rn == pai.number }
    end
  end
end

#possible_actionsObject

Possible actions except for dahai.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mjai/player.rb', line 204

def possible_actions
  action = @game.current_action
  result = []
  if (action.type == :tsumo && action.actor == self) ||
      ([:dahai, :kakan].include?(action.type) && action.actor != self)
    if can_hora?
      result.push(create_action({
          :type => :hora,
          :target => action.actor,
          :pai => action.pai,
      }))
    end
    if can_reach?
      result.push(create_action({:type => :reach}))
    end
    if can_ryukyoku?
      result.push(create_action({:type => :ryukyoku, :reason => :kyushukyuhai}))
    end
  end
  result += self.possible_furo_actions
  return result
end

#possible_dahais(action = @game.current_action, tehais = @tehais) ⇒ Object



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

def possible_dahais(action = @game.current_action, tehais = @tehais)

  if self.reach? && action.type == :tsumo && action.actor == self

    # Only tsumogiri is allowed after reach.
    return [action.pai]

  elsif action.type == :reach

    # Tehais after the dahai must be tenpai just after reach.
    result = []
    for pai in tehais.uniq()
      pais = tehais.dup()
      pais.delete_at(pais.index(pai))
      if ShantenAnalysis.new(pais, 0).shanten <= 0
        result.push(pai)
      end
    end
    return result

  else

    # Excludes kuikae.
    return tehais.uniq() - kuikae_dahais(action, tehais)

  end

end

#possible_dahais_after_furo(action) ⇒ Object



371
372
373
374
375
376
377
# File 'lib/mjai/player.rb', line 371

def possible_dahais_after_furo(action)
  remains = @tehais.dup()
  for pai in action.consumed
    remains.delete_at(remains.index(pai))
  end
  return possible_dahais(action, remains)
end

#possible_furo_actionsObject



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
# File 'lib/mjai/player.rb', line 227

def possible_furo_actions
  
  action = @game.current_action
  result = []
  
  if action.type == :dahai &&
      action.actor != self &&
      !self.reach? &&
      @game.num_pipais > 0
    
    if @game.can_kan?
      for consumed in get_pais_combinations([action.pai] * 3, @tehais)
        result.push(create_action({
          :type => :daiminkan,
          :pai => action.pai,
          :consumed => consumed,
          :target => action.actor
        }))
      end
    end
    for consumed in get_pais_combinations([action.pai] * 2, @tehais)
      result.push(create_action({
        :type => :pon,
        :pai => action.pai,
        :consumed => consumed,
        :target => action.actor
      }))
    end
    if (action.actor.id + 1) % 4 == self.id && action.pai.type != "t"
      for i in 0...3
        target_pais = (((-i)...(-i + 3)).to_a() - [0]).map() do |j|
          Pai.new(action.pai.type, action.pai.number + j)
        end
        for consumed in get_pais_combinations(target_pais, @tehais)
          result.push(create_action({
            :type => :chi,
            :pai => action.pai,
            :consumed => consumed,
            :target => action.actor,
          }))
        end
      end
    end
    # Excludes furos which forces kuikae afterwards.
    result = result.select() do |a|
      a.type == :daiminkan || !possible_dahais_after_furo(a).empty?
    end
    
  elsif action.type == :tsumo &&
      action.actor == self &&
      @game.num_pipais > 0 &&
      @game.can_kan?
    
    for pai in self.tehais.uniq
      same_pais = self.tehais.select(){ |tp| tp.same_symbol?(pai) }
      if same_pais.size >= 4
        if self.reach?
          orig_tenpai = TenpaiAnalysis.new(self.tehais[0...-1])
          new_tenpai = TenpaiAnalysis.new(
              self.tehais.select(){ |tp| !tp.same_symbol?(pai) })
          ok = new_tenpai.tenpai? && new_tenpai.waited_pais == orig_tenpai.waited_pais
        else
          ok = true
        end
        result.push(create_action({:type => :ankan, :consumed => same_pais})) if ok
      end
      pon = self.furos.find(){ |f| f.type == :pon && f.taken.same_symbol?(pai) }
      if pon
        result.push(create_action({:type => :kakan, :pai => pai, :consumed => pon.pais}))
      end
    end
    
  end
  
  return result
  
end

#rankObject



406
407
408
# File 'lib/mjai/player.rb', line 406

def rank
  return @game.ranked_players.index(self) + 1
end

#reach?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mjai/player.rb', line 28

def reach?
  return @reach_state == :accepted
end

#rinshan?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/mjai/player.rb', line 40

def rinshan?
  return @rinshan
end

#tenpai?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/mjai/player.rb', line 153

def tenpai?
  return ShantenAnalysis.new(@tehais, 0).shanten <= 0
end

#update_state(action) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/mjai/player.rb', line 44

def update_state(action)
  
  if @game.previous_action &&
      @game.previous_action.type == :dahai &&
      @game.previous_action.actor != self &&
      action.type != :hora
    @extra_anpais.push(@game.previous_action.pai)
  end
  
  case action.type
    when :start_game
      @id = action.id
      @name = action.names[@id] if action.names
      @score = 25000
      @attributes = OpenStruct.new()
      @tehais = nil
      @furos = nil
      @ho = nil
      @sutehais = nil
      @extra_anpais = nil
      @reach_state = nil
      @reach_ho_index = nil
      @double_reach = false
      @ippatsu_chance = false
      @rinshan = false
    when :start_kyoku
      @tehais = action.tehais[self.id]
      @furos = []
      @ho = []
      @sutehais = []
      @extra_anpais = []
      @reach_state = :none
      @reach_ho_index = nil
      @double_reach = false
      @ippatsu_chance = false
      @rinshan = false
    when :chi, :pon, :daiminkan, :kakan, :ankan
      @ippatsu_chance = false
  end
  
  if action.actor == self
    case action.type
      when :tsumo
        @tehais.push(action.pai)
      when :dahai
        delete_tehai(action.pai)
        @tehais.sort!()
        @ho.push(action.pai)
        @sutehais.push(action.pai)
        @ippatsu_chance = false
        @rinshan = false
        @extra_anpais.clear() if !self.reach?
      when :chi, :pon, :daiminkan, :ankan
        for pai in action.consumed
          delete_tehai(pai)
        end
        @furos.push(Furo.new({
          :type => action.type,
          :taken => action.pai,
          :consumed => action.consumed,
          :target => action.target,
        }))
        if [:daiminkan, :ankan].include?(action.type)
          @rinshan = true
        end
      when :kakan
        delete_tehai(action.pai)
        pon_index =
            @furos.index(){ |f| f.type == :pon && f.taken.same_symbol?(action.pai) }
        raise("should not happen") if !pon_index
        @furos[pon_index] = Furo.new({
          :type => :kakan,
          :taken => @furos[pon_index].taken,
          :consumed => @furos[pon_index].consumed + [action.pai],
          :target => @furos[pon_index].target,
        })
        @rinshan = true
      when :reach
        @reach_state = :declared
        @double_reach = true if @game.first_turn?
      when :reach_accepted
        @reach_state = :accepted
        @reach_ho_index = @ho.size - 1
        @ippatsu_chance = true
    end
  end
  
  if action.target == self
    case action.type
      when :chi, :pon, :daiminkan
        pai = @ho.pop()
        raise("should not happen") if pai != action.pai
    end
  end
  
  if action.scores
    @score = action.scores[self.id]
  end
  
end