2014-01-15 9 views
0

Здравствуйте, может кто-нибудь, пожалуйста, опишите алгоритм этого кода. Что-то вроде pythonTutor.com, но сайт не может работать с внешними файлами.Двумерный массив из файла

j = 0 
self.pole = [] 
while True: 
      newLine = t.readline().strip() 
      if newLine == '': 
       break 
      for i in range(len(newLine)): 
       if newLine[i] == 'v': 
        self.vajce = Vajce(i*40+20, j*40+20) #this is class witch i am calling 
        newLine = riadok.replace('v','.') 
       if newLine[i] == 'z': 
        self.dvere[j,i] = Dvere(i*40, j*40) # another class 
      self.pole.append(list(newLine)) 
      j += 1 

из кода

import tkinter, math 

class Vajce: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def kresli(self, g): 
     self.g = g 
     self.id = g.create_oval(self.x-10,self.y-13,self.x+10,self.y+13,fill='white') 

    def pohni(self, dx, dy): 
     self.x += dx 
     self.y += dy 
     self.g.move(self.id, dx, dy) 

class Klucik: 
    def __init__(self, x, y, polygon): 
     self.x = x 
     self.y = y 
     self.polygon = polygon 

    def kresli(self, g): 
     self.g = g 
     self.id = self.g.create_polygon(self.polygon,fill='red') 
     self.g.move(self.id, self.x, self.y) 

    def zmaz(self): 
     self.g.delete(self.id) 
     self.x = -100 

    def blizko(self, x, y):  # bod (x, y) nie je od kľúčika ďalej ako 20 
     return math.sqrt((self.x-x)**2+(self.y-y)**2) <= 20 

class Dvere: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def kresli(self, g): 
     self.g = g 
     self.id = g.create_rectangle(self.x,self.y,self.x+40,self.y+40,fill='blue') 

    def zmen_stav(self, stav): 
     self.g.itemconfig(self.id, fill=['blue','green','seagreen'][stav]) 

class Program: 
    def __init__(self, meno_suboru): 
     t = open(meno_suboru) 
     self.pole = [] 
     self.dvere = {}   # ako slovník 
     j = 0 
     while True: 
      riadok = t.readline().strip() # odstráň zbytočné medzery a koniec riadka 
      if riadok == '': 
       break 
      for i in range(len(riadok)): 
       if riadok[i] == 'v': 
        self.vajce = Vajce(i*40+20, j*40+20) 
        riadok = riadok.replace('v','.') 
       if riadok[i] == 'z': 
        self.dvere[j,i] = Dvere(i*40, j*40) # pridá do slovníka 
      self.pole.append(list(riadok)) 
      j += 1 
     print(self.pole) 
     vyska = j*40     # pre výšku grafickej plochy 
     poly = t.readline().split() 
     for i in range(len(poly)): 
      poly[i] = int(poly[i]) 
     self.klucik = [] 
     while True: 
      riadok = t.readline() 
      if riadok == '': 
       break 
      x,y = riadok.split() 
      self.klucik.append(Klucik(int(x),int(y),poly)) 
     t.close() 
     sirka = len(self.pole[0])*40 # šírka grafickej plochy 
     self.g = tkinter.Canvas(bg='seagreen',width=sirka,height=vyska) 
     self.g.pack() 
     self.kresli() 
     self.pocitadlo = 0  # počítadlo zdvihnutých klúčikov 
     self.g.bind_all('<Key>', self.klaves) 
     tkinter.mainloop() 

    def kresli(self): 
     for i in range(len(self.pole)): 
      for j in range(len(self.pole[0])): 
       x,y = j*40,i*40 
       p = self.pole[i][j] 
       if p == 'x': 
        self.g.create_rectangle(x,y,x+40,y+40,fill='brown') 
       if p == 'k': 
        self.g.create_rectangle(x,y,x+40,y+40,fill='yellow') 
       if p == 'z': 
        self.dvere[i,j].kresli(self.g) 
     for k in self.klucik: 
      k.kresli(self.g) 
     self.vajce.kresli(self.g) 

    def klaves(self, e): 
     if e.keysym == 'Up': 
      self.pohni(0, -4) 
     if e.keysym == 'Down': 
      self.pohni(0, 4) 
     if e.keysym == 'Left': 
      self.pohni(-4, 0) 
     if e.keysym == 'Right': 
      self.pohni(4, 0) 

    def pohni(self, dx, dy):  # zavolá sa z udalosti stlačenie šípky 
     x, y = self.vajce.x+dx, self.vajce.y+dy 
     for i in range(len(self.klucik)): 
      if self.klucik[i].blizko(x,y): 
       self.pocitadlo += 1 
       self.klucik[i].zmaz() 
     if self.pocitadlo > 0: 
      for i in range(len(self.pole)): 
       for j in range(len(self.pole[0])): 
        if self.pole[i][j] == 'z': 
         self.pole[i][j] = 'y' 
         self.dvere[i,j].zmen_stav(1) 
     r, s = y//40, x//40 
     if self.pole[r][s] in 'xz': 
      return 
     self.vajce.pohni(dx,dy) 
     if self.pole[r][s] == 'y':  # odomknuté dvere 
      #print('dvere',r,s) 
      self.pocitadlo -= 1 
      self.dvere[r,s].zmen_stav(2) 
      self.pole[r][s] = '.' 
      if self.pocitadlo == 0: 
       for i in range(len(self.pole)): 
        for j in range(len(self.pole[0])): 
         if self.pole[i][j] == 'y': 
          self.pole[i][j] = 'z' 
          self.dvere[i,j].zmen_stav(0) 
     if self.pole[r][s] == 'k': 
      self.g.create_text(300,70,text='HURA!!!',fill='red',font='arial 100 bold') 
      self.g.unbind_all('<Key>') 

Program('plocha.txt') 

она должна быть игра, когда вы гуляете с яйцом и собирать ключи, чтобы открыть двери.

+2

Бог, я не знаю, где код пришел, то, что он делает, что контекст, и вы хотите меня ** описать алгоритм **? – laike9m

ответ

1
t = open(meno_suboru) 
# open a file to read in 
j = 0 
# initialise line counter 
self.pole = [] 
# create list to hold things 
self.dvere = {} 
# create dictionary to hold other things 
while True: 
# loop until condition met 
    riadok = t.readline().strip() 
    # get the next line from t and strip the newline character, whitespace etc 
    if newLine == '': 
    # if we've reached a blank line 
     break 
     # we're done 
    for i in range(len(riadok)): 
    # count through the characters in the new line 
     if riadok[i] == 'v': 
     # if current character is a 'v' 
      self.vajce = Vajce(i*40+20, j*40+20)  
      # create a new Vjace object and set to self.vajce 
      raidok = riadok.replace('v','.') 
      # replace all 'v's in the line with '.'s to prevent further matches 
      # so we only ever have one self.vjace 
     if newLine[i] == 'z': 
     # if current character is a 'z' 
      self.dvere[j,i] = Dvere(i*40, j*40) 
      # same as before, but self.dvere is a dictionary so we 
      # keep any previous Dvere objects too - dictionary key 
      # is a tuple (j, i) 
    self.pole.append(list(riadok)) 
    # turn the line into a list of characters and add to the self.pole list 
    j += 1 
    # increment the line counter 

Обратите внимание, что это может быть лучше структурированы:

with open(meno_suboru) as t: not 'open()' and 'close()' 
    for j, riadok in enumerate(t): # not 'while True:' 
     # use index j and line riadok 
     ... 
     for i, char in riadok: # not 'for i in range(len(riadok)):' 
      # use index i and character char 
      ... 
Смежные вопросы