Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- class Labyrinthe {
- private Cell[][] cells;
- private int width;
- private int height;
- public Labyrinthe(int width, int height, Cell[][] cells){
- this.width = width;
- this.height = height;
- this.cells = cells;
- }
- public Labyrinthe(int width, int height){
- this.width = width;
- this.height = height;
- this.cells = new Cell[height][width];
- }
- public Labyrinthe(String pathToLab){
- int col = 0;
- int row = 0;
- try (FileReader reader = new FileReader(pathToLab);
- BufferedReader br = new BufferedReader(reader)) {
- // read line by line
- String line;
- while ((line = br.readLine()) != null) {
- for (col = 0; col < line.length() ; col++) {
- }
- row++;
- }
- this.width = col;
- this.height = row;
- } catch (Exception e) {
- e.printStackTrace();
- }
- generateCells(pathToLab);
- }
- public void addCell(int column, int row, Cell cell){
- cells[row][column] = cell;
- }
- public void addCell(int column, int row, char type){
- cells[row][column] = new Cell(type);
- }
- public void generateCells(String pathToLab){
- int row = 0;
- int col = 0;
- try (FileReader reader = new FileReader(pathToLab);
- BufferedReader br = new BufferedReader(reader)) {
- // read line by line
- String line;
- while ((line = br.readLine()) != null) {
- for (col = 0; col < line.length() ; col++) {
- cells[row][col] = new Cell(line.charAt(col));
- }
- row++;
- }
- if (this.width == 0 && this.height == 0) {
- this.width = col;
- this.height = row;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void printCells(){
- for (int row = 0; row < height ; row++) {
- System.out.print("\n");
- for (int col = 0; col < width ; col++) {
- System.out.print(cells[row][col].toString());
- }
- }
- }
- public static void main(String[] args) {
- Labyrinthe myLab = new Labyrinthe(5,6);
- myLab.generateCells("M:\\Mathieu\\Bureau\\Cours\\TD_TP\\RO Decision\\TP1_RO-Labyrinthes-Etudiant_Java\\labyrinths\\lab1.lab");
- myLab.printCells();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement