Class: Mjai::ActiveGame

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

Constant Summary collapse

ACTION_PREFERENCES =
{
    :hora => 4,
    :ryukyoku => 3,
    :pon => 2,
    :daiminkan => 2,
    :chi => 1,
}

Instance Attribute Summary collapse

Attributes inherited from Game

#all_pais, #bakaze, #current_action, #dora_markers, #honba, #last, #num_pipais, #oya, #players, #previous_action

Instance Method Summary collapse

Methods inherited from Game

#action_in_view, #can_kan?, #distance, #do_action, #doras, #dump_action, #first_turn?, #get_hora, #on_action, #on_responses, #ranked_players, #render_board, #validate, #validate_fields_exist, #validate_response_content, #validate_response_type, #validate_responses

Constructor Details

#initialize(players) ⇒ ActiveGame

Returns a new instance of ActiveGame.



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

def initialize(players)
  super(players.shuffle())
  @game_type = :one_kyoku
end

Instance Attribute Details

#game_typeObject

Returns the value of attribute game_type.



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

def game_type
  @game_type
end

Instance Method Details

#add_doraObject



313
314
315
316
# File 'lib/mjai/active_game.rb', line 313

def add_dora()
  dora_marker = @wanpais.pop()
  do_action({:type => :dora, :dora_marker => dora_marker})
end

#check_ryukyokuObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mjai/active_game.rb', line 140

def check_ryukyoku()
  if players.all?(){ |pl| pl.reach? }
    process_ryukyoku(:suchareach)
    throw(:end_kyoku)
  end
  if first_turn? && !players[0].sutehais.empty? && players[0].sutehais[0].fonpai? &&
      players.all?(){ |pl| pl.sutehais == [players[0].sutehais[0]] }
    process_ryukyoku(:sufonrenta)
    throw(:end_kyoku)
  end
  kan_counts = players.map(){ |pl| pl.furos.count(){ |f| f.kan? } }
  if kan_counts.inject(0){ |total, n| total + n } == 4 && !kan_counts.include?(4)
    process_ryukyoku(:sukaikan)
    throw(:end_kyoku)
  end
end

#choose_actions(actions) ⇒ Object



164
165
166
167
168
169
# File 'lib/mjai/active_game.rb', line 164

def choose_actions(actions)
  actions = actions.select(){ |a| a }
  max_pref = actions.map(){ |a| ACTION_PREFERENCES[a.type] || 0 }.max
  max_actions = actions.select(){ |a| (ACTION_PREFERENCES[a.type] || 0) == max_pref }
  return max_actions
end

#decide_last(last_bakaze, tenpai_renchan) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/mjai/active_game.rb', line 291

def decide_last(last_bakaze, tenpai_renchan)
  if @players.any? { |pl| pl.score < 0 }
    return true
  end

  if @ag_bakaze == last_bakaze.succ.succ
    return true
  end
  if @ag_bakaze == last_bakaze.succ
    return @players.any? { |pl| pl.score >= 30000 }
  end

  # Agari-yame, tenpai-yame
  if @ag_bakaze == last_bakaze && @ag_oya == @players[3] &&
      tenpai_renchan && @players[3].score >= 30000 &&
      (0..2).all? { |i| @players[i].score < @players[3].score }
    return true
  end

  return false
end

#expect_response_from?(player) ⇒ Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/mjai/active_game.rb', line 334

def expect_response_from?(player)
  return true
end

#game_finished?Boolean

Returns:

  • (Boolean)


318
319
320
321
322
323
324
325
# File 'lib/mjai/active_game.rb', line 318

def game_finished?
  if @last
    return true
  else
    @last = true if @game_type == :one_kyoku
    return false
  end
end

#get_final_scoresObject



327
328
329
330
331
332
# File 'lib/mjai/active_game.rb', line 327

def get_final_scores()
  # The winner takes remaining kyotaku.
  deltas = [0, 0, 0, 0]
  deltas[self.ranked_players[0].id] = @ag_kyotaku * 1000
  return get_scores(deltas)
end

#get_scores(deltas) ⇒ Object



338
339
340
# File 'lib/mjai/active_game.rb', line 338

def get_scores(deltas)
  return (0...4).map(){ |i| self.players[i].score + deltas[i] }
end

#motaObject

摸打



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

def mota()
  reach_pending = false
  kandora_pending = false
  tsumo_actor = @actor
  actions = [Action.new({:type => :tsumo, :actor => @actor, :pai => @pipais.pop()})]
  while !actions.empty?
    if actions[0].type == :hora
      if actions.size >= 3
        process_ryukyoku(:sanchaho, actions.map(){ |a| a.actor })
      else
        process_hora(actions)
      end
      throw(:end_kyoku)
    elsif actions[0].type == :ryukyoku
      raise("should not happen") if actions.size != 1
      process_ryukyoku(:kyushukyuhai, [actions[0].actor])
      throw(:end_kyoku)
    else
      raise("should not happen") if actions.size != 1
      action = actions[0]
      responses = do_action(action)
      next_actions = nil
      case action.type
        when :daiminkan, :kakan, :ankan
          if action.type == :ankan
            add_dora()
          end
          # Actually takes one from wanpai and moves one pai from pipai to wanpai,
          # but it's equivalent to taking from pipai.
          next_actions =
            [Action.new({:type => :tsumo, :actor => action.actor, :pai => @pipais.pop()})]
          # TODO Handle 4 kans.
        when :reach
          reach_pending = true
      end
      next_actions ||= choose_actions(responses)
      if reach_pending &&
          (next_actions.empty? || ![:dahai, :hora].include?(next_actions[0].type))
        @ag_kyotaku += 1
        deltas = [0, 0, 0, 0]
        deltas[tsumo_actor.id] = -1000
        do_action({
            :type => :reach_accepted,
            :actor => tsumo_actor,
            :deltas => deltas,
            :scores => get_scores(deltas),
        })
        reach_pending = false
      end
      if kandora_pending &&
          !next_actions.empty? && [:dahai, :tsumo].include?(next_actions[0].type)
        add_dora()
        kandora_pending = false
      end
      if [:daiminkan, :kakan].include?(action.type)
        kandora_pending = true
      end
      if action.type == :dahai && (next_actions.empty? || next_actions[0].type != :hora)
        check_ryukyoku()
      end
      actions = next_actions
    end
  end
end

#playObject



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

def play()
  if ![:one_kyoku, :tonpu, :tonnan].include?(@game_type)
    raise("Unknown game_type")
  end
  begin
    do_action({:type => :start_game, :names => self.players.map(){ |pl| pl.name }})
    @ag_oya = @ag_chicha = @players[0]
    @ag_bakaze = Pai.new("E")
    @ag_honba = 0
    @ag_kyotaku = 0
    while !self.game_finished?
      play_kyoku()
    end
    do_action({:type => :end_game, :scores => get_final_scores()})
    return true
  rescue ValidationError => ex
    do_action({:type => :error, :message => ex.message})
    return false
  end
end

#play_kyokuObject



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

def play_kyoku()
  catch(:end_kyoku) do
    @pipais = @all_pais.shuffle()
    @pipais.shuffle!()
    @wanpais = @pipais.pop(14)
    dora_marker = @wanpais.pop()
    tehais = Array.new(4){ @pipais.pop(13).sort() }
    do_action({
        :type => :start_kyoku,
        :bakaze => @ag_bakaze,
        :kyoku => (4 + @ag_oya.id - @ag_chicha.id) % 4 + 1,
        :honba => @ag_honba,
        :kyotaku => @ag_kyotaku,
        :oya => @ag_oya,
        :dora_marker => dora_marker,
        :tehais => tehais,
    })
    @actor = self.oya
    while !@pipais.empty?
      mota()
      @actor = @players[(@actor.id + 1) % 4]
    end
    process_fanpai()
  end
  do_action({:type => :end_kyoku})
end

#process_fanpaiObject



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

def process_fanpai()
  tenpais = []
  tehais = []
  for player in players
    if player.tenpai?
      tenpais.push(true)
      tehais.push(player.tehais)
    else
      tenpais.push(false)
      tehais.push([Pai::UNKNOWN] * player.tehais.size)
    end
  end
  tenpai_ids = (0...4).select(){ |i| tenpais[i] }
  noten_ids = (0...4).select(){ |i| !tenpais[i] }
  deltas = [0, 0, 0, 0]
  if (1..3).include?(tenpai_ids.size)
    for id in tenpai_ids
      deltas[id] += 3000 / tenpai_ids.size
    end
    for id in noten_ids
      deltas[id] -= 3000 / noten_ids.size
    end
  end
  do_action({
      :type => :ryukyoku,
      :reason => :fanpai,
      :tenpais => tenpais,
      :tehais => tehais,
      :deltas => deltas,
      :scores => get_scores(deltas),
  })
  update_oya(tenpais[self.oya.id], true)
end

#process_hora(actions) ⇒ Object



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

def process_hora(actions)
  tsumibo = self.honba
  for action in actions.sort_by(){ |a| distance(a.actor, a.target) }
    uradora_markers = action.actor.reach? ? @wanpais.pop(self.dora_markers.size) : []
    hora = get_hora(action, {
        :uradora_markers => uradora_markers,
        :previous_action => self.previous_action,
    })
    raise("no yaku") if !hora.valid?
    deltas = [0, 0, 0, 0]
    deltas[action.actor.id] += hora.points + tsumibo * 300 + @ag_kyotaku * 1000
    if hora.hora_type == :tsumo
      for player in self.players
        next if player == action.actor
        deltas[player.id] -=
            ((player == self.oya ? hora.oya_payment : hora.ko_payment) + tsumibo * 100)
      end
    else
      deltas[action.target.id] -= (hora.points + tsumibo * 300)
    end
    do_action({
      :type => action.type,
      :actor => action.actor,
      :target => action.target,
      :pai => action.pai,
      :hora_tehais => action.actor.tehais,
      :uradora_markers => uradora_markers,
      :yakus => hora.yakus,
      :fu => hora.fu,
      :fan => hora.fan,
      :hora_points => hora.points,
      :deltas => deltas,
      :scores => get_scores(deltas),
    })
    # Only kamicha takes them in case of daburon.
    tsumibo = 0
    @ag_kyotaku = 0
  end
  update_oya(actions.any?(){ |a| a.actor == self.oya }, false)
end

#process_ryukyoku(reason, actors = []) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/mjai/active_game.rb', line 212

def process_ryukyoku(reason, actors=[])
  actor = (reason == :kyushukyuhai) ? actors[0] : nil
  tenpais = []
  tehais = []
  for player in players
    if reason == :suchareach || actors.include?(player)  # :sanchaho, :kyushukyuhai
      tenpais.push(reason != :kyushukyuhai)
      tehais.push(player.tehais)
    else
      tenpais.push(false)
      tehais.push([Pai::UNKNOWN] * player.tehais.size)
    end
  end
  do_action({
      :type => :ryukyoku,
      :actor => actor,
      :reason => reason,
      :tenpais => tenpais,
      :tehais => tehais,
      :deltas => [0, 0, 0, 0],
      :scores => players.map(){ |player| player.score }
  })
  update_oya(true, true)
end

#update_oya(renchan, ryukyoku) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/mjai/active_game.rb', line 271

def update_oya(renchan, ryukyoku)
  if renchan
    @ag_oya = self.oya
  else
    @ag_oya = @players[(self.oya.id + 1) % 4]
    @ag_bakaze = @ag_bakaze.succ if @ag_oya == @players[0]
  end
  if renchan || ryukyoku
    @ag_honba += 1
  else
    @ag_honba = 0
  end
  case @game_type
    when :tonpu
      @last = decide_last(Pai.new("E"), renchan)
    when :tonnan
      @last = decide_last(Pai.new("S"), renchan)
  end
end

#update_state(action) ⇒ Object



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

def update_state(action)
  super(action)
  if action.type == :tsumo && @pipais.size != self.num_pipais
    raise("num pipais mismatch: %p != %p" % [@pipais.size, self.num_pipais])
  end
end