Advertisement
bebesurf

Labyrinthe.class

May 27th, 2019
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3.  
  4. class Labyrinthe {
  5.  
  6.     private Cell[][] cells;
  7.     private int width;
  8.     private int height;
  9.  
  10.     public Labyrinthe(int width, int height, Cell[][] cells){
  11.         this.width = width;
  12.         this.height = height;
  13.         this.cells = cells;
  14.     }
  15.  
  16.     public Labyrinthe(int width, int height){
  17.         this.width = width;
  18.         this.height = height;
  19.         this.cells = new Cell[height][width];
  20.     }
  21.  
  22.     public Labyrinthe(String pathToLab){
  23.         int col = 0;
  24.         int row = 0;
  25.  
  26.         try (FileReader reader = new FileReader(pathToLab);
  27.              BufferedReader br = new BufferedReader(reader)) {
  28.             // read line by line
  29.             String line;
  30.             while ((line = br.readLine()) != null) {
  31.                 for (col = 0; col < line.length() ; col++) {
  32.  
  33.                 }
  34.                 row++;
  35.             }
  36.  
  37.             this.width  = col;
  38.             this.height = row;
  39.  
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.         }
  43.  
  44.         generateCells(pathToLab);
  45.     }
  46.  
  47.     public void addCell(int column, int row, Cell cell){
  48.         cells[row][column] = cell;
  49.     }
  50.  
  51.     public void addCell(int column, int row, char type){
  52.         cells[row][column] = new Cell(type);
  53.     }
  54.  
  55.     public void generateCells(String pathToLab){
  56.         int row = 0;
  57.         int col = 0;
  58.  
  59.         try (FileReader reader = new FileReader(pathToLab);
  60.             BufferedReader br = new BufferedReader(reader)) {
  61.                 // read line by line
  62.                 String line;
  63.                 while ((line = br.readLine()) != null) {
  64.                     for (col = 0; col < line.length() ; col++) {
  65.                         cells[row][col] = new Cell(line.charAt(col));
  66.                     }
  67.                     row++;
  68.                 }
  69.  
  70.                 if (this.width == 0 && this.height == 0) {
  71.                     this.width  = col;
  72.                     this.height = row;
  73.                 }
  74.  
  75.         } catch (Exception e) {
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.  
  80.     public void printCells(){
  81.         for (int row = 0; row < height ; row++) {
  82.             System.out.print("\n");
  83.             for (int col = 0; col < width ; col++) {
  84.                 System.out.print(cells[row][col].toString());
  85.             }
  86.         }      
  87.     }
  88.  
  89.     public static void main(String[] args) {
  90.         Labyrinthe myLab = new Labyrinthe(5,6);
  91.         myLab.generateCells("M:\\Mathieu\\Bureau\\Cours\\TD_TP\\RO Decision\\TP1_RO-Labyrinthes-Etudiant_Java\\labyrinths\\lab1.lab");
  92.         myLab.printCells();
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement