The code that determines the map size and content in this lab is this section:
scrSize = scrWidth, scrHeight = 480,480 mapRows, mapCols = 15, 15 # the size of the map in rows by columns tilePixels = 32 defaultMap = [ ['w','w','w','w','w','w','w','w','w','w','w','w','w','w','w'], ['w','g','g','g','g','g','g','g','g','g','w','g','g','g','w'], ['w','g','w','g','g','w','w','w','w','g','w','g','w','f','w'], ['w','g','w','g','g','w','g','g','g','g','w','g','w','w','w'], ['w','g','w','w','w','w','g','w','w','w','w','g','g','g','w'], ['w','g','g','w','g','g','g','g','g','g','w','g','g','g','w'], ['w','g','g','w','g','g','g','g','l','g','w','w','w','g','w'], ['w','g','g','w','g','g','l','l','l','g','g','g','g','g','w'], ['w','g','w','w','w','w','l','l','l','l','g','g','g','g','w'], ['w','g','w','g','g','g','g','l','w','g','g','w','w','w','w'], ['w','g','w','g','g','g','g','g','w','g','g','w','g','g','w'], ['w','g','w','w','w','w','w','g','w','g','w','w','g','g','w'], ['w','g','g','g','g','w','g','g','w','g','w','w','g','w','w'], ['w','g','w','g','g','g','g','g','w','g','g','g','g','g','w'], ['w','w','w','w','w','w','w','w','w','w','w','w','w','w','w'] ] |
The first exercise is to replace the map with a map that is 5 rows by 5 columns, with walls around the outside, the finish flag in the center, and grass everywhere else. (This should result in 10 lines of code.)
Sample solution scrSize = scrWidth, scrHeight = 160,160 mapRows, mapCols = 5, 5 # the size of the map in rows by columns tilePixels = 32 defaultMap = [ ['w','w','w','w','w'], ['w','g','g','g','w'], ['w','g','f','g','w'], ['w','g','g','g','w'], ['w','w','w','w','w'] ] |
The code that controls the 'wall-hugging' algorithm for the AI contained in a portion of the plotDirection routine (shown below with the comments removed).
The second portion of the lab exercise is to provide an alternate version of the code that causes the AI to hug the wall in the opposite direction - i.e. counter clockwise instead of clockwise.
Sample solution elif (self.plotting == 'clockwise'): if (self.movingDir == 'n'): if self.checkAndMove('w'): return elif self.checkAndMove('n'): return elif self.checkAndMove('e'): return else: self.checkAndMove('s') elif (self.movingDir == 'e'): if self.checkAndMove('n'): return elif self.checkAndMove('e'): return elif self.checkAndMove('s'): return else: self.checkAndMove('w') elif (self.movingDir == 's'): if self.checkAndMove('e'): return elif self.checkAndMove('s'): return elif self.checkAndMove('w'): return else: self.checkAndMove('n') else: if self.checkAndMove('s'): return elif self.checkAndMove('w'): return elif self.checkAndMove('n'): return else: self.checkAndMove('e') |