Skip to content

Instantly share code, notes, and snippets.

@audhiaprilliant
Created April 16, 2023 16:01
Show Gist options
  • Save audhiaprilliant/b179b3b5237a5b2c339833121154b243 to your computer and use it in GitHub Desktop.
Save audhiaprilliant/b179b3b5237a5b2c339833121154b243 to your computer and use it in GitHub Desktop.
Creating a Monopoly Game Board Simulation with Python
# Player(s) movement
def move_spaces(
dict_latest: dict,
dices: int,
subiteration: int,
three_times: bool,
card: bool,
data_card: dict,
data_space: dict
):
# Template of players' iteration
template_iter = {
'subiteration': subiteration,
'dices': dices,
'move': {
'chance_card': None,
'community_card': None,
'jail': {
'status': False,
'number': 0
},
'before': None,
'after': {
'real': None,
'updated': None
},
'forward': {
'real': None,
'updated': None
}
}
}
# Jail status and number
jail_status, jail_trial = dict_latest['move']['jail'].values()
# latest space position
last_move = dict_latest['move']['after']['real']
if dict_latest['move']['after']['updated'] != None:
last_move = dict_latest['move']['after']['updated']
# If player(s) is on jail space
if jail_status:
if (len(np.unique(dices)) != 1):
if (jail_trial < 2):
# Update dictionary
template_iter['move']['jail']['status'] = jail_status
template_iter['move']['jail']['number'] = jail_trial + 1
template_iter['move']['before'] = last_move
template_iter['move']['after']['real'] = last_move
else:
# Current space
d_current_space = update_space(
last_move = last_move,
dices = dices,
data_space = data_space
)
# Update dictionary
template_iter['move']['before'] = last_move
template_iter['move']['after']['real'] = d_current_space[0]['order']
template_iter['move']['forward']['real'] = True
else:
# Current space
d_current_space = update_space(
last_move = last_move,
dices = dices,
data_space = data_space
)
# Update dictionary
template_iter['move']['before'] = last_move
template_iter['move']['after']['real'] = d_current_space[0]['order']
template_iter['move']['forward']['real'] = True
# If player(s) is not on jail space
else:
# If player(s) throw the same number three times
if three_times:
template_iter['move']['jail']['status'] = True
template_iter['move']['after']['real'] = 11
template_iter['move']['before'] = last_move
template_iter['move']['forward']['real'] = False
# Otherwise
else:
# List of space orders based on space type
d_space = {
'community_chest': [space['order'] for space in data_space if space['space_type'] == 'community_chest'],
'chance': [space['order'] for space in data_space if space['space_type'] == 'chance'],
'road': [space['order'] for space in data_space if space['space_type'] == 'road']
}
# Current space
d_current_space = update_space(
last_move = last_move,
dices = dices,
data_space = data_space
)
# Update dictionary
template_iter['move']['before'] = last_move
template_iter['move']['forward']['real'] = True
# Player is on 'Go to Jail' space
if d_current_space[0]['order'] == 31:
# Update dictionary
template_iter['move']['jail']['status'] = True
template_iter['move']['after']['real'] = d_current_space[0]['order']
template_iter['move']['after']['updated'] = 11
template_iter['move']['forward']['real'] = True
template_iter['move']['forward']['updated'] = False
# Player is not on 'Go to Jail' space
else:
# Update dictionary
template_iter['move']['after']['real'] = d_current_space[0]['order']
template_iter['move']['forward']['real'] = True
# Player is on 'Community Chest' space
if d_current_space[0]['order'] in d_space['community_chest']:
# Update dictionary
template_iter = take_cards(
subiteration_dict = template_iter,
card = card,
chance_card = False,
data_card = data_card,
data_space = data_space
)
if d_current_space[0]['order'] in d_space['chance']:
# Update dictionary
template_iter = take_cards(
subiteration_dict = template_iter,
card = card,
chance_card = True,
data_card = data_card,
data_space = data_space
)
else:
pass
return template_iter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment