-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRealEstateGame.py
447 lines (336 loc) · 14.1 KB
/
RealEstateGame.py
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# Author: Paola Cernada
# GitHub username: paolacernada
# Date: 6/3/2022
# Description: Create a RealEstateGame class that
# allows two or more individuals to
# play a simplified version of the
# game Monopoly.
class Player:
"""
Player is a class that generates a player and
stores information about the player in order
for the player to participate in a RealEstateGame.
"""
def __init__(self, name, account_balance):
self._name = name
self._account_balance = account_balance
self._is_active = True
self._location = 0
def get_name(self):
"""
A method to get the name of the player.
:return: the player's name.
"""
return self._name
def get_account_balance(self):
"""
A method to check the player's account balance.
:return: the player's account balance.
"""
return self._account_balance
def get_is_active(self):
"""
A method to check whether the player is active.
:return: the status of the player
is_active = True or False
"""
return self._is_active
def get_location(self):
"""
A method to get the location of the player.
:return: the player's location.
"""
return self._location
def set_name(self, name):
"""
A method to set the name of the player.
:param name: the name of the player.
:return: the player's name.
"""
self._name = name
def set_account_balance(self, account_balance):
"""
A method to set the account balance of the player.
:param account_balance: the account balance of the player.
:return: the player's account balance.
"""
self._account_balance = account_balance
def set_is_active(self, is_active):
"""
A method to set the status of the player.
:param is_active: the status of the player.
:return: True if the player is active, otherwise False.
"""
self._is_active = is_active
def set_location(self, location):
"""
A method to set the location of the player.
:param location: the location of the player.
:return: the player's location.
"""
self._location = location
class Space:
"""
Space is a class that generates spaces and saves
information about them, so they may be used in a
RealEstateGame.
"""
def __init__(self, name, rent_amount, can_be_purchased):
self._name = name
self._rent_amount = rent_amount
self._can_be_purchased = can_be_purchased
self._owner = ""
def get_name(self):
"""
A method to retrieve the name of the space.
:return: the name of the space.
"""
return self._name
def get_rent_amount(self):
"""
A method to get the rent for the space.
:return: the rent amount for the space.
"""
return self._rent_amount
def get_can_be_purchased(self):
"""
A method for determining the state of the
space and whether it may be purchased.
:return: True or False.
"""
return self._can_be_purchased
def get_purchase_price(self):
"""
A method for determining the purchasing price
of a particular place.
:return: the cost of buying the given space.
"""
return self._rent_amount * 5
def get_owner(self):
"""
A method to get the owner of a space.
:return: the owner of the space.
"""
return self._owner
def set_name(self, name):
"""
A method to set the name of the space.
:param name: the name of the space.
:return: the name of the space.
"""
self._name = name
def set_rent_amount(self, rent_amount):
"""
A method to set the rent amount of the space.
:param rent_amount: the rent amount for the space.
:return: the rent for the space.
"""
self._rent_amount = rent_amount
def set_can_be_purchased(self, can_be_purchased):
"""
A method to set the status of the
space and whether it may be purchased.
:param can_be_purchased: the status of the space.
:return: True or False.
"""
self._can_be_purchased = can_be_purchased
def set_owner(self, owner):
"""
A method to set the owner of a space.
:param owner: the name of the new owner of the space.
:return: the name of the owner.
"""
self._owner = owner
class RealEstateGame:
"""
RealEstateGame is a class that lets two or more players
play a simplified version of the game Monopoly.
"""
def __init__(self):
self._player_dict = {}
self._space_list = []
self._is_end_game = True
self._money_on_GO = 0
def create_spaces(self, money_on_GO, rent_amounts):
"""
Accepts two parameters: the amount of money supplied
to players when they land on or pass through the "GO"
space, and a 24-int list which are rent amounts.
:param money_on_GO: money Player receives when they land/pass GO.
:param rent_amounts: list of ints, represents rent amount for 24 spaces.
:return: n/a.
"""
MONOPOLY_BOARD = ["Amsterdam", "Athens",
"Barcelona", "Berlin",
"Budapest", "Copenhagen",
"Dublin", "Edinburgh",
"Florence", "Helsinki",
"Lisbon", "London",
"Majorca", "Munich",
"Paris", "Porto",
"Prague", "Reykjavik",
"Rome", "Salamanca",
"Stockholm", "Tallinn",
"Venice", "Zagreb"]
self._money_on_GO = money_on_GO
assert (len(rent_amounts) == len(MONOPOLY_BOARD))
for index in range(len(MONOPOLY_BOARD)):
self._space_list.append(Space(MONOPOLY_BOARD[index], rent_amounts[index], True))
self._space_list.insert(0, Space("GO", 0, False))
def create_player(self, player_name, initial_balance):
"""
Uses two parameters: a distinct name and a starting
account balance.
:param player_name: the name of the player.
:param initial_balance: amount of $ player starts with.
:return: n/a.
"""
self._player_dict[player_name] = Player(player_name, initial_balance)
def get_player_account_balance(self, player_name):
"""
Takes the player's name as an argument and returns
the player's account balance.
:param player_name: the name of the player.
:return: the player's account balance.
"""
return self._player_dict[player_name].get_account_balance()
def get_player_current_position(self, player_name):
"""
Accepts the player's name as an input and returns
the player's current location on the board as an
integer (where the "GO" space is position zero).
:param player_name: the name of the player.
:return: the player's current position on the
board as an integer.
"""
return self._player_dict[player_name].get_location()
def buy_space(self, player_name):
"""
If the player's account balance is larger than the
purchase amount and the slot is not already occupied,
the player buys the space.
:param player_name: the name of the player.
:return: True or False.
"""
player = self._player_dict[player_name]
space = self._space_list[player.get_location()]
# If the player has enough money to buy the space and the space is available, the player buys -
# the space and is set as the owner of the space.
if player.get_account_balance() > space.get_purchase_price() and space.get_can_be_purchased():
player.set_account_balance(player.get_account_balance() - space.get_purchase_price())
space.set_owner(player.get_name())
space.set_can_be_purchased(False)
return True
return False
def move_player(self, player_name, spaces_to_move):
"""
Moves the player from the current location.
:param player_name: the name of the player.
:param spaces_to_move: the number of spaces to move.
:return: n/a.
"""
assert (type(spaces_to_move) is int)
assert (0 < spaces_to_move < 7)
# To prevent a key error when accessing the dictionary.
assert (player_name in self._player_dict)
player = self._player_dict[player_name]
if player.get_account_balance() <= 0:
return
# If player's current location greater than the location to be set.
if player.get_location() > (player.get_location() + spaces_to_move) % 25:
player.set_account_balance(player.get_account_balance() + self._money_on_GO)
# % len(self._space_list) To move from end point to start point.
player.set_location((player.get_location() + spaces_to_move) % len(self._space_list))
# The player location is an int that represents the index on the space_list.
space = self._space_list[player.get_location()]
# If the space cannot be purchased and the owner is not the player and space is not GO.
if not space.get_can_be_purchased() and space.get_owner() != player.get_name() and space.get_name() != "GO":
# Get the space's current owner.
space_owner = self._player_dict[space.get_owner()]
# If the player has enough money to pay the rent due.
if player.get_account_balance() > space.get_rent_amount():
# The current owner receives the rent money from the player.
space_owner.set_account_balance(space_owner.get_account_balance() + space.get_rent_amount())
# The rent owed is deducted from the player's account.
player.set_account_balance(player.get_account_balance() - space.get_rent_amount())
elif player.get_account_balance() < space.get_rent_amount():
# The current owner receives the player's balance (to cover rent), and the player is eliminated.
space_owner.set_account_balance(space_owner.get_account_balance() + player.get_account_balance())
self._player_game_over(player_name)
def check_game_over(self):
"""
If all players except one have a 0 account balance,
the game is ended.
:return: the name of the winner if the game is over
otherwise, returns an empty string.
"""
counter = 0
winner = ""
for player in self._player_dict:
if self._player_dict[player].get_account_balance() <= 0:
counter += 1
else:
winner = self._player_dict[player].get_name()
if len(self._player_dict) == counter + 1:
return winner
return ""
def _player_game_over(self, player_name):
"""
If the player's balance is 0, the player loses the game,
and must be removed as the owner of any spaces.
:param player_name: the name of the player.
:return: n/a.
"""
player = self._player_dict[player_name]
for space in self._space_list:
if space.get_owner() == player_name:
space.set_owner("")
space.set_can_be_purchased(True)
player.set_account_balance(0)
player.set_is_active(False)
def main():
"""
A main function to interactively test/play the RealEstateGame :-)
"""
game = RealEstateGame()
rents = [50, 50, 50, 75, 75, 75, 100, 100, 100, 150, 150, 150, 200, 200, 200, 250, 250, 250, 300, 300, 300, 350,
350, 350]
game.create_spaces(50, rents)
player_list = ["PowerGirl", "AstroBoy", "BlueLagoon"]
game.create_player("PowerGirl", 290)
game.create_player("AstroBoy", 290)
game.create_player("BlueLagoon", 290)
turn = 0
winner = ""
while winner == "":
for player in player_list:
if game.get_player_account_balance(player) != 0:
turn += 1
print("Active Player: ", player, " Turn #", turn)
number_of_moves = 0
while number_of_moves < 1 or number_of_moves > 6:
number_of_moves = int(input("How many spaces will you move 1-6? \n"))
if number_of_moves < 1 or number_of_moves > 6:
print("Invalid input. Please, try again.")
game.move_player(player, number_of_moves)
if game.get_player_current_position(player) != 0:
buy_property = "invalid"
while buy_property.lower() != "y" and buy_property.lower() != "n":
buy_property = input("Would you like to buy this space? [Y / N] \n")
if buy_property.lower() != "y" and buy_property.lower() != "n":
print("Invalid answer. Please, try again.")
if buy_property.lower() == "y":
is_purchased = game.buy_space(player)
if is_purchased:
print("Congratulations on your new purchase!\n")
else:
print("Sorry, invalid action!\n")
if game.get_player_account_balance(player) == 0:
print("In-Game Update:")
print(player, "is out of the game!\n")
print(player, ": Current position: ", game.get_player_current_position(player))
print(player, ": Account balance: ", game.get_player_account_balance(player), "\n")
winner = game.check_game_over()
print(winner, ", you win!")
if __name__ == "__main__":
main()