# Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 4.92 KB  |  hits: 9  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #!/usr/bin/env python
2.
3. from sys import stdout
4. from math import exp
5. from numpy import zeros
6. from array import array
7. import random
8. from sys import exit
9.
10. #base classes
11. class Grid:
12.
13.     def __init__(self, sizeX, sizeY):
14.         self.grid = []
15.         for i in range(sizeX):
16.             newGrid = []
17.             for j in range(sizeY):
18.                 newGrid.append(Cell())
19.             self.grid.append(newGrid)
20.
21.     def printGrid(self):
22.         for line in self.grid:
23.             #newString = ''
24.             for item in line:
25.                 stdout.write(str(item.getGrid()) + ' ')
26.             print ''
27.
28.     def getCellLine(self, i, j, k):
29.         return self.grid[i][j].getLine(k)
30.
31.     def setCell(self, i, j, value):
32.         self.grid[i][j].setCell(value)
33.
34.     def setSubCell(self, i, j, k, L, value):
35.         self.grid[i][j].setSubCell(k, L, value)
36.
37. class Cell:
38.
39.     def __init__(self, sizeX=10, sizeY=10):
40.         self.grid = (zeros((10, 10), dtype=int))
41.
42.     def getGrid(self):
43.         return self.grid
44.
45.     def getLine(self, index):
46.         return self.grid[index]
47.
48.     def setSubCell(self, i, j, value):
49.         self.grid[i][j] = value
50.
51.     def setCell(self, value):
52.         for i in range(len(self.grid)):
53.             for j in range(len(self.grid)):
54.                 self.grid[i][j] = value
55.
56. #functions
57. def createRocksExp(grid, sizeX, sizeY, s=-8.30, L=6.84):
58.     rockSizes = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
59.     numRocks = []
60.     prevTotal = 0
61.     rockSizes.reverse()
62.     for i in rockSizes:
63.         numRocks.append((L * exp(i * s)) - prevTotal)
64.         prevTotal = (L * exp(i * s)) + prevTotal
65.     # rockSizes.reverse()
66.     # numRocks.reverse()
67.     #print numRocks
68.     for i in range(sizeX):
69.         for j in range(sizeY):
70.             random.seed()
71.             #if(random.random() < numRocks[0]):
72.             #    grid.setCell(i, j, 5)
73.             for size in rockSizes:
74.                 rockIndex = rockSizes.index(size)
75.                 if(random.random() < numRocks[rockIndex]):
76.                     #add code for if indices spill over an edge and can't be
77.                     #corrected
78.                     acceptable = False
79.                     rangeX = []
80.                     rangeY = []
81.                     while(acceptable == False):
82.                         shapeX = random.randint(1, (10 * rockSizes[rockIndex]))
83.                         shapeY = int((10 * rockSizes[rockIndex]) / shapeX)
84.                         remainder = (10 * rockSizes[rockIndex]) - (shapeX * shapeY)
85.                         indexX = random.randint(0, 9)
86.                         indexY = random.randint(0, 9)
87.
88.                         if(indexX + shapeX < 10):
89.                             rangeX = ([index for index in range(indexX, indexX + shapeX)]
90.                                 if [index for index in range(indexX, indexX + shapeX)]
91.                                 else [indexX])
92.                         elif(indexX - shapeX >= 0):
93.                             rangeX = ([index for index in range(indexX - shapeX, indexX)]
94.                                 if [index for index in range(indexX - shapeX, indexX)]
95.                                 else [indexX])
96.                         else:
97.                             continue
98.                         if(indexY + shapeY < 10):
99.                             rangeY = ([index for index in range(indexY, indexY + shapeY)]
100.                                 if [index for index in range(indexY, indexY + shapeY)]
101.                                 else [indexY])
102.                         elif(indexY - shapeY >= 0):
103.                             rangeY = ([index for index in range(indexY - shapeY, indexY)]
104.                                 if [index for index in range(indexY - shapeY, indexY)]
105.                                 else [indexY])
106.                         else:
107.                             continue
108.                         acceptable = True
109.
110.                     ##print 'Editing Block ' + str(i) + ', ' + str(j)
111.                     for k in rangeX:
112.                         for m in rangeY:
113.                             grid.setSubCell(i, j, k, m, 5)
114.                     #        #we'll ignore the remainder for now, this is enough hurt as
115.                     #        #is
116.
117. def createFile(writeFile, sizeX, sizeY, grid):
118.     for i in range(sizeX):
119.         for k in range(10):
120.             for j in range(sizeY):
121.                 writeFile.write(str(grid.getCellLine(i, j, k)).replace('[', '')
122.                                 .replace(']', ' '))
123.             writeFile.write('\n')
124.
125. if __name__ == '__main__':
126.     sizeX = 84
127.     sizeY = 84
128.     newWorld = Grid(sizeX,sizeY)
129.     createRocksExp(newWorld, sizeX, sizeY)
130.     #newWorld.printGrid()
131.     with open('world.pgm', 'w') as writeFile:
132.         writeFile.write('P2\n# world.pgm\n' + str(sizeX * 10) + ' '
133.                         + str(sizeY * 10) +'\n5\n')
134.         createFile(writeFile, sizeX, sizeY, newWorld)
135.     exit()