Beispiel #1
0
  public void jFileChooserActionPerformed(java.awt.event.ActionEvent evt)
      throws FileNotFoundException, IOException {
    File initialBoard = this.jFileChooser.getSelectedFile();
    InputStream inputStream = new FileInputStream(initialBoard);
    Scanner scanner = new Scanner(inputStream);

    if (!initialBoard
        .getName()
        .toLowerCase()
        .contains("initialboard")) { // .toLowerCase().compareTo("initialboard")!=0){
      JOptionPane.showMessageDialog(
          frame, "Wrong File \n Please select InitialBoard.txt", "Eror", JOptionPane.ERROR_MESSAGE);
    } else {

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      try {
        in = new BufferedReader(new FileReader(initialBoard.getAbsolutePath()));

      } catch (FileNotFoundException e) {
        System.out.println(e.getCause());
        System.out.println("Error loading initial board");
      }

      int[] boardRows = new int[15];

      // ----------------
      String text = in.readLine();

      StringTokenizer tokenizer = new StringTokenizer(text, " ");

      int boardSize = 0;
      while (tokenizer.hasMoreElements()) {
        boardRows[boardSize] = Integer.parseInt(tokenizer.nextToken());
        boardSize++;
      }

      int[] newBoard = new int[boardSize * boardSize + 1];
      System.arraycopy(boardRows, 0, newBoard, 1, boardSize);

      int index = 0;
      while (in.ready()) {
        index++;
        text = in.readLine();

        tokenizer = new StringTokenizer(text, " ");
        int pos = 0;
        while (tokenizer.hasMoreElements()) {
          pos++;
          newBoard[index * boardSize + pos] = Integer.parseInt(tokenizer.nextToken());
        }
      }

      this.jFrameFileChooser.setVisible(false);
      // this.boardPanel.s

      gameInitialBoard = new Board(newBoard, boardSize);
      gameBoard = new Board(newBoard, boardSize);
      init();
    }
  }