Skip to content

Instantly share code, notes, and snippets.

@josephglanville
Created April 21, 2018 09:51
Show Gist options
  • Save josephglanville/beb5d8fe99324a4c1c8a84f568e7d3f1 to your computer and use it in GitHub Desktop.
Save josephglanville/beb5d8fe99324a4c1c8a84f568e7d3f1 to your computer and use it in GitHub Desktop.
Example programs BKK NHL
class Car:
def __init__(self, wheels, color, speed):
self.wheels = wheels
self.color = color
self.speed = speed
self.distance = 0
def drive(self, how_long):
self.distance = self.distance + (self.speed * how_long)
def time_travelled(self):
time = self.distance / self.speed
return time
josephs_car = Car(4, "Red", 100)
print(josephs_car.distance)
josephs_car.drive(4)
print(josephs_car.distance)
josephs_car.drive(2)
print(josephs_car.distance)
print(josephs_car.time_travelled())
directions = ["NORTH", "EAST", "SOUTH", "WEST"]
class Robot:
def __init__(self, board, position_x, position_y):
self.x = position_x
self.y = position_y
self.board = board
self.direction = "NORTH"
def drive(self):
if self.direction == "NORTH":
if board.validate(self.x, self.y + 1):
self.y = 1
return
if self.direction == "EAST":
if self.direction == "SOUTH":
if self.direction == "WEST":
else:
print("Where are you facing!")
return
def left(self):
self.direction = directions[(directions.index(self.direction) - 1) % 4]
def right(self):
self.direction = directions[(directions.index(self.direction) + 1) % 4]
def position(self):
return
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
def validate(self, x, y):
if (x < 0):
return False
if (y < 0):
return False
if (x > self.width):
return False
if (y > self.height):
return False
else:
return True
board = Board(10, 10)
print(board.validate(5, 5))
print(board.validate(-1, -1))
print(board.validate(0, 0))
print(board.validate(10, 10))
robot = Robot(0, 0)
language = """
drive
left
right
position
""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment