-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.e
executable file
·407 lines (369 loc) · 10.4 KB
/
player.e
1
2
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
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
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
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
313
314
315
316
317
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
class
PLAYER
inherit
MONOPOLY_OBJECT
create
make
feature {NONE} -- Initialization
make (n: STRING; m: INTEGER; t: TOKEN; color: EV_COLOR; b: MONOPOLY_BOARD)
-- Create a player with name `n' and `m' CHF.
require
name_exists: n /= Void and then not n.is_empty
m_positive: m >= 0
b_exists: b /= Void and then b.Square_count > 0
token_exist: t /= Void
do
name := n.twin
money := m
board := b
token := t
Monopoly_view.add_token (token.view)
create info_view.make_with_player (Current, color)
info_view.house_option_button.select_actions.extend (agent house_options)
create properties.make (0)
ensure
name_set: name ~ n
money_set: money = m
t /= Void
end
feature -- Access
name: STRING
-- Player name.
position: INTEGER
-- Current position on the board.
money: INTEGER
-- Current money of the player
has_lost: BOOLEAN
board: MONOPOLY_BOARD
-- Board of the current game
turns_left_in_jail: INTEGER
-- Turns the player must still wait before going out of jail
properties: ARRAYED_SET [PROPERTY_SQUARE]
-- Properties the player own
token: TOKEN
-- Token of the player
info_view: PLAYER_INFO_VIEW
feature -- Moving
set_position (pos: INTEGER)
-- Set position to `pos'.
require
correct_position: pos >= 0 and pos - board.Square_count <= board.Square_count
do
if pos > board.Square_count then
position := pos - board.Square_count
else
position := pos
end
token.move_to_position (position)
ensure
correct_position: position >= 1 and position <= board.Square_count
end
move (dice_value: INTEGER)
-- Move the player and execute action of the square
require
correct_value: dice_value >= 2 and dice_value <= 12
local
new_position: INTEGER
do
new_position := position + dice_value
set_position (new_position)
if new_position > board.Square_count then
add_money (150)
Monopoly_view.break ("You pass through go, you collect 150 CHF")
end
board.squares.item (position).do_action (Current)
end
feature -- Basic operations
play (d1, d2: DIE; double_count: INTEGER)
-- Play a turn with dice `d1', `d2'.
require
dice_exist: d1 /= Void and d2 /= Void
double_count >= 1 and double_count <= 3
do
if not has_lost then
Game.set_current_player (Current)
info_view.update_label
info_view.roll_dice_button.hide
info_view.roll_dice_button.select_actions.wipe_out
info_view.sell_property_button.hide
info_view.sell_property_button.select_actions.wipe_out
info_view.pay_fine_button.hide
info_view.pay_fine_button.select_actions.wipe_out
Monopoly_view.clean_console
d1.roll
d2.roll
if d1.face_value = d2.face_value and double_count = 3 then
Monopoly_view.break ("Third double, you go directly to jail !")
put_in_jail
elseif turns_left_in_jail = 0 then
move (d1.face_value + d2.face_value)
else
if d1.face_value = d2.face_value then
Monopoly_view.break ("Double ! You are lucky today and you go out of jail !")
turns_left_in_jail := 0
move (d1.face_value + d2.face_value)
else
if turns_left_in_jail = 1 then
pay_fine
move (d1.face_value + d2.face_value)
else
turns_left_in_jail := turns_left_in_jail - 1
Monopoly_view.break ("Still " + (turns_left_in_jail - 1).out + " turns in jail.")
end
end
end
end
finish_turn (d1, d2, double_count)
end
finish_turn (d1, d2: DIE; double_count: INTEGER)
-- Set things up for the next turn
local
next_player: PLAYER
player_index: INTEGER
do
info_view.update_label
if d1.face_value = d2.face_value and turns_left_in_jail = 0 and not has_lost then
Monopoly_view.break ("You had a double, you play again !")
info_view.roll_dice_button.show
info_view.roll_dice_button.select_actions.extend (agent play(d1, d2, double_count + 1))
if properties.count >= 1 then
info_view.sell_property_button.show
info_view.sell_property_button.select_actions.extend (agent sell_property_options)
end
else
player_index := Game.players.index_of (Current, 1)
if player_index = Game.players.count then
next_player := Game.players.i_th (1)
else
next_player := Game.players.i_th (player_index + 1)
end
Monopoly_view.print_text ("" + next_player.name + "'s turn.")
next_player.info_view.roll_dice_button.show
next_player.info_view.roll_dice_button.select_actions.extend (agent next_player.play(d1, d2, 1))
if next_player.properties.count >= 1 then
next_player.info_view.sell_property_button.show
next_player.info_view.sell_property_button.select_actions.extend (agent next_player.sell_property_options)
end
if next_player.turns_left_in_jail >= 1 then
next_player.jail_options
end
end
if has_lost then
if Game.players.count = 2 then
info_view.remove
Monopoly_view.remove_token (token.view)
Game.players.prune_all (Current)
Game.players.start
Game.set_winner (Game.players.item)
Monopoly_view.break ("The winner is " + Game.winner.name)
Game.end_game
else
info_view.remove
Monopoly_view.remove_token (token.view)
Game.players.prune_all (Current)
end
end
end
add_money (m: INTEGER)
-- Add `m' to player's money
do
if money + m >= 0 then
money := money + m
info_view.update_label
else
if properties.count /= 0 then
recover_actions (- m)
if not has_lost and money >= m then
money := money + m
else
has_lost := True
end
else
has_lost := True
end
end
end
pay_to_player (m: INTEGER; p: PLAYER)
-- Pay `m' to player `p'
require
p /= Void
m >= 0
do
if money >= m then
add_money (- m)
p.add_money (m)
else
if properties.count /= 0 then
recover_actions (m)
if money >= m then
add_money (- m)
p.add_money (m)
else
p.add_money (money)
add_money (- money)
has_lost := True
end
else
add_money (- money)
p.add_money (money)
has_lost := True
end
end
end
add_property (p: PROPERTY_SQUARE)
-- Add `p' to player's properties
require
p /= Void
do
properties.extend (p)
end
put_in_jail
-- The player will be in jail for 3 turns
do
turns_left_in_jail := 4
set_position (board.jail_position)
ensure
turns_left_in_jail = 4
end
recover_actions (m: INTEGER)
-- When a player is in dept, he can try to sell his houses and properties
require
money < m
local
modal: LOST_DIALOG
do
create modal
modal.add_player_info (Current, m)
modal.show_modal_to_window (Monopoly_view.main_window)
if money < m and properties.count /= 0 then
if modal.selected_button ~ "Abandon" then
has_lost := True
end
if not (modal.selected_button ~ "Abandon" and Game.players.count <= 2) then
Monopoly_view.break ("Your properties will be sold to the best bidder")
from
properties.start
until
properties.after
loop
properties.item.sell_to_best_bidder
properties.forth
end
end
if properties.count /= 0 then
from
properties.start
until
properties.count = 0
loop
properties.item.remove_owner
properties.remove
properties.start
end
end
end
end
jail_options
-- Actions to perform when player is in jail
require
turns_left_in_jail >= 1
do
info_view.pay_fine_button.show
info_view.pay_fine_button.select_actions.extend (agent pay_fine)
info_view.pay_fine_button.select_actions.extend (agent play(Game.die_1, Game.die_2, 1))
end
pay_fine
-- Pay the fine to go out of jail
require
turns_left_in_jail >= 1
do
add_money (-50)
turns_left_in_jail := 0
Monopoly_view.break ("You pay a 50 CHF fine")
end
house_options
-- Let the player proceed to option regarding his houses
local
property: PROPERTY_SQUARE
modal: HOUSES_DIALOG
do
if properties.count >= 1 and properties.to_array.there_exists (agent can_have_houses(?)) then
create modal
modal.add_info_from_player (Current)
modal.show_modal_to_window (Monopoly_view.main_window)
if modal.selected_button /= Void and then modal.selected_button ~ (create {EV_DIALOG_CONSTANTS}).ev_ok then
property := modal.selected_property
if modal.buy_button.is_selected then
property.add_house (modal.house_number.value)
elseif modal.sell_button.is_selected then
property.sell_house (modal.house_number.value)
end
end
else
Monopoly_view.break ("You are unable to add houses")
end
end
sell_property_options
-- Let the player sell properties
require
properties.count >= 1
local
property: PROPERTY_SQUARE
modal: PROPERTY_DIALOG
bid_result: TUPLE [player: PLAYER; price: INTEGER]
do
create modal
modal.add_info_from_player (Current)
modal.show_modal_to_window (Monopoly_view.main_window)
if modal.selected_button /= Void and then modal.selected_button ~ (create {EV_DIALOG_CONSTANTS}).ev_ok then
property := modal.selected_property
bid_result := property.bid
if bid_result /= Void and bid_result.player /= Void then
if property.house_count >= 1 then
property.sell_house (property.house_count)
end
bid_result.player.pay_to_player (bid_result.price, Current)
property.set_owner (bid_result.player)
properties.prune_all (property)
bid_result.player.properties.extend (property)
Monopoly_view.break ("You sold " + property.name + " to " + bid_result.player.name + " for " + bid_result.price.out + " CHF")
if properties.count = 0 then
info_view.sell_property_button.select_actions.wipe_out
info_view.sell_property_button.hide
end
end
end
end
sell_property
-- Let the user sell a property
require
properties.count > 0
do
end
feature {NONE} -- Implementation
has_money (p: PLAYER): BOOLEAN
-- Checks if a player has still money
require
p /= Void
do
Result := p.money > 0
end
can_have_houses (p: PROPERTY_SQUARE): BOOLEAN
-- Checks if the player is able to add houses to `p'
require
p /= Void
do
if money >= p.house_price or p.house_count >= 1 then
if p.linked_square = Void or else properties.has (p.linked_square) then
Result := True
else
Result := False
end
else
Result := False
end
end
invariant
name_exists: name /= Void and then not name.is_empty
turns_positive: turns_left_in_jail >= 0
end