# # Random Map Generator/Validator for Avenue Z # # Todd C. Edwards # # Random map gen vs. try all combinations? Use random, but skip if it is already on the list # Add: write valid maps to a text file import random def mapGen(map, wallGrid): # Define the tile types in a list tile = [11, 21, 22, 31, 32, 33, 34] # Generate a map of 9 random tiles for i in xrange(9): j = random.randrange(7) map.append (tile[j]) #print i, j, tile[j] updateWalls (i, tile[j], wallGrid) #print map #print the wall grid #for m in xrange (11): #print wallGrid[m] #print '' def updateWalls (tileNum, tileType, wallGrid): x = y = 0 # Get x, y for the current tile (x, y in wallGrid coords) if tileNum == 0: x, y = 5, 2 elif tileNum == 1: x, y = 7, 4 elif tileNum == 2: x, y = 9, 6 elif tileNum == 3: x, y = 3, 4 elif tileNum == 4: x, y = 5, 6 elif tileNum == 5: x, y = 7, 8 elif tileNum == 6: x, y = 1, 6 elif tileNum == 7: x, y = 3, 8 elif tileNum == 8: x, y = 5, 10 else: print 'error, tileNum = ', print tileNum # Set up the Wall Grid based on tile type. if tileType == 11: wallGrid[x][y+1] = 0 wallGrid[x+1][y] = 0 wallGrid[x][y-1] = 0 wallGrid[x-1][y] = 0 elif tileType == 21: wallGrid[x][y+1] = 1 wallGrid[x+1][y] = 0 wallGrid[x][y-1] = 1 wallGrid[x-1][y] = 0 elif tileType == 22: wallGrid[x][y+1] = 0 wallGrid[x+1][y] = 1 wallGrid[x][y-1] = 0 wallGrid[x-1][y] = 1 elif tileType == 31: wallGrid[x][y+1] = 1 wallGrid[x+1][y] = 0 wallGrid[x][y-1] = 0 wallGrid[x-1][y] = 1 elif tileType == 32: wallGrid[x][y+1] = 1 wallGrid[x+1][y] = 1 wallGrid[x][y-1] = 0 wallGrid[x-1][y] = 0 elif tileType == 33: wallGrid[x][y+1] = 0 wallGrid[x+1][y] = 1 wallGrid[x][y-1] = 1 wallGrid[x-1][y] = 0 elif tileType == 34: wallGrid[x][y+1] = 0 wallGrid[x+1][y] = 0 wallGrid[x][y-1] = 1 wallGrid[x-1][y] = 1 else: print 'error, tileType = ', print tileType def pathFind (x, y, wx, wy, wallGrid): global valid #print valid if wallGrid[wx][wy] == 1: # If you hit a wall, dead end #print 'dead end' return elif (x,y) == (4,11): # If you reached the end, map is valid valid = True #print 'VALID', x, y, 'and sealing off', wx, wy return else: #print 'Arriving at', x, y, 'and sealing off', wx, wy # Set the path you just traveled to a wall so you don't come back over the spot again wallGrid[wx][wy] = 1 # Search in all directions pathFind (x, y+2, x, y+1, wallGrid) pathFind (x, y-2, x, y-1, wallGrid) pathFind (x-2, y, x-1, y, wallGrid) pathFind (x+2, y, x+1, y, wallGrid) def main (): global valid list = [] dupes = 0 size = 3 maxsize = 4*size + 2 halfsize = 2*size + 1 for i in xrange(1000): valid = False #print 'start' # Generate wall grid 0's for clear, 1's for walls # start with all 0's, then fill in the start area wall (7,1) wallGrid = [[0 for n in xrange(-halfsize, halfsize, 1)] for n in xrange (-halfsize, halfsize, 1)] for y in xrange(-halfsize, halfsize, 1) for x in xrange(-halfsize, halfsize, 1) if x wallGrid[x][y] = 1 wallGrid[0][4] = 1 wallGrid[0][8] = 1 wallGrid[1][3] = 1 wallGrid[1][9] = 1 wallGrid[2][2] = 1 wallGrid[2][10] = 1 wallGrid[3][1] = 1 wallGrid[3][11] = 1 wallGrid[4][0] = 1 #wallGrid[6][0] = 1 wallGrid[7][1] = 1 wallGrid[7][11] = 1 wallGrid[8][2] = 1 wallGrid[8][10] = 1 wallGrid[9][3] = 1 wallGrid[9][9] = 1 wallGrid[10][4] = 1 wallGrid[10][8] = 1 map = [] mapGen (map, wallGrid) pathFind (6, 1, 6, 0, wallGrid) # Start at the start and fill in the wall behind you #for m in xrange (10): #print wallGrid[m] if valid == True: #print map if map in list: print 'Already in list, skipping.' dupes = dupes + 1 else: #print 'Another map in the list.' list.append (map) for m in xrange (len(list)): print 'hashtable.add ( tileMap, "', m, '", "',list[m], '")' #print 'Uniques:', len(list) #print 'Dupes:', dupes if __name__ == '__main__': main()