Skip to content

Instantly share code, notes, and snippets.

@audhiaprilliant
Created April 16, 2023 16:00
Show Gist options
  • Save audhiaprilliant/4b8099548c54b09195f29c241566f7c3 to your computer and use it in GitHub Desktop.
Save audhiaprilliant/4b8099548c54b09195f29c241566f7c3 to your computer and use it in GitHub Desktop.
Creating a Monopoly Game Board Simulation with Python
# Select a card and show the result
def take_cards(
subiteration_dict: dict,
card: bool,
chance_card: bool,
data_card: dict,
data_space: dict
):
# Card status
card_status = card
# Card type
card = 'community_card'
# Chance or community chest card
if chance_card:
# Update the card type
card = 'chance_card'
# Select the card
chance_cards = data_card['chance_card']
selected_card = random.choice(chance_cards)
else:
# Select the card
community_cards = data_card['community_card']
selected_card = random.choice(community_cards)
# CORE ALGORITHM
# Update the card status
subiteration_dict['move'][card] = selected_card['number']
# Run the following command if card is used
if card_status:
# Move the space
if selected_card['action'] in ['move', 'move_money']:
# Last space
last_space = subiteration_dict['move']['after']['real']
if subiteration_dict['move']['after']['updated'] != None:
last_space = subiteration_dict['move']['after']['updated']
# Properties
property_index = selected_card['move']['properties']
# Move few step
if selected_card['move']['properties'] == None:
# Steps
step = selected_card['move']['step']
# Multiplier
multiplier = -1
# Update the multiplier
if selected_card['move']['forward']:
multiplier = 1
# Current space
d_current_space_card = update_space(
last_move = last_space,
dices = (multiplier * step),
data_space = data_space
)
# Update the last space
if card:
subiteration_dict['move']['after']['updated'] = d_current_space_card[0]['order']
subiteration_dict['move']['forward']['updated'] = selected_card['move']['forward']
else:
pass
# Move to the specific space
else:
# Go to nearest property (998: utilities, 999: stations)
if property_index in [998, 999]:
# Get a list of spaces
targets = {
998: [13, 29],
999: [6, 16, 26, 36]
}
# Get the shortest space
update_index, forward_stat = shortest_space(
source = last_space,
targets = targets[property_index],
data_space = data_space
)
# Update the last space
if card:
subiteration_dict['move']['after']['updated'] = update_index
subiteration_dict['move']['forward']['updated'] = forward_stat
else:
pass
# Go to specific property
else:
# Update the last space
if card:
subiteration_dict['move']['after']['updated'] = property_index
subiteration_dict['move']['forward']['updated'] = True
else:
pass
# Run the following command if card is not used
else:
pass
return subiteration_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment