Created
October 9, 2014 13:43
-
-
Save 1328/cf2efb78362a3ab75baa to your computer and use it in GitHub Desktop.
tiles
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
| import os | |
| import sys | |
| import math | |
| import random | |
| import itertools | |
| import bisect | |
| # Fix Python 2.x. | |
| try: input = raw_input | |
| except NameError: pass | |
| sys.stdout.buffer.write(chr(9986).encode('utf8')) | |
| running = True | |
| ##If chance < math.ceil(random.random()*100) | |
| water_chance = 10 #Chance of ground being water | |
| water_near_chance = 30 #If a water tile is to our left or top | |
| boat_chance = 10 #If we're on a water tile, chance of it being a boat | |
| water_around_boat = 100 #if boat is to top or left, should water generate? | |
| house_chance = 2 #chance of a house being on the ground | |
| ground_tile = "." #0 in matrix | |
| water_tile = "~" #1 in matrix | |
| house_tile = "h" | |
| boat_tile = "b" #only if left and top are water | |
| # good place for a dict | |
| tile_rep = { | |
| 0: ground_tile, | |
| 1: water_tile, | |
| 2: house_tile, | |
| 3: boat_tile, | |
| } | |
| def ran(): | |
| # consider using random.range here | |
| return math.ceil(random.random()*100) | |
| def cls(): | |
| os.system(['clear','cls'][os.name == 'nt']) | |
| def get_piece(score): | |
| # overkill, but useful if more choices were given | |
| # here i just put it in to show you something that is probably new to you | |
| breakpoints = [water_chance] | |
| pieces = (1, 0) | |
| i = bisect.bisect(breakpoints, score) | |
| return pieces[i] | |
| def print_matrix(m): | |
| ''' | |
| look how we use the dict tile_rep to translate the numbers into tile | |
| representations! | |
| ''' | |
| for r in m: | |
| for t in r: | |
| print(tile_rep[t], end='') | |
| print() | |
| # changed to True and use a break instead | |
| while True: | |
| # too lazy to enter input | |
| print("ThatOneCrazyFriend's Simple Map Generator") | |
| #grid = input("Input Grid Size [20x10] ") | |
| grid = '' | |
| if grid == '': | |
| grid = '20x10' | |
| split = grid.partition("x") | |
| if split[1] != "x" or not split[0].isdigit() or not split[2].isdigit(): | |
| print("Input incorrect! Please try again.\n") | |
| continue | |
| print("GENERATED MAP:\n") | |
| width = int(split[0]) | |
| height = int(split[2]) | |
| # the double x is a bit confusing | |
| Matrix = [[0 for w in range(width)] for h in range(height)] | |
| for w,h in itertools.product(range(width), range(height)): | |
| Matrix[h][w] = get_piece(random.randrange(0,101)) | |
| # nothing wrong with try/except loops, but this keeps indents to a | |
| # minimum | |
| if w == 0 or h ==0: | |
| continue | |
| left = w - 1 | |
| up = h - 1 | |
| # a set might be an even better idea | |
| adjacent_tiles = (Matrix[h][left], | |
| Matrix[up][w] | |
| ) | |
| # there is probably a better way to do this... | |
| if 3 in adjacent_tiles: | |
| if water_around_boat > ran(): | |
| Matrix[h][w] = 1 | |
| elif 2 in adjacent_tiles: | |
| Matrix[h][w] = 0 | |
| elif adjacent_tiles == (1,1): | |
| if boat_chance > ran(): | |
| Matrix[h][w] = 3 | |
| elif water_near_chance > ran(): | |
| Matrix[h][w] = 1 | |
| elif 1 in adjacent_tiles: | |
| if water_near_chance > ran(): | |
| Matrix[h][w] = 1 | |
| elif 1 not in adjacent_tiles: | |
| if house_chance > ran(): | |
| Matrix[h][w] = 2 | |
| print_matrix(Matrix) | |
| # too lazy to enter input | |
| # but could put in continue and if =='n': break here | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment