Class: Mjai::ActiveGame
- Inherits:
-
Game
- Object
- Game
- Mjai::ActiveGame
show all
- Defined in:
- lib/mjai/active_game.rb
Constant Summary
collapse
- ACTION_PREFERENCES =
{
:hora => 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, #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.
18
19
20
21
|
# File 'lib/mjai/active_game.rb', line 18
def initialize(players)
super(players.shuffle())
@game_type = :one_kyoku
end
|
Instance Attribute Details
#game_type ⇒ Object
Returns the value of attribute game_type.
23
24
25
|
# File 'lib/mjai/active_game.rb', line 23
def game_type
@game_type
end
|
Instance Method Details
#add_dora ⇒ Object
257
258
259
260
|
# File 'lib/mjai/active_game.rb', line 257
def add_dora()
dora_marker = @wanpais.pop()
do_action({:type => :dora, :dora_marker => dora_marker})
end
|
#choose_actions(actions) ⇒ Object
133
134
135
136
137
138
|
# File 'lib/mjai/active_game.rb', line 133
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/mjai/active_game.rb', line 235
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
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
278
279
280
|
# File 'lib/mjai/active_game.rb', line 278
def expect_response_from?(player)
return true
end
|
#game_finished? ⇒ Boolean
262
263
264
265
266
267
268
269
|
# File 'lib/mjai/active_game.rb', line 262
def game_finished?
if @last
return true
else
@last = true if @game_type == :one_kyoku
return false
end
end
|
#get_final_scores ⇒ Object
271
272
273
274
275
276
|
# File 'lib/mjai/active_game.rb', line 271
def get_final_scores()
deltas = [0, 0, 0, 0]
deltas[self.ranked_players[0].id] = @ag_kyotaku * 1000
return get_scores(deltas)
end
|
#get_scores(deltas) ⇒ Object
282
283
284
|
# File 'lib/mjai/active_game.rb', line 282
def get_scores(deltas)
return (0...4).map(){ |i| self.players[i].score + deltas[i] }
end
|
#mota ⇒ Object
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
|
# File 'lib/mjai/active_game.rb', line 71
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
process_hora(actions)
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
next_actions =
[Action.new({:type => :tsumo, :actor => action.actor, :pai => @pipais.pop()})]
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
actions = next_actions
end
end
end
|
#play ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/mjai/active_game.rb', line 25
def play()
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_kyoku ⇒ Object
43
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
|
# File 'lib/mjai/active_game.rb', line 43
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_ryukyoku()
end
do_action({:type => :end_kyoku})
end
|
#process_hora(actions) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/mjai/active_game.rb', line 140
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),
})
tsumibo = 0
@ag_kyotaku = 0
end
update_oya(actions.any?(){ |a| a.actor == self.oya }, false)
end
|
#process_ryukyoku ⇒ Object
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
|
# File 'lib/mjai/active_game.rb', line 181
def process_ryukyoku()
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
|
#update_oya(renchan, ryukyoku) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/mjai/active_game.rb', line 215
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
126
127
128
129
130
131
|
# File 'lib/mjai/active_game.rb', line 126
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
|