from Core.Cell import Cell
from Core.Maze import Maze 
from Builder.BuilderInterface import MazeBuilders

class TextFileMazeBuilder(MazeBuilders):

    def build_from_file(self, filename):
        grid = []
        start = None
        exit = None

        with open(filename, "r", encoding="utf-8") as f:
            lines = [line.rstrip("\n") for line in f]
        
        for y, line in enumerate(lines): 
            row = []
            for x, ch in enumerate(line):
                cell = Cell(x, y, isWall = (ch == "#"), isStart = (ch == "S"), isExit = (ch == "E"))
                if (ch == "S"):
                    start = cell
                if (ch == "E"):
                    exit = cell 
                
                row.append(cell)

            grid.append(row)
        
        return Maze(grid, start, exit)