import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;


public class Maze {
    private boolean [][] array;
    private int width;
    private int height;

    public Maze(File in_file) throws FileNotFoundException {
        String s;
        Scanner sc = new Scanner(in_file);
        // Get the height and width from the file.
        while(!sc.hasNextInt())
            sc.nextLine();
        this.width = sc.nextInt();
        this.height = sc.nextInt();
        sc.nextLine();

        array = new boolean [this.height][this.width];

        for (int h = 0; h < this.height; h++) {
            for (int w = 0; w < this.width; ) {
                s = sc.findInLine(".");
                if (s == null)
                    sc.nextLine();
                else
                    array[h][w++] = s.charAt(0) == '1';
            }
        }
    }

    public Maze(Maze m) {
        this.width = m.width;
        this.height = m.height;
        this.array = new boolean[height][width];

        for(int i = 0; i < height; i++)
            for(int j = 0; j < width; j++)
                this.array[i][j] = m.array[i][j];
    }

    public void rotate() {
        boolean[][] rotated = new boolean[this.width][this.height];

        for (int w = this.width - 1; w >= 0; w--) {
            for (int h = 0; h < this.height; h++) {
                rotated[this.width-w-1][h] = this.array[h][w];
            }
         }

        this.array = rotated;
        int temp = this.height;
        this.height = this.width;
        this.width = temp;
    }

    public void write(File outFile) {
        try {
            // Write the pbm file format.
            PrintStream outStream = new PrintStream(outFile);
            outStream.println("P1");
            outStream.println("#Created by " + getClass().getName() + "UTFSM ELO329");
            outStream.println(this.width + " " + this.height);
            // Write the matrix in the file.
            for (int h = 0; h < height; h++) {
                for (int w = 0; w < width; w++) {
                    outStream.print(array[h][w] ? "1" : "0");
                    outStream.print(w < width-1 ? " " : "\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isThere_a_wall(int x, int y) {
        return x == width || y == height || y == 0 || x == 0 || this.array[y][x] ;
    }

    public boolean isThereFinish(int x, int y) {
        return x == width || y == height;
    }

    public void markPoint(Vector2D p){
        this.array[(int)(p.getY())][(int)p.getX()] = true;
    }
}