class Maze: def __init__(self, w=20, h=20, complexity=.75, density=.75, outputfile="out.csv", mode=0): sc.init() self.font = sc.font.SysFont(None, 24) self.text = self.font.render("Right Key: Changes Maze", True, (0, 128, 0)) self.text1 = self.font.render("Down Key: Increases Density", True, (0, 128, 0)) self.text2 = self.font.render("Up Key: Increases Complexity", True, (0, 128, 0)) self.car = Car(4, 4, 9, 9) States.__init__(self) self.w = w self.h = h self.mode = 0 #three modes 0=just play grounf 1=bot mode 2=train mode self.outputfile = outputfile self.maze = BaseMaze(self.w, self.h, complexity, density, 1, 1, self.w - 2, self.h - 2) self.predictor = predict.Predictor() def exe_logger(self): self.logger = Logger(open("output/" + self.outputfile, "w"), self) def startup(self, nums): #setting up the start up print(nums) if (nums[0] == '0'): self.mode = 0 print("playing mode") elif (nums[0] == '1'): self.mode = 1 print("bot mode") else: self.mode = 2 if nums[1] == ' ' or nums[1] == '': self.outputfile = "out.csv" else: self.outputfile = nums[1] self.exe_logger() sc.key.set_repeat(1, 28) return def get_event(self, event): if event.type == sc.KEYDOWN: self.logger.log(event.key) if event.key == sc.K_RIGHT: #re-builds maze self.car = Car(4, 4, 9, 9) self.maze.re_Construct() elif event.key == sc.K_UP: #increases complexity self.car = Car(4, 4, 9, 9) self.maze.inc_com() elif event.key == sc.K_DOWN: #increases density self.car = Car(4, 4, 9, 9) self.maze.inc_den() #adding new part to the code if event.key == sc.K_a and self.maze.get_Value( int(self.car.get_x()), int(self.car.get_y()) - 2) in DRIVABLE and self.maze.get_Value( int(self.car.get_x() + 1), int(self.car.get_y()) - 2) in DRIVABLE: #left self.car.m_u() if event.key == sc.K_d and self.maze.get_Value( int(self.car.get_x()), int(self.car.get_y()) + 1) in DRIVABLE and self.maze.get_Value( int(self.car.get_x() + 1), int(self.car.get_y()) + 1) in DRIVABLE: #right self.car.m_d() if event.key == sc.K_w and self.maze.get_Value( int(self.car.get_x()) - 1, int( self.car.get_y())) in DRIVABLE and self.maze.get_Value( int(self.car.get_x()) - 1, int(self.car.get_y() - 1)) in DRIVABLE: #UP self.car.m_l() if event.key == sc.K_s and self.maze.get_Value( int(self.car.get_x()) + 1 + 1, int(self.car.get_y() - 1)) in DRIVABLE and self.maze.get_Value( int(self.car.get_x()) + 1 + 1, int( self.car.get_y())) in DRIVABLE: #down self.car.m_r() # Check whether car has hit the finish if self.maze.get_Value(int(self.car.get_x()), int(self.car.get_y())) == 3: print("Congratulations!") def update(self, screen, dt): self.draw(screen) subMaze = predict.subArray(self.maze.maze, self.car.get_x(), self.car.get_y(), 24, 24) # comment below this to not have the bot running if self.mode is 1: self.bot_input(self.predictor.act(subMaze)) #reinforcement learning (simplifies maze) self.maze.maze = reinforce.copy_into( self.maze.maze, reinforce.fill_dead_ends( predict.subArray(self.maze.maze, self.car.get_x(), self.car.get_y(), 24, 24)), self.car.get_x() - 12, self.car.get_y() - 12) def bot_input(self, event): if event == 1 and self.maze.get_Value( int(self.car.get_x()), int(self.car.get_y()) - 2) in DRIVABLE and self.maze.get_Value( int(self.car.get_x() + 1), int(self.car.get_y()) - 2) in DRIVABLE: #left self.car.m_u() if event == 3 and self.maze.get_Value( int(self.car.get_x()), int(self.car.get_y()) + 1) in DRIVABLE and self.maze.get_Value( int(self.car.get_x() + 1), int(self.car.get_y()) + 1) in DRIVABLE: #right self.car.m_d() if event == 0 and self.maze.get_Value( int(self.car.get_x()) - 1, int( self.car.get_y())) in DRIVABLE and self.maze.get_Value( int(self.car.get_x()) - 1, int(self.car.get_y() - 1)) in DRIVABLE: #UP self.car.m_l() if event == 2 and self.maze.get_Value( int(self.car.get_x()) + 1 + 1, int(self.car.get_y() - 1)) in DRIVABLE and self.maze.get_Value( int(self.car.get_x()) + 1 + 1, int( self.car.get_y())) in DRIVABLE: #down self.car.m_r() # Check whether car has hit the finish if self.maze.get_Value(int(self.car.get_x()), int(self.car.get_y())) == 3: print("Congratulations!") def draw(self, screen): #actual drawing goes here screen.fill((255, 255, 255)) sc.draw.rect(screen, (130, 82, 1), sc.Rect(0, 0, 850, 455)) x1 = 0 y1 = 0 Cw = 4 #cell width for a in range(0, self.maze.g_w()): for b in range(0, self.maze.g_h()): if self.maze.get_Value(b, a) == 0: #road sc.draw.rect(screen, (128, 128, 128), sc.Rect(y1, x1, Cw, Cw)) sc.draw.rect(screen, (128, 128, 128), sc.Rect(y1, x1, Cw, Cw)) if self.maze.get_Value(b, a) == 2: #start sc.draw.rect(screen, (0, 0, 0), sc.Rect(y1, x1, Cw, Cw)) sc.draw.rect(screen, (0, 0, 0), sc.Rect(y1, x1, Cw, Cw)) if self.maze.get_Value(b, a) == 3: #end sc.draw.rect(screen, (255, 255, 255), sc.Rect(y1, x1, Cw, Cw)) sc.draw.rect(screen, (255, 255, 255), sc.Rect(y1, x1, Cw, Cw)) x1 = x1 + Cw y1 = y1 + Cw x1 = 0 self.car.draw(screen) screen.blit(self.text, (0, 455)) screen.blit(self.text1, (0, 505)) screen.blit(self.text2, (0, 480)) screen.blit( self.font.render((str(round(self.maze.g_com(), 2))), True, (0, 128, 0)), (250, 480)) screen.blit( self.font.render((str(round(self.maze.g_den(), 2))), True, (0, 128, 0)), (250, 505)) return def cleanup(self): #createclean up pass
class Maze: def __init__(self, w=20, h=20, complexity=.75, density=.75): sc.init() self.font = sc.font.SysFont(None, 24) self.text = self.font.render("Right key:changes maze", True, (0, 128, 0)) self.text1 = self.font.render("Down Key: Increases Density", True, (0, 128, 0)) self.text2 = self.font.render("Up Key:Increases complexity", True, (0, 128, 0)) self.car = Car(10, 10, 1, 1) States.__init__(self) self.w = w self.h = h self.maze = BaseMaze(self.w, self.h, complexity, density) def get_event(self, event): if event.type == sc.KEYDOWN: if event.key == sc.K_RIGHT: #re-builds maze self.car = Car(10, 10, 1, 1) self.maze.re_Construct() elif event.key == sc.K_UP: #increases complexity self.car = Car(10, 10, 1, 1) self.maze.inc_com() elif event.key == sc.K_DOWN: #increases density self.car = Car(10, 10, 1, 1) self.maze.inc_den() #adding new part to the code if event.key == sc.K_a and self.maze.get_Value( int(self.car.get_x()), int(self.car.get_y()) - 1) == 0: #UP self.car.m_u() elif event.key == sc.K_a: self.car.dec_side() if (event.key == sc.K_d and self.maze.get_Value( int(self.car.get_x()), int(self.car.get_y()) + 1) == 0): #right self.car.m_d() elif event.key == sc.K_d: self.car.inc_side() if event.key == sc.K_w and self.maze.get_Value( int(self.car.get_x()) - 1, int( self.car.get_y())) == 0: #Left self.car.m_l() elif event.key == sc.K_w: self.car.dec_up_down() if event.key == sc.K_s and self.maze.get_Value( int(self.car.get_x()) + 1, int( self.car.get_y())) == 0: #right self.car.m_r() elif event.key == sc.K_s: self.car.inc_up_down() def update(self, screen, dt): self.draw(screen) def draw(self, screen): #actual drawing goes here screen.fill((255, 255, 255)) sc.draw.rect(screen, (130, 82, 1), sc.Rect(0, 0, 750, 455)) x1 = 0 y1 = 0 Cw = 30 #cell width for a in range(0, self.maze.g_w()): for b in range(0, self.maze.g_h()): if self.maze.get_Value(b, a) == 0: #road sc.draw.rect(screen, (128, 128, 128), sc.Rect(y1, x1, Cw, Cw)) sc.draw.rect(screen, (128, 128, 128), sc.Rect(y1, x1, Cw, Cw)) x1 = x1 + Cw y1 = y1 + 30 x1 = 0 #z1=int(self.car.get_x())*30 #z2=int(self.car.get_y())*30 #sc.draw.rect(screen,(0,0,255),sc.Rect(z2,z1,self.car.get_h(),self.car.get_w())) sc.draw.rect( screen, (0, 0, 255), sc.Rect(self.car.get_y1(), self.car.get_x1(), self.car.get_h(), self.car.get_w())) screen.blit(self.text, (0, 455)) screen.blit(self.text1, (0, 505)) screen.blit(self.text2, (0, 480)) screen.blit( self.font.render((str(round(self.maze.g_com(), 2))), True, (0, 128, 0)), (250, 480)) screen.blit( self.font.render((str(round(self.maze.g_den(), 2))), True, (0, 128, 0)), (250, 505)) return def cleanup(self): #createclean up pass