Last active
August 29, 2015 14:04
-
-
Save QuantumFractal/9c01b9909a9d81e52234 to your computer and use it in GitHub Desktop.
Random Python Code
This file contains 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
#Node System | |
# We'll see where this goes | |
class Node: | |
def __init__(self, parent): | |
if parent is None: | |
self.isRoot = True | |
else: | |
self.isRoot = False | |
self.parent = parent | |
self.parent.add_child(self) | |
self.children = [] | |
def __str__(self): | |
return 'Node' | |
def add_child(self, child): | |
self.children.append(child) | |
def print_tree_below(self, tabs = 0): | |
string = '' | |
print self | |
for child in self.children: | |
if child.children == []: | |
print tabs*'\t'+ str(child) | |
else: | |
child.print_tree_below(tabs+1) | |
n1 = Node(None) | |
n2 = Node(n1) | |
n3 = Node(n1) | |
n4 = Node(n2) | |
n5 = Node(n2) | |
n1.print_tree_below() |
This file contains 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
class SodaMachine: | |
def __init__(self): | |
pass | |
def fill(self, soda_cup, amount, soda_type): | |
if soda_cup.lidded: | |
return -1 | |
soda_cup.amount += amount | |
soda_cup.soda_type = soda_type | |
class SodaCup: | |
_soda_limit = 10 | |
amount = 0 | |
lidded = False | |
soda_type = None | |
def __init__(self, soda_limit ): | |
self._soda_limit = soda_limit | |
def __str__(self): | |
return str(self.amount)+ ' ml of ' + str(self.soda_type) | |
def is_full(self): | |
return self._soda_limit == self.amount | |
def is_overfilled(self): | |
return self._soda_limit < self.amount | |
def place_lid(self): | |
if self.lidded == True: | |
return -1 | |
else: | |
self.lidded = True | |
def remove_lid(self): | |
if self.lidded == False: | |
return -1 | |
else: | |
self.lidded = False | |
cup1 = SodaCup(20) | |
machine = SodaMachine() | |
cup1.place_lid() | |
machine.fill(cup1, 20, 'Dr Pepper') | |
print cup1 | |
cup1.remove_lid() | |
machine.fill(cup1, 35, 'Coke') | |
print cup1 | |
print cup1.is_overfilled() |
This file contains 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
#!/usr/bin/env python | |
import random, copy | |
height = 20 | |
width = 40 | |
terrain = [[0 for x in xrange(width)] for x in xrange(height)] | |
class Terrain: | |
def __init__(self): | |
self.grass_template = Grass() | |
self.mountain_template = Mountain() | |
self.marsh_template = Marsh() | |
self.river_template = River() | |
self.grid = [[self.grass_template for x in xrange(width)] for x in xrange(height)] | |
def __str__(self): | |
string = '' | |
for row in self.grid: | |
for col in row: | |
string += str(col) | |
string += '\n' | |
return string | |
def gen_marsh(self): | |
marshes = [{'vec':Vector2(random.randint(0,height-1), random.randint(0,width-1)), | |
'size':random.randint(0,3)} for x in range(5)] | |
for marsh in marshes: | |
self.grid[marsh['vec'].x][marsh['vec'].y] = self.marsh_template | |
for x in range(marsh['size']): | |
self.paint_circle(marsh['vec'].x, marsh['vec'].y, x-1, self.marsh_template) | |
def gen_mountain(self): | |
seed = Vector2(random.randint(0,height-1), random.randint(0,width-1)) | |
cursor = copy.copy(seed) | |
dist = random.randint(0,40) | |
self.grid[seed.x][seed.y] = self.mountain_template | |
for x in range(dist): | |
direction = random.randint(0,8) | |
cursor.move(direction) | |
self.paint_pixel(cursor.x, cursor.y, self.mountain_template) | |
def gen_mountain_range(self, freq): | |
for x in range(freq): | |
self.gen_mountain() | |
def gen_river(self): | |
seed = Vector2(random.randint(0,height-1), random.randint(0,width-1)) | |
cursor = copy.copy(seed) | |
dist = random.randint(0,40) | |
self.grid[seed.x][seed.y] = self.river_template | |
for x in range(dist): | |
if x % 3: | |
cursor.move(direction) | |
direction = random.randint(0,8) | |
self.paint_circle(cursor.x, cursor.y, 2, self.river_template) | |
def paint_pixel(self, x, y, tile): | |
try: | |
self.grid[x][y] = tile | |
except: | |
#Meaning we hit a boundary, ignore it. | |
pass | |
def paint_circle(self, x0, y0, radius, tile): | |
f = 1 - radius | |
ddf_x = 1 | |
ddf_y = -2 * radius | |
x = 0 | |
y = radius | |
self.paint_pixel(x0, y0 + radius, tile) | |
self.paint_pixel(x0, y0 - radius, tile) | |
self.paint_pixel(x0 + radius, y0, tile) | |
self.paint_pixel(x0 - radius, y0, tile) | |
while x < y: | |
if f >= 0: | |
y -= 1 | |
ddf_y += 2 | |
f += ddf_y | |
x += 1 | |
ddf_x += 2 | |
f += ddf_x | |
self.paint_pixel(x0 + x, y0 + y, tile) | |
self.paint_pixel(x0 - x, y0 + y, tile) | |
self.paint_pixel(x0 + x, y0 - y, tile) | |
self.paint_pixel(x0 - x, y0 - y, tile) | |
self.paint_pixel(x0 + y, y0 + x, tile) | |
self.paint_pixel(x0 - y, y0 + x, tile) | |
self.paint_pixel(x0 + y, y0 - x, tile) | |
self.paint_pixel(x0 - y, y0 - x, tile) | |
class Vector2: | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
def move(self, direction): | |
if direction == 1: | |
self.y+=1 | |
if direction == 2: | |
self.y+=1 | |
self.x+=1 | |
if direction == 3: | |
self.x+=1 | |
if direction == 4: | |
self.y-=1 | |
self.x+-1 | |
if direction == 5: | |
self.y-=1 | |
if direction == 6: | |
self.y-=1 | |
self.x-=1 | |
if direction == 7: | |
self.x-=1 | |
if direction == 8: | |
self.x-=1 | |
self.y+=1 | |
class Tile: | |
def __str__(self): | |
return self.symbol | |
class Grass(Tile): | |
def __init__(self): | |
self.name = 'Grass' | |
self.symbol = ',' | |
class Mountain(Tile): | |
def __init__(self): | |
self.name = 'Mountain' | |
self.symbol = '^' | |
class Marsh(Tile): | |
def __init__(self): | |
self.name = 'Marsh' | |
self.symbol = '#' | |
class River(Tile): | |
def __init__(self): | |
self.name = 'River' | |
self.symbol = '~' | |
world = Terrain() | |
world.gen_marsh() | |
world.gen_mountain_range(10) | |
world.gen_river() | |
print world | |
#world.gen_marsh() | |
''' OUTPUT | |
,,,,,,,,,,,,,,,,,,,,^^^^,,,,,,,,,,,,,^^, | |
,,,,,,,,,,,,,,,,,,,,,,,^,,,,,,,,,,,^^^^^ | |
,,,,,,,,,,,,,,,,,,,,,,^^,,,^,,,^,^^^^,^^ | |
,,,,,,,,,,,,,,,,,,,,,^^#,,^^^,~~~~^^^,,^ | |
,,,#,,,,,,,,,,,,,,,,^,#,,,,^,~~~~~~~~,,, | |
,,###,,,,,,,,,,,,,,^,,,,,,,,^~~~~~~~~~,, | |
,,,#,,,,,,,,,,,,,,,,^^^,,,,^^~~~~~~~~~#, | |
,,^^,,,,,,,,,,,,,,,,,,,,,,,,,^~~~~~~~~,, | |
,,^,^,,,,,^^,,,,,,,,,,,,,,,,,,,~~~~~~,,, | |
,,,^,,,,,,,^^^,,,,,,,,,,,,,,,,,^~~~~,,,, | |
,,^,,,,,,,#^^,,,,,,,,,,,,,,,,^^,,,,,,,,, | |
,^^,,,,,,##^^,,,,,,,,,,,,,,,,^^,,,,,^^,, | |
,,,^,,,,,,#^,,,,,,,,,,,,,,,,,,,^^^,,^,,, | |
,,,^^,,,,,,^,,,,,,,,,,,,,,,,,,,,^^,,,,,, | |
,,^^,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, | |
,,,^^,,,,,,,,,,,,,,,,,,^,,,,,,,,,,,,,,,, | |
,,,,^,,,,,,,,,,,,,,,,,,^,,,,,,,,,,,,,,,, | |
,,,,^^^,,,,,,,,,,,,,,,^^,,,,^^,,,,,,,,,, | |
,,^,,^,,,,,,#,,,,,,,,,^^^,^,,,,,,,,,,,,, | |
,,,^,,,,,,,###,,,,,,^^^,,^,^,,,,,,,,^^,, | |
''' |
This file contains 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
# Streetlamp simulator... I don't know why I made this | |
# Enjoy the simplicity of being a streetlamp | |
# | |
# licensed under the GNU GPL. Not to be distributed for monetary gain. | |
class Lamppost: | |
def __init__(self, initial_state = False): | |
self.state = initial_state | |
self.color = (1,1,1) | |
def __str__(self): | |
if self.state: | |
return "[On] "+str(self.color) | |
else: | |
return "[Off] "+str(self.color) | |
def toggle(self): | |
self.state = not self.state | |
def set_color(sefl, color_tuple): | |
self.color = color_tuple | |
class Command: | |
def execute(self, object): | |
pass | |
class ToggleCommand(Command): | |
def execute(self, object): | |
object.toggle() | |
class OnCommand(Command): | |
def execute(self, object): | |
if not object.state: | |
object.toggle() | |
class Street: | |
def __init__(self, number_of_lamps = 0, initial_state = False): | |
self.lamps = [] | |
for x in range(number_of_lamps): | |
self.lamps.append(Lamppost(initial_state)) | |
self.toggleCMD = ToggleCommand() | |
self.onCMD = OnCommand() | |
def __str__(self): | |
string = '' | |
for lamp in self.lamps: | |
string += (str(lamp)+'\n') | |
return string | |
def toggle_lamp(self, lamp_index): | |
self.toggleCMD.execute(self.lamps[lamp_index]) | |
def toggle_all(self): | |
for lamp in self.lamps: | |
self.toggleCMD.execute(lamp) | |
def all_on(self): | |
for lamp in self.lamps: | |
self.onCMD.execute(lamp) | |
def all_off(self): | |
self.all_on() | |
self.toggle_all() | |
street = Street(10) | |
print street | |
street.toggle_lamp(5) | |
print street | |
street.toggle_all() | |
print street | |
street.all_on() | |
print street | |
street.all_off() | |
print street |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment