Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.89 KB | None | 0 0
  1. #!/usr/local/bin/python3
  2.  
  3. import random
  4. import sys
  5.  
  6. class CellRoom:
  7.  
  8.   def generateGame(self):
  9.     ## Constants
  10.     self.UNDEFINED = 0
  11.     self.FROM_NOWHERE = 1
  12.     self.FROM_NORTH = 2
  13.     self.FROM_EAST = 3
  14.     self.FROM_SOUTH = 4
  15.     self.FROM_WEST = 5
  16.  
  17.     self.LEFT = 0
  18.     self.RIGHT = 1
  19.     ln = input()
  20.     tests = int(ln)
  21.     total = 0
  22.     for t in range(tests):
  23.         sys.stderr.write(str(t))
  24.         ln = input()
  25.         #print(ln)
  26.         w, h = (int(s) for s in ln.split())
  27.         matrix = [[int(j) for j in input().split()] for i in range(h)]
  28.         self.GAME_WIDTH = w
  29.         self.GAME_HEIGHT = h
  30.         ansrow = -1
  31.         anscol = -1
  32.         best = 0
  33.         beststring = ''
  34.         for it in range(500):
  35.             self.initGame()
  36.             for i in range(10):
  37.               self.permutate()
  38.             ##self.logGameWithPath()
  39.             ##self.logGameWithArrow()
  40.             for i in range(20):
  41.               self.start = self.moveExtremity(self.start)
  42.             #self.logGameWithPath()
  43.             #self.logGameWithArrow()
  44.             path = self.logFriendly()
  45.             cr = self.start[0];
  46.             cc = self.start[1]
  47.             ans = 1
  48.             ans = ans + (ans % matrix[cr][cc])
  49.             for i in range(self.GAME_WIDTH * self.GAME_HEIGHT - 1):
  50.                 if path[i] == 'N':
  51.                     cr-=1
  52.                 elif path[i] == 'S':
  53.                     cr+=1
  54.                 elif path[i] == 'W':
  55.                     cc-=1
  56.                 else:
  57.                     cc+=1
  58.                 #print(str(cr) + ' ' + str(cc))
  59.                 ans = ans + (ans % matrix[cr][cc])
  60.             if ans > best:
  61.                 ansrow = self.start[0]
  62.                 anscol = self.start[1]
  63.                 beststring = path
  64.                 best = ans
  65.             #self.verifyGame()
  66.         print(str(anscol + 1) + ' ' + str(ansrow + 1))
  67.         print(beststring)
  68.         print(best)
  69.         total += best  
  70.     sys.stderr.write('TOTAL ' + str(total))
  71.    
  72.   def logFriendly(self):
  73.     #print ('' + str(self.start[1] + 1) + ' ' + str(self.start[0] + 1) + '')
  74.     crow = self.start[0]
  75.     ccol = self.start[1]
  76.     ans = 1
  77.     ret = ''
  78.     for i in range(self.GAME_WIDTH * self.GAME_HEIGHT - 1):
  79.         a = -1
  80.         if (crow > 0) and (self.gameGrid[crow - 1][ccol] == self.FROM_SOUTH):
  81.             a = 0
  82.         if (crow < self.GAME_HEIGHT - 1) and (self.gameGrid[crow + 1][ccol] == self.FROM_NORTH):
  83.             a = 1
  84.         if (ccol > 0) and (self.gameGrid[crow][ccol - 1] == self.FROM_EAST):
  85.             a = 2
  86.         if (ccol < self.GAME_WIDTH - 1) and (self.gameGrid[crow][ccol + 1] == self.FROM_WEST):
  87.             a = 3
  88.         if a == -1:
  89.             print('greska')
  90.         elif a == 0:
  91.             ret += 'N'
  92.             crow -= 1
  93.         elif a == 1:
  94.             ret += 'S'
  95.             crow += 1
  96.         elif a == 2:
  97.             ret += 'W'
  98.             ccol -= 1
  99.         else:
  100.             ret += 'E'
  101.             ccol += 1
  102.     return ret
  103.        
  104.   def logGameWithPath(self):
  105.     #print ('game width: ' + str(self.GAME_WIDTH))
  106.     #print ('game height: ' + str(self.GAME_HEIGHT))
  107.     print ('' + str(self.start[1] + 1) + ' ' + str(self.start[0] + 1) + '')
  108.  
  109.     gameText = ''
  110.  
  111.     for i in range(len(self.gameGrid)):
  112.       for j in range(len(self.gameGrid[i])):
  113.         if (self.gameGrid[i][j] == self.FROM_NORTH) or ((i > 0) and (self.gameGrid[i - 1][j] == self.FROM_SOUTH)):
  114.           gameText = gameText + ' |'
  115.         else:
  116.           gameText = gameText + '  '
  117.       gameText = gameText + ' \n'
  118.       for j in range(len(self.gameGrid[i])):
  119.         if (self.gameGrid[i][j] == self.FROM_WEST) or ((j > 0) and (self.gameGrid[i][j - 1] == self.FROM_EAST)):
  120.           gameText = gameText + '-O'
  121.         else:
  122.           gameText = gameText + ' O'
  123.       gameText = gameText + ' \n'
  124.  
  125.     for j in range(len(self.gameGrid[i])):
  126.       gameText = gameText + '  '
  127.     gameText = gameText + ' \n'
  128.  
  129.     print (gameText)
  130.  
  131.   def logGameWithArrow(self):
  132.     print ('' + str(self.start[1] + 1) + ' ' + str(self.start[0] + 1) + '')
  133.     gameText = ''
  134.  
  135.     for gameLine in self.gameGrid:
  136.       for j in gameLine:
  137.         if j == self.FROM_NOWHERE:
  138.           gameText = gameText + 'X'
  139.         elif j == self.FROM_NORTH:
  140.           gameText = gameText + 'V'
  141.         elif j == self.FROM_EAST:
  142.           gameText = gameText + '('
  143.         elif j == self.FROM_SOUTH:
  144.           gameText = gameText + '^'
  145.         elif j == self.FROM_WEST:
  146.           gameText = gameText + ')'
  147.       gameText = gameText + '\n'
  148.  
  149.     print (gameText)
  150.  
  151.   def moveExtremity(self, extremity):
  152.     ## Search the points.
  153.     possibleNewOrigins = []
  154.     if ((extremity[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[extremity[0] + 1][extremity[1]] != self.FROM_NORTH)):
  155.       possibleNewOrigins.append(self.FROM_NORTH)
  156.       besidePoint = [extremity[0] + 1, extremity[1]]
  157.     elif ((extremity[1] > 0) and (self.gameGrid[extremity[0]][extremity[1] - 1] != self.FROM_EAST)):
  158.       possibleNewOrigins.append(self.FROM_EAST)
  159.       besidePoint = [extremity[0], extremity[1] - 1]
  160.     elif ((extremity[0] > 0) and (self.gameGrid[extremity[0] - 1][extremity[1]] != self.FROM_SOUTH)):
  161.       possibleNewOrigins.append(self.FROM_SOUTH)
  162.       besidePoint = [extremity[0] - 1, extremity[1]]
  163.     elif ((extremity[1] < self.GAME_WIDTH - 1) and (self.gameGrid[extremity[0]][extremity[1] + 1] != self.FROM_WEST)):
  164.       possibleNewOrigins.append(self.FROM_WEST)
  165.       besidePoint = [extremity[0], extremity[1] + 1]
  166.  
  167.     besidePointNewOrigin = possibleNewOrigins[random.randint(0, len(possibleNewOrigins) - 1)]
  168.  
  169.     if besidePointNewOrigin == self.FROM_NORTH:
  170.       besidePoint = [extremity[0] + 1, extremity[1]]
  171.     elif besidePointNewOrigin == self.FROM_EAST:
  172.       besidePoint = [extremity[0], extremity[1] - 1]
  173.     elif besidePointNewOrigin == self.FROM_SOUTH:
  174.       besidePoint = [extremity[0] - 1, extremity[1]]
  175.     elif besidePointNewOrigin == self.FROM_WEST:
  176.       besidePoint = [extremity[0], extremity[1] + 1]
  177.  
  178.     ##print ('New start: [' + str(extremity[0]) + ', ' + str(extremity[1]) + ']')
  179.     ##print ('besidePoint: [' + str(besidePoint[0]) + ', ' + str(besidePoint[1]) + ']')
  180.  
  181.     if self.gameGrid[besidePoint[0]][besidePoint[1]] == self.FROM_NORTH:
  182.       newExtremity = [besidePoint[0] - 1, besidePoint[1]]
  183.     elif self.gameGrid[besidePoint[0]][besidePoint[1]] == self.FROM_EAST:
  184.       newExtremity = [besidePoint[0], besidePoint[1] + 1]
  185.     elif self.gameGrid[besidePoint[0]][besidePoint[1]] == self.FROM_SOUTH:
  186.       newExtremity = [besidePoint[0] + 1, besidePoint[1]]
  187.     elif self.gameGrid[besidePoint[0]][besidePoint[1]] == self.FROM_WEST:
  188.       newExtremity = [besidePoint[0], besidePoint[1] - 1]
  189.  
  190.     self.reversePath(extremity, newExtremity)
  191.  
  192.     self.gameGrid[besidePoint[0]][besidePoint[1]] = besidePointNewOrigin
  193.     self.gameGrid[newExtremity[0]][newExtremity[1]] = self.FROM_NOWHERE
  194.     ##print ('extremity: [' + str(newExtremity[0]) + ', ' + str(newExtremity[1]) + ']')
  195.  
  196.     return newExtremity
  197.  
  198.   def reversePath(self, start, end):
  199.     currentPoint = start
  200.     ##print ('start: [' + str(currentPoint[0]) + ', ' + str(currentPoint[1]) + ']')
  201.     ##print ('end: [' + str(end[0]) + ', ' + str(end[1]) + ']')
  202.     while (currentPoint[0] != end[0]) or (currentPoint[1] != end[1]):
  203.       ##print ('currentPoint: [' + str(currentPoint[0]) + ', ' + str(currentPoint[1]) + ']')
  204.       if (currentPoint[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[currentPoint[0] + 1][currentPoint[1]] == self.FROM_NORTH) and (self.gameGrid[currentPoint[0]][currentPoint[1]] != self.FROM_SOUTH):
  205.         self.gameGrid[currentPoint[0]][currentPoint[1]] = self.FROM_SOUTH
  206.         currentPoint[0] = currentPoint[0] + 1
  207.       elif (currentPoint[1] > 0) and (self.gameGrid[currentPoint[0]][currentPoint[1] - 1] == self.FROM_EAST) and (self.gameGrid[currentPoint[0]][currentPoint[1]] != self.FROM_WEST):
  208.         self.gameGrid[currentPoint[0]][currentPoint[1]] = self.FROM_WEST
  209.         currentPoint[1] = currentPoint[1] - 1
  210.       elif (currentPoint[0] > 0) and (self.gameGrid[currentPoint[0] - 1][currentPoint[1]] == self.FROM_SOUTH) and (self.gameGrid[currentPoint[0]][currentPoint[1]] != self.FROM_NORTH):
  211.         self.gameGrid[currentPoint[0]][currentPoint[1]] = self.FROM_NORTH
  212.         currentPoint[0] = currentPoint[0] - 1
  213.       elif (currentPoint[1] < self.GAME_WIDTH - 1) and (self.gameGrid[currentPoint[0]][currentPoint[1] + 1] == self.FROM_WEST) and (self.gameGrid[currentPoint[0]][currentPoint[1]] != self.FROM_EAST):
  214.         self.gameGrid[currentPoint[0]][currentPoint[1]] = self.FROM_EAST
  215.         currentPoint[1] = currentPoint[1] + 1
  216.     ##print ('reversePath: [' + str(currentPoint[0]) + ', ' + str(currentPoint[1]) + ']')
  217.     self.gameGrid[currentPoint[0]][currentPoint[1]] = self.UNDEFINED
  218.  
  219.   def verifyGame(self):
  220.     moveCount = 0
  221.     currentPoint = [self.start[0], self.start[1]]
  222.  
  223.     isEnd = 0
  224.     while (isEnd == 0):
  225.       if (currentPoint[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[currentPoint[0] + 1][currentPoint[1]] == self.FROM_NORTH):
  226.         currentPoint[0] = currentPoint[0] + 1
  227.       elif (currentPoint[1] > 0) and (self.gameGrid[currentPoint[0]][currentPoint[1] - 1] == self.FROM_EAST):
  228.         currentPoint[1] = currentPoint[1] - 1
  229.       elif (currentPoint[0] > 0) and (self.gameGrid[currentPoint[0] - 1][currentPoint[1]] == self.FROM_SOUTH):
  230.         currentPoint[0] = currentPoint[0] - 1
  231.       elif (currentPoint[1] < self.GAME_WIDTH - 1) and (self.gameGrid[currentPoint[0]][currentPoint[1] + 1] == self.FROM_WEST):
  232.         currentPoint[1] = currentPoint[1] + 1
  233.       else:
  234.         isEnd = 1
  235.  
  236.       if isEnd == 0:
  237.         moveCount = moveCount + 1
  238.  
  239.     if moveCount == ((self.GAME_HEIGHT * self.GAME_WIDTH) - 1):
  240.       print ('OK')
  241.     else:
  242.       print ('ko!!!')
  243.  
  244.   def initGame(self):
  245.     self.gameGrid = []
  246.     for i in range(self.GAME_HEIGHT):
  247.       gameLine = []
  248.       for j in range(self.GAME_WIDTH):
  249.         gameLine.append(self.UNDEFINED)
  250.  
  251.       self.gameGrid.append(gameLine)
  252.  
  253.     self.initComplexMap()
  254.  
  255.   def initComplexMap(self):
  256.     startPoint = random.randint(0, 3)
  257.     if startPoint == 0:
  258.       self.start = [0, 0]
  259.     elif startPoint == 1:
  260.       self.start = [0, self.GAME_WIDTH - 1]
  261.     elif startPoint == 2:
  262.       self.start = [self.GAME_HEIGHT - 1, 0]
  263.     elif startPoint == 3:
  264.       self.start = [self.GAME_HEIGHT - 1, self.GAME_WIDTH - 1]
  265.  
  266.     self.gameGrid[self.start[0]][self.start[1]] = self.FROM_NOWHERE
  267.     currentPoint = [self.start[0], self.start[1]]
  268.  
  269.     while ((0 < currentPoint[0]) and (self.gameGrid[currentPoint[0] - 1][currentPoint[1]] == self.UNDEFINED)) or ((currentPoint[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[currentPoint[0] + 1][currentPoint[1]] == self.UNDEFINED)) or ((0 < currentPoint[1]) and (self.gameGrid[currentPoint[0]][currentPoint[1] - 1] == self.UNDEFINED)) or ((currentPoint[1] < self.GAME_WIDTH - 1) and (self.gameGrid[currentPoint[0]][currentPoint[1] + 1] == self.UNDEFINED)):
  270.       possibilities = []
  271.       if ((0 < currentPoint[0]) and (self.gameGrid[currentPoint[0] - 1][currentPoint[1]] == self.UNDEFINED)) and (((0 == currentPoint[1]) or (self.gameGrid[currentPoint[0] - 1][currentPoint[1] - 1] != self.UNDEFINED)) or ((currentPoint[1] == self.GAME_WIDTH - 1) or (self.gameGrid[currentPoint[0] - 1][currentPoint[1] + 1] != self.UNDEFINED))):
  272.         possibilities.append([currentPoint[0] - 1, currentPoint[1], self.FROM_SOUTH])
  273.  
  274.       if ((currentPoint[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[currentPoint[0] + 1][currentPoint[1]] == self.UNDEFINED)) and (((0 == currentPoint[1]) or (self.gameGrid[currentPoint[0] + 1][currentPoint[1] - 1] != self.UNDEFINED)) or ((currentPoint[1] == self.GAME_WIDTH - 1) or (self.gameGrid[currentPoint[0] + 1][currentPoint[1] + 1] != self.UNDEFINED))):
  275.         possibilities.append([currentPoint[0] + 1, currentPoint[1], self.FROM_NORTH])
  276.  
  277.       if ((0 < currentPoint[1]) and (self.gameGrid[currentPoint[0]][currentPoint[1] - 1] == self.UNDEFINED)) and (((0 == currentPoint[0]) or (self.gameGrid[currentPoint[0] - 1][currentPoint[1] - 1] != self.UNDEFINED)) or ((currentPoint[0] == self.GAME_HEIGHT - 1) or (self.gameGrid[currentPoint[0] + 1][currentPoint[1] - 1] != self.UNDEFINED))):
  278.         possibilities.append([currentPoint[0], currentPoint[1] - 1, self.FROM_EAST])
  279.  
  280.       if ((currentPoint[1] < self.GAME_WIDTH - 1) and (self.gameGrid[currentPoint[0]][currentPoint[1] + 1] == self.UNDEFINED)) and (((0 == currentPoint[0]) or (self.gameGrid[currentPoint[0] - 1][currentPoint[1] + 1] != self.UNDEFINED)) or ((currentPoint[0] == self.GAME_HEIGHT - 1) or (self.gameGrid[currentPoint[0] + 1][currentPoint[1] + 1] != self.UNDEFINED))):
  281.         possibilities.append([currentPoint[0], currentPoint[1] + 1, self.FROM_WEST])
  282.  
  283.       possibility = possibilities.pop(random.randint(0, len(possibilities) - 1))
  284.       currentPoint = [possibility[0], possibility[1]]
  285.       self.gameGrid[possibility[0]][possibility[1]] = possibility[2]
  286.  
  287.   def initSimpleMap(self):
  288.     direction = self.RIGHT
  289.  
  290.     if random.randint(0, 1) == 0:
  291.       for i in range(self.GAME_HEIGHT):
  292.         if direction == self.RIGHT:
  293.           self.gameGrid[i][0] = self.FROM_NORTH
  294.           for j in range(1, self.GAME_WIDTH):
  295.             self.gameGrid[i][j] = self.FROM_WEST
  296.           direction = self.LEFT
  297.         else:
  298.           for j in range(self.GAME_WIDTH - 1):
  299.             self.gameGrid[i][j] = self.FROM_EAST
  300.           self.gameGrid[i][self.GAME_WIDTH - 1] = self.FROM_NORTH
  301.           direction = self.RIGHT
  302.  
  303.         self.gameGrid.append(gameLine)
  304.  
  305.       self.gameGrid[0][0] = self.FROM_NOWHERE
  306.     else:
  307.       for j in range(self.GAME_WIDTH):
  308.         if direction == self.RIGHT:
  309.           self.gameGrid[0][j] = self.FROM_WEST
  310.           for i in range(1, self.GAME_HEIGHT):
  311.             self.gameGrid[i][j] = self.FROM_NORTH
  312.           direction = self.LEFT
  313.         else:
  314.           for i in range(self.GAME_HEIGHT - 1):
  315.             self.gameGrid[i][j] = self.FROM_SOUTH
  316.           self.gameGrid[self.GAME_HEIGHT - 1][j] = self.FROM_WEST
  317.           direction = self.RIGHT
  318.  
  319.       self.gameGrid[0][0] = self.FROM_NOWHERE
  320.  
  321.   def listPermutation(self):
  322.     self.permutableZones = []
  323.     for i in range(self.GAME_HEIGHT - 1):
  324.       for j in range(self.GAME_WIDTH - 1):
  325.         if (self.gameGrid[i + 1][j] == self.FROM_NORTH) and (self.gameGrid[i][j + 1] == self.FROM_SOUTH):
  326.           self.permutableZones.append([[i + 1, j], [i, j + 1]])
  327.         elif (self.gameGrid[i][j] == self.FROM_SOUTH) and (self.gameGrid[i + 1][j + 1] == self.FROM_NORTH):
  328.           self.permutableZones.append([[i, j], [i + 1, j + 1]])
  329.         elif (self.gameGrid[i][j] == self.FROM_EAST) and (self.gameGrid[i + 1][j + 1] == self.FROM_WEST):
  330.           self.permutableZones.append([[i, j], [i + 1, j + 1]])
  331.         elif (self.gameGrid[i][j + 1] == self.FROM_WEST) and (self.gameGrid[i + 1][j] == self.FROM_EAST):
  332.           self.permutableZones.append([[i, j + 1], [i + 1, j]])
  333.  
  334.   def permutate(self):
  335.     self.listPermutation()
  336.  
  337.     if len(self.permutableZones) > 0:
  338.       permutation = self.permutableZones.pop(random.randint(0, len(self.permutableZones) - 1))
  339.       start = permutation[0]
  340.       end = permutation[1]
  341.       ##print ('Entry of the loop: (' + str(start[0]) + ', ' + str(start[1]) + ')')
  342.       ##print ('Exit of the loop: (' + str(end[0]) + ', ' + str(end[1]) + ')')
  343.       if self.isLoop(end, start):
  344.         self.findPermutation(start, end)
  345.       else:
  346.         end = permutation[0]
  347.         start = permutation[1]
  348.         if not self.isLoop(end, start):
  349.           print ('Wrong!')
  350.         self.findPermutation(start, end)
  351.  
  352.   def isInLoop(self, searchedPoint):
  353.     found = False
  354.     for point in self.currentLoop:
  355.       if (searchedPoint[0] == point[0]) and (searchedPoint[1] == point[1]):
  356.         found = True
  357.  
  358.     return found
  359.  
  360.   def isLoop(self, originalPoint, destination):
  361.     self.currentLoop = []
  362.  
  363.     point = []
  364.     point.append(originalPoint[0])
  365.     point.append(originalPoint[1])
  366.     self.currentLoop.append([originalPoint[0], originalPoint[1]])
  367.     while ((point[0] != destination[0]) or (point[1] != destination[1])) and (self.gameGrid[point[0]][point[1]] != self.FROM_NOWHERE):
  368.       ##print ('Loop point: (' + str(point[0]) + ', ' + str(point[1]) + ')')
  369.       newY = point[0]
  370.       newX = point[1]
  371.       if self.gameGrid[point[0]][point[1]] == self.FROM_SOUTH:
  372.         newY = point[0] + 1
  373.       elif self.gameGrid[point[0]][point[1]] == self.FROM_NORTH:
  374.         newY = point[0] - 1
  375.       elif self.gameGrid[point[0]][point[1]] == self.FROM_WEST:
  376.         newX = point[1] - 1
  377.       elif self.gameGrid[point[0]][point[1]] == self.FROM_EAST:
  378.         newX = point[1] + 1
  379.       point[0] = newY
  380.       point[1] = newX
  381.       self.currentLoop.append([newY, newX])
  382.  
  383.     return ((point[0] == destination[0]) and (point[1] == destination[1]))
  384.  
  385.   def findPermutation(self, start, end):
  386.     self.findIntersections()
  387.     if len(self.intersections) > 0:
  388.       self.modifyIntersection(start, end)
  389.  
  390.   def findIntersections(self):
  391.     self.intersections = []
  392.     for i in range(1, len(self.currentLoop) - 1):
  393.       point = self.currentLoop[i]
  394.       if self.gameGrid[point[0]][point[1]] == self.FROM_NORTH:
  395.         if (0 < point[1]) and (self.gameGrid[point[0] - 1][point[1] - 1] == self.FROM_SOUTH) and not self.isInLoop([point[0] - 1, point[1] - 1]):
  396.           self.intersections.append([[point[0], point[1]], [point[0] - 1, point[1] - 1]])
  397.         elif (point[1] < self.GAME_WIDTH - 1) and (self.gameGrid[point[0] - 1][point[1] + 1] == self.FROM_SOUTH) and not self.isInLoop([point[0] - 1, point[1] + 1]):
  398.           self.intersections.append([[point[0], point[1]], [point[0] - 1, point[1] + 1]])
  399.  
  400.       elif self.gameGrid[point[0]][point[1]] == self.FROM_SOUTH:
  401.         if (0 < point[1]) and (self.gameGrid[point[0] + 1][point[1] - 1] == self.FROM_NORTH) and not self.isInLoop([point[0] + 1, point[1] - 1]):
  402.           self.intersections.append([[point[0], point[1]], [point[0] + 1, point[1] - 1]])
  403.         elif (point[1] < self.GAME_WIDTH - 1) and (self.gameGrid[point[0] + 1][point[1] + 1] == self.FROM_NORTH) and not self.isInLoop([point[0] + 1, point[1] + 1]):
  404.           self.intersections.append([[point[0], point[1]], [point[0] + 1, point[1] + 1]])
  405.  
  406.       elif self.gameGrid[point[0]][point[1]] == self.FROM_WEST:
  407.         if (0 < point[0]) and (self.gameGrid[point[0] - 1][point[1] - 1] == self.FROM_EAST) and not self.isInLoop([point[0] - 1, point[1] - 1]):
  408.           self.intersections.append([[point[0], point[1]], [point[0] - 1, point[1] - 1]])
  409.         elif (point[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[point[0] + 1][point[1] - 1] == self.FROM_EAST) and not self.isInLoop([point[0] + 1, point[1] - 1]):
  410.           self.intersections.append([[point[0], point[1]], [point[0] + 1, point[1] - 1]])
  411.  
  412.       elif self.gameGrid[point[0]][point[1]] == self.FROM_EAST:
  413.         if (0 < point[0]) and (self.gameGrid[point[0] - 1][point[1] + 1] == self.FROM_WEST) and not self.isInLoop([point[0] - 1, point[1] + 1]):
  414.           self.intersections.append([[point[0], point[1]], [point[0] - 1, point[1] + 1]])
  415.         elif (point[0] < self.GAME_HEIGHT - 1) and (self.gameGrid[point[0] + 1][point[1] + 1] == self.FROM_WEST) and not self.isInLoop([point[0] + 1, point[1] + 1]):
  416.           self.intersections.append([[point[0], point[1]], [point[0] + 1, point[1] + 1]])
  417.  
  418.   ## Permutate the connections of path.
  419.   ## It receives and returns a valid map.
  420.   def modifyIntersection(self, start, end):
  421.     ##self.logGameWithPath()
  422.     ##self.printGameOld()
  423.     intersection = self.intersections[random.randint(0, len(self.intersections) - 1)]
  424.     self.modifyPath([start, end])
  425.     self.modifyPath(intersection)
  426.  
  427.   def modifyPath(self, intersection):
  428.     ##print ('modifyPath: (' + str(intersection[0][0]) + ', ' + str(intersection[0][1]) + ') with (' + str(intersection[1][0]) + ', ' + str(intersection[1][1]) + ')')
  429.     firstPoint = self.gameGrid[intersection[0][0]][intersection[0][1]]
  430.     secondPoint = self.gameGrid[intersection[1][0]][intersection[1][1]]
  431.  
  432.     if (self.gameGrid[intersection[0][0]][intersection[0][1]] == self.FROM_NORTH) or (self.gameGrid[intersection[0][0]][intersection[0][1]] == self.FROM_SOUTH):
  433.       if (intersection[0][1] < intersection[1][1]):
  434.         firstPoint = self.FROM_EAST
  435.         secondPoint = self.FROM_WEST
  436.       else:
  437.         firstPoint = self.FROM_WEST
  438.         secondPoint = self.FROM_EAST
  439.     if (self.gameGrid[intersection[0][0]][intersection[0][1]] == self.FROM_EAST) or (self.gameGrid[intersection[0][0]][intersection[0][1]] == self.FROM_WEST):
  440.       if (intersection[0][0] < intersection[1][0]):
  441.         firstPoint = self.FROM_SOUTH
  442.         secondPoint = self.FROM_NORTH
  443.       else:
  444.         firstPoint = self.FROM_NORTH
  445.         secondPoint = self.FROM_SOUTH
  446.  
  447.     self.gameGrid[intersection[0][0]][intersection[0][1]] = firstPoint
  448.     self.gameGrid[intersection[1][0]][intersection[1][1]] = secondPoint
  449.  
  450. cellRoom = CellRoom()
  451. cellRoom.generateGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement