Created
June 16, 2013 01:08
-
-
Save hellais/5790290 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 | |
def write_to_log(car): | |
try: | |
with open('solver.txt', 'a+') as f: | |
f.write("-----------\n") | |
f.write("Moves: %s\n" % car.total_moves) | |
f.write(car.all_data_received) | |
f.write("-----------\n") | |
except Exception, e: | |
print "PORCO DIO" | |
print e | |
sample_map = """ | |
|-----| | |
| T | | |
| P | | |
| | | |
| c| | |
|~ Z| | |
| | | |
| TT | | |
| Z | | |
| u | | |
|-----| | |
""" | |
class ObstacleMap(object): | |
def __init__(self, ascii_map=sample_map, car=None): | |
self.obstacle_map = {} | |
self.car = car | |
for i in range(9): | |
self.obstacle_map[i] = {} | |
self.import_map(ascii_map) | |
@property | |
def player_position(self): | |
for j in range(5): | |
try: | |
if self.obstacle_map[8][j] == 'player': | |
return j | |
except Exception, e: | |
print self.ascii_map | |
print "ERRRRRRRRRRRORRRRRRRRRR!" | |
print self.ascii_map | |
write_to_log(self.car) | |
sys.exit(1) | |
def import_map(self, ascii_map): | |
self.ascii_map = 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 | |
if not track_started: | |
print "Error parsing map" | |
print ascii_map | |
write_to_log(self) | |
sys.exit(1) | |
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') | |
def path_to_end(self): | |
moves = '' | |
simulated_player_position = self.player_position | |
for i, row in self.obstacle_map.items(): | |
if i > 4: | |
continue | |
next_row = self.obstacle_map[7-i] | |
next_next_row = self.obstacle_map[7-i] | |
# for j, column in self.obstacle_map[i].items(): | |
# pass | |
if simulated_player_position < 2 and \ | |
next_row[simulated_player_position + 1] == 'free': | |
print "[0]" | |
moves += 'r' | |
simulated_player_position += 1 | |
if next_row[simulated_player_position] == 'free': | |
print "[5]" | |
moves += 'f' | |
elif simulated_player_position != 0 and \ | |
next_row[simulated_player_position - 1] == 'free': | |
print "[6]" | |
moves += 'l' | |
simulated_player_position -= 1 | |
else: | |
print "[7]" | |
moves += 'r' | |
simulated_player_position += 1 | |
print "Using path %s" % moves | |
self.car.all_data_received += "next moves: " + moves + '\n' | |
return moves | |
def next_best_move(self): | |
row_7 = self.obstacle_map[7].values() | |
row_6 = self.obstacle_map[6].values() | |
if self.player_position < 2 and \ | |
row_7[self.player_position+1] == 'free': | |
# (row_6[self.player_position+1] == 'free' or \ | |
# row_6[self.player_position+2] == 'free'): | |
print "[0]" | |
return 'right' | |
if row_7[self.player_position] == 'free': | |
if row_6[self.player_position] != 'free' and \ | |
row_7[self.player_position-1] == 'free': | |
if row_6[self.player_position - 2] == 'free': | |
print "[1]" | |
return 'left' | |
elif self.player_position < 3 and \ | |
row_6[self.player_position + 2] == "free": | |
print "[2]" | |
return 'right' | |
elif self.player_position > 3 and \ | |
row_6[self.player_position - 2] == 'free': | |
print "[3]" | |
return 'left' | |
else: | |
print "[4]" | |
return "forward" | |
print "[5]" | |
return 'forward' | |
elif self.player_position != 0 and \ | |
row_7[self.player_position-1] == 'free': | |
print "[6]" | |
return 'left' | |
else: | |
print "[7]" | |
return 'right' | |
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() | |
self.total_moves = 0 | |
self.all_data_received = '' | |
print self.ascii_map | |
def read_screen(self): | |
try: | |
data_received = self.s.recv(1024*2) | |
self.all_data_received += data_received | |
self.ascii_map = data_received | |
except: | |
write_to_log(self) | |
def move(self, direction=""): | |
self.total_moves += 1 | |
msg = direction | |
self.s.send(msg) | |
self.read_screen() | |
self.om = ObstacleMap(self.ascii_map, self) | |
print "~-= Grand Prix Pro Racer =-~" | |
print "| |" | |
print "| pos: %s " % self.om.player_position | |
print "| moves: %s " % self.total_moves | |
print "|__________________________|" | |
self.om.print_obstacle_map() | |
def move_forward(self): | |
self.move("\n") | |
def move_left(self): | |
self.move("l") | |
def move_right(self): | |
self.move("r") | |
def path_move(self, path): | |
directions = "" | |
for s in path: | |
if s == "f": | |
directions += '\n' | |
else: | |
directions += s | |
self.move(directions) | |
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() | |
car.move_forward() | |
while True: | |
try: | |
next_best_move = car.om.next_best_move() | |
except: | |
print "Error in calculating next best move" | |
write_to_log(car) | |
print car.om.ascii_map | |
if next_best_move == 'left': | |
car.move_left() | |
elif next_best_move == 'right': | |
car.move_right() | |
elif next_best_move == 'forward': | |
car.move_forward() | |
def test_car_all_moves(): | |
car = Car() | |
car.connect() | |
car.move_forward() | |
while True: | |
path = car.om.path_to_end() | |
car.path_move(path) | |
# try: | |
# pass | |
# except Exception, e: | |
# print "Error in calculating next best move" | |
# write_to_log(car.om.ascii_map) | |
# print car.om.ascii_map | |
# raise e | |
test_car_all_moves() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment