Created
June 15, 2013 23:15
-
-
Save hellais/5789974 to your computer and use it in GitHub Desktop.
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 sys | |
import socket | |
sample_map = """ | |
|-----| | |
| T | | |
| P | | |
| | | |
| c| | |
|~ Z| | |
| | | |
| TT | | |
| Z | | |
| u | | |
|-----| | |
""" | |
class ObstacleMap(object): | |
def __init__(self, ascii_map=sample_map): | |
self.obstacle_map = {} | |
for i in range(9): | |
self.obstacle_map[i] = {} | |
self.import_map(ascii_map) | |
def import_map(self, ascii_map): | |
track_started = False | |
line_number = -1 | |
for line in ascii_map.split('\n'): | |
if len(line) < 5: | |
continue | |
if track_started: | |
line_number += 1 | |
if line == "|-----|" and not track_started: | |
track_started = True | |
continue | |
if line == "|-----|" and track_started: | |
return None | |
for i in range(5): | |
if line[i+1] == "u": | |
label = 'player' | |
elif line[i+1] == " ": | |
label = 'free' | |
else: | |
label = 'obstacle' | |
self.obstacle_map[line_number][i] = label | |
def print_obstacle_map(self): | |
for i in range(9): | |
for j in range(5): | |
cell = self.obstacle_map[i][j] | |
if cell == 'player': | |
msg = "u" | |
elif cell == 'free': | |
msg = " " | |
elif cell == 'obstacle': | |
msg = "X" | |
sys.stdout.write(msg) | |
sys.stdout.write('\n') | |
class Car(object): | |
_host = "grandprix.shallweplayaga.me" | |
_port = 2038 | |
ascii_map = "" | |
def connect(self): | |
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.s.connect((self._host , self._port)) | |
self.read_screen() | |
print self.ascii_map | |
def read_screen(self): | |
self.ascii_map = self.s.recv(1024*2) | |
def move(self, direction=""): | |
msg = direction + "\n" | |
self.s.send(msg) | |
self.read_screen() | |
self.om = ObstacleMap(self.ascii_map) | |
self.om.print_obstacle_map() | |
def move_forward(self): | |
self.move() | |
def move_left(self): | |
self.move("l") | |
def move_right(self): | |
self.move("r") | |
def disconnect(self): | |
self.s.close() | |
def test_obstacle_map_object(): | |
""" | |
This tests usage of obstacle map that represents the map of obstacles. | |
What is interesting in ObstacleMap.obstacle_map that is a two-dimensional | |
array containing at position x[i][j] the thing present on the ith row and | |
the jth column. | |
The values of the cell can be "obstacle", "free" or "player" | |
""" | |
om = ObstacleMap() | |
om.print_obstacle_map() | |
def test_car_object(): | |
""" | |
This allows you to create a car on the server and control it left, right | |
forward. | |
Every time you move the car you also get the ascii map of it's current | |
position and an instance of obstacle map is created to contain it (self.om) | |
""" | |
car = Car() | |
car.connect() | |
for i in range(10): | |
car.move_forward() | |
car.move_left() | |
car.move_forward() | |
car.move_forward() | |
car.move_forward() | |
car.move_right() | |
car.move_forward() | |
test_car_object() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment