Created
April 16, 2023 16:02
-
-
Save audhiaprilliant/0d06a3b760b78de6436a4557606fc9a2 to your computer and use it in GitHub Desktop.
Creating a Monopoly Game Board Simulation with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monopoly simulation | |
def monopoly_simulation( | |
num_player: int, | |
iteration: int, | |
card: bool | |
): | |
# String of players | |
player_str = ['Player ' + str(i) for i in range(1, num_player + 1)] | |
# A dictionary for players | |
obj = { | |
'players': num_player, | |
'iteration': iteration, | |
'simulation': dict([(idx, dict([('iterations', [])])) for idx in player_str]) | |
} | |
# Loop for iterations | |
for turn in range(iteration): | |
# Loop for players | |
for player in player_str: | |
# Length of iterations in each player | |
length_iter = len(obj['simulation'][player]['iterations']) | |
if turn == 0: | |
# Initial move | |
d_last_move = { | |
'subiteration': 0, | |
'dices': [0, 0], | |
'move': { | |
'chance_card': None, | |
'community_card': None, | |
'jail': { | |
'status': False, | |
'number': 0 | |
}, | |
'before': 1, | |
'after': { | |
'real': 1, | |
'updated': None | |
} | |
} | |
} | |
else: | |
# Check latest move | |
d_last_move = obj['simulation'][player]['iterations'][-1]['data'][-1] | |
# CORE ALGORITHM | |
# Template for subiteration | |
subiteration_temp = {'iteration': turn, 'data': [d_last_move]} | |
# Roll the dices | |
dices = random.choices(population = range(1, 7), k = 2) | |
# Subiteration | |
loop = 1 | |
# While loop | |
while (len(np.unique(dices)) == 1) and (loop < 3): | |
# Get the subiteration | |
subiteration_data = move_spaces( | |
dict_latest = subiteration_temp['data'][-1], | |
dices = dices, | |
subiteration = loop, | |
three_times = False, | |
card = card, | |
data_card = data_card, | |
data_space = data_space | |
) | |
# Append the subiteration data | |
subiteration_temp['data'].append(subiteration_data) | |
# Update the indexer | |
dices = random.choices(population = range(1, 7), k = 2) | |
loop += 1 | |
else: | |
# If same numbers are found three times | |
if (len(np.unique(dices)) == 1) and (loop > 2): | |
three_times = True | |
# Next move | |
else: | |
three_times = False | |
# Get the subiteration | |
subiteration_data = move_spaces( | |
dict_latest = subiteration_temp['data'][-1], | |
dices = dices, | |
subiteration = loop, | |
three_times = three_times, | |
card = card, | |
data_card = data_card, | |
data_space = data_space | |
) | |
# Append the subiteration data | |
subiteration_temp['data'].append(subiteration_data) | |
# Remove first element in data of 'subiteration_temp' | |
subiteration_temp['data'] = subiteration_temp['data'][1:] | |
# Append the obj | |
obj['simulation'][player]['iterations'].append(subiteration_temp) | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment