Advertisement
Guest User

map.py

a guest
Jul 13th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from random import choice
  3. from os import system, name
  4. from itertools import chain
  5.  
  6.  
  7. class Tiles(object):
  8.     tree = '\033[7;1;32m  \033[0m'
  9.     land = '\033[7;32m  \033[0m'
  10.     sand = '\033[7;1;33m  \033[0m'
  11.     water = '\033[7;1;34m  \033[0m'
  12.  
  13.  
  14. class Sizes(object):
  15.     small = 10
  16.     med1 = 20
  17.     med2 = 30
  18.     large = 40
  19.  
  20. sizes = (
  21.     Sizes.small, Sizes.med1, Sizes.med2, Sizes.large)
  22.  
  23. tiles = (
  24.     Tiles.land, Tiles.sand, Tiles.tree, Tiles.water)
  25.  
  26.  
  27. transitions = {Tiles.land: [Tiles.sand] +
  28.                [Tiles.tree] * 2 +
  29.                [Tiles.land] * 5,
  30.                Tiles.sand: [Tiles.water, Tiles.sand],
  31.                Tiles.tree: [Tiles.tree, Tiles.land],
  32.                Tiles.water: [Tiles.water] * 10
  33.                }
  34.  
  35.  
  36. def pick_tile_for(world, x, y):
  37.     surrounding = []
  38.     if x > 0:
  39.         surrounding.append(world[x - 1][y])
  40.     if y < len(world) - 1:
  41.         surrounding.append(world[x][y + 1])
  42.     if y > 0:
  43.         surrounding.append(world[x][y - 1])
  44.     if x < len(world) - 1:
  45.         surrounding.append(world[x + 1][y])
  46.     surrounding = [tile for tile in surrounding if tile is not None]
  47.     if len(surrounding) == 0:
  48.         return None
  49.     excluded = set(surrounding[0])
  50.     for tile in surrounding:
  51.         excluded = excluded - set(transitions[tile])
  52.     next = list(chain(*[[t for t in transitions[tile] if t not in excluded]
  53.                         for tile in surrounding]))
  54.     return choice(next)
  55.  
  56.  
  57. def generate(world_size):
  58.     system('cls' if name == 'nt' else 'clear')
  59.     world = []
  60.     for x in range(world_size):
  61.         world.append([None for _ in range(world_size)])
  62.     world[choice(range(world_size))][choice(range(world_size))] = Tiles.land
  63.     for x in range(world_size):
  64.         for y in range(world_size):
  65.             if world[x][y] is None:
  66.                 world[x][y] = pick_tile_for(world, x, y)
  67.     for x in range(world_size - 1, -1, -1):
  68.         for y in range(world_size - 1, -1, -1):
  69.             if world[x][y] is None:
  70.                 world[x][y] = pick_tile_for(world, x, y)
  71.     for line in world:
  72.         print ''.join(line)
  73.  
  74.  
  75. if __name__ == "__main__":
  76.     generate(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement