@SuppressWarnings("resource")
    public Board(File file, boolean debug) {
        ArrayList<Cell> boardList = new ArrayList<Cell>();
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        int numLines = 0;
        int x = 0;
        int y = 0;
        ArrayList<Integer> lineLengths = new ArrayList<Integer>();
        try {
            fis = new FileInputStream(file);

            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            // dis.available() returns 0 if the file does not have more lines.
            while (dis.available() != 0) {
                @SuppressWarnings("deprecation")
                String currentLine = dis.readLine();
                y = 0;
                numLines++;
                lineLengths.add(currentLine.length());

                // Check the first character of the line
                char c = currentLine.charAt(0);

                if (c != '0' && c != '1') {
                    throw new RuntimeException(
                            "invalid input - first character not 0 or 1.");
                }

                // Loop through all the characters of the line
                for (int i = 0; i < currentLine.length(); i++) {

                    char currentChar = currentLine.charAt(i);

                    // Check validity of each character
                    if (currentChar != '1' && currentChar != '0'
                            && currentChar != ' ' && currentChar != '\n') {
                        throw new RuntimeException(
                                "invalid input - character is something other than 1, 0 or space");
                    }

                    else if (currentChar == '1' || currentChar == '0') {
                        if (i + 1 < currentLine.length()) {
                            char next = currentLine.charAt(i + 1);
                            if (next != ' ' && next != '\n') {
                                throw new RuntimeException(
                                        "invalid input - something other than a space or new line follows a 1 or 0");
                            }
                        }
                        if (currentChar == '1') {
                            boardList.add(new Cell(x, y, true));
                            y++;
                        } else {
                            boardList.add(new Cell(x, y, false));
                            y++;
                        }
                    } else if (currentChar == ' ') {
                        if (i + 1 < currentLine.length()) {
                            char next = currentLine.charAt(i + 1);
                            if (next != '1' && next != '0') {
                                throw new RuntimeException(
                                        "invalid input - a 1 or 0 does not follow a space");
                            }
                        }
                    }
                }

                // moving on to next line - increment x
                x++;
            }

            // dispose all the resources after using them.
            fis.close();
            bis.close();
            dis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Check the dimensions
        if (x != y) {
            throw new RuntimeException("invalid input - dimensions differ");
        }

        // Check every line is the same length
        for (int i = 0; i < lineLengths.size() - 1; i++) {
            if (lineLengths.get(i) != lineLengths.get(i + 1)) {
                throw new RuntimeException(
                        "invalid input - line lengths differ");
            }
        }

        // Everything is fine
        size = numLines;
        board = new Cell[size][size];
        for (Cell cell : boardList) {
            board[cell.getCoordinates()[0]][cell.getCoordinates()[1]] = cell;
        }
    }