r/pythonhelp • u/mach0-nach0 • Jun 12 '24
In need of assistance from the community on my beginner project.
Amateur Python tinkerer here expanding my knowledge and building my skills.
Essentially, I'm creating a small procedural-generated text-based game. The only 'graphical representation' is a small ASCII map that will mark the location of the player and surrounding towns, land-features, etc.
I'm having issues with my town generation as I would like there to be 'padding' between my towns to keep them from stacking on the same row/column in my 2D matrix. Each town generates a random X and Y value within a While Loop and 'scan' nearby locations within a certain range (padding) to check if a town_symbol is already there. If so, it passes and assigns new random values to X and Y and reruns the 'check'. Else, it inserts the town_symbol and breaks from the while loop, repeating for each town.
I can't seem to get towns to NOT stack on either the X or Y axis. Help is greatly appreciated!
If further info is needed I'm happy to oblige.
Any and all feedback on code quality is HIGHLY appreciated as I'm considering a career change into Python development in the future. Thanks!!
Code:
•
u/AutoModerator Jun 12 '24
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/SCM456 Jun 12 '24
I'm an amateur too (this is my first time on this sub, I have an issue of my own) so there might be a more efficient way of doing this but I just created a list of all the possible neighboring points around the 9x9 square that a town is on (in a nested for loop), and made it only place a town if it doesn't find any towns in that list of neighbors. The x_vals list isn't needed with this method, unless you want to use it for something else later down the line. Feel free to change any variable names I chose and comments or whatever.
Here is the code:
The reason why the range on the for loops is (-1, 2) instead of (-1, 1) is because 'range' doesn't include the actual written endpoint value so you need to set the end of the range to one above the endpoint if you want it included.
If you want, this method allows you to increase the distance between towns to a larger area, as long as the spaces don't get so big that you can't fit your chosen number of towns in. For example if you set both of the for loops to range(-3, 4) then no two towns will be in the same 7x7 area. Increasing the total number of points or reducing the number of towns would give you the ability to have the towns even more spaced out.
Similarly if you changed the range of one for loop but not the other, you could even change the shape of the area that no two towns can coexist in. For example, with a map size of World_Map(16,8), by putting range(-10, 11) on the first for loop and range(-1, 2) on the second for loop, you create a 'no neighbor zone' the shape of a rectangle that extends all the way across the y axis which results in no two towns ever existing above or below the other.
I hope this helps.