Welcome! Here you can paste sources and general debugging text, You can even set yourself a password if you want to keep it just for yourself.

Posted by letters on November Thu 2nd 4:38 PM - Never Expires - Views: 80

  1. #! /usr/bin/python
  2. import numpy as np
  3.  
  4. class Game(object):
  5.  
  6.     def __init__(self, seed):
  7.         self.maxcol, self.maxline = (0,0)
  8.         self.fill_board(seed)
  9.  
  10.     def fill_board(self, seed):
  11.         self.maxline = len(seed)
  12.         for line in seed:
  13.             self.maxcol = max(len(line),self.maxcol)
  14.  
  15.         limit_cell = Cell(Cell.empty,Cell.limit)
  16.  
  17.         self.board = np.full((self.maxline+2, self.maxcol+2), limit_cell)
  18.  
  19.         for line_index in range(self.maxline):
  20.             line = seed[line_index]
  21.             for col_index in range(self.maxcol):
  22.                 if col_index < len(line):
  23.                     value = seed[line_index][col_index]
  24.                 else:
  25.                     value = Cell.empty
  26.                 c = Cell(value,(line_index,col_index))
  27.                 self.board[line_index][col_index] = c
  28.  
  29.     def get_neighbours(self, cell):
  30.         positions = {'N':(-1,0),'S':(+1,0),'w':(0,-1),'E':(0,+1),'NE':(-1,1),'NW':(-1,-1),'SE':(+1,+1),'SW':(+1,-1),}
  31.         c_line, c_col = cell.position
  32.  
  33.         def search_coords(coords):
  34.             n_list = []
  35.             for c in coords:
  36.                 d_line, d_col = c
  37.                 n = self.board[c_line+d_line][c_col+d_col]
  38.                 if not n.is_empty() and not n.is_limit():
  39.                     n_list.append(n.value)
  40.             return n_list
  41.  
  42.         return search_coords(positions.values())
  43.  
  44.     def step(self):
  45.         for line in self.board:
  46.             for cell in line:
  47.                 if not cell.is_limit():
  48.                     neighbours = self.get_neighbours(cell)
  49.                     print "testando celula %s - qtd vizinhos %s" % (cell.to_string(), len(neighbours))
  50.                     if cell.is_dead():
  51.                         if len(neighbours) == 3:
  52.                             print "decidimos revivela"
  53.                             self.born_cell(cell,neighbours)
  54.                     else:
  55.                         if (len(neighbours) < 2 or len(neighbours) > 3):
  56.                             print "decidimos matala"
  57.                             self.kill_cell(cell)
  58.  
  59.     def kill_cell(self, cell):
  60.         cell.value = Cell.empty
  61.  
  62.     def born_cell(self,cell,neighbours):
  63.         print "Dos vizinhos %s - o maior: %s" % (neighbours, max(neighbours))
  64.         cell.value = max(neighbours)
  65.  
  66.     def get_flag(self):
  67.         result = ''
  68.         for line in self.board:
  69.             for cell in line:
  70.                 if not cell.is_empty():
  71.                     result += cell.value
  72.         return 'shelter{%s}' % result
  73.  
  74. class Cell(object):
  75.  
  76.     empty = ' '
  77.     limit = (-1,-1)
  78.  
  79.     def __init__(self, value, (line, col)):
  80.         self.position = (line, col)
  81.         self.value = value
  82.  
  83.     def __str__(self):
  84.         return self.value
  85.  
  86.     def to_string(self):
  87.         return "['%s'%s]" % (self.value, self.position)
  88.  
  89.     def asord(self):
  90.         return ord(self.value)
  91.  
  92.     def is_limit(self):
  93.         return self.position == Cell.limit
  94.  
  95.     def is_empty(self):
  96.         return self.value == ' '
  97.  
  98.     def is_dead(self):
  99.         return (not self.is_limit()) and self.is_empty()
  100.  
  101.  
  102. def get_seed():
  103.     with open('/root/Documentos/pentest/Life/input.txt','r') as seed:
  104.         result = []
  105.         lines_list = seed.readlines()
  106.         for line_index, line in enumerate(lines_list):
  107.             line = line.replace('\n','')
  108.             result.append([])
  109.             for letter_index, letter in enumerate(line):
  110.                 result[line_index].append(letter)
  111.  
  112.     np.savetxt('seed.txt',result,'%s')
  113.     return result
  114.  
  115. if __name__ == "__main__":
  116.     print 'comecando'
  117.     game = Game(get_seed())
  118.  
  119.     for n in range(3):
  120.         print 'geracao %s' % n
  121.         game.step()
  122.  
  123.     np.savetxt('output_game.txt',game.board,'%s')
  124.  
  125.     print game.get_flag()
Language:
To highlight particular lines, prefix each line with @@




© 2018  YSblue.com.br (Implemented and Adapted) - Powered by PASTE 1.0