// Initialize Board public void init() { // Create cells and handlers cells = new JTextField[gameBoard.boardSize * gameBoard.boardSize + 1]; // Redraw Panel boardPanel.removeAll(); boardPanel.setLayout(new GridLayout(gameBoard.boardSize, gameBoard.boardSize)); // Set layout JTextFilter TextFilter = new JTextFilter(3); JTextDocumentListener JTextDocFilter = new JTextDocumentListener(); for (int i = 1; i <= gameBoard.boardSize * gameBoard.boardSize; i++) { cells[i] = new JTextField(); ((AbstractDocument) cells[i].getDocument()).setDocumentFilter(TextFilter); ((AbstractDocument) cells[i].getDocument()).addDocumentListener(JTextDocFilter); ((AbstractDocument) cells[i].getDocument()).putProperty("index", i); cells[i].setHorizontalAlignment(JTextField.CENTER); cells[i].setFont(new Font("Agency FB", Font.BOLD, 24)); // Add elements to the grid content pane boardPanel.add(cells[i]); } // Initialize booleans gameOver = false; // Clear Board for (int i = 1; i <= (gameBoard.boardSize * gameBoard.boardSize); i++) { String ch = Integer.toString(this.gameBoard.cells[i]); char chr = '-'; if (ch.compareTo("0") == 0 || ch == Character.toString(chr)) { cells[i].setText(""); } else { cells[i].setText(ch); cells[i].setBackground(Color.lightGray); } } // gameBoard.out(); setVisible(true); this.boardPanel.repaint(); this.gameTimer.reset(); jButtonSOLVE.setEnabled(true); }
private void jButtonSOLVEActionPerformed(java.awt.event.ActionEvent evt) throws IOException { System.out.println("solving"); // DEBUG // BufferedReader in = new BufferedReader(new // FileReader("C:\\Users\\Rainer\\Desktop\\2013 FALL\\CAP 4621 - Artificial // Intelligence\\Project\\InitialBoard.txt")); // // try{ // in = new BufferedReader(new FileReader("C:\\Users\\Rainer\\Desktop\\2013 // FALL\\CAP 4621 - Artificial Intelligence\\Project\\InitialBoard.txt")); // // }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(); // // END DEBUG long startTime = System.nanoTime(); this.gameBoard.solveBoard(); long endTime = System.nanoTime(); double time = (endTime - startTime) / 100000000.0; System.out.println("Excecution time: " + time + " seconds"); // jPanelSolvedBoard jPanelSolvedBoard.removeAll(); jPanelSolvedBoard.setLayout( new GridLayout(gameBoard.boardSize, gameBoard.boardSize)); // Set layout solvedCells = new JTextField[gameBoard.boardSize * gameBoard.boardSize + 1]; for (int i = 1; i <= gameBoard.boardSize * gameBoard.boardSize; i++) { solvedCells[i] = new JTextField(); solvedCells[i].setHorizontalAlignment(JTextField.CENTER); solvedCells[i].setFont(new Font("Agency FB", Font.BOLD, 24)); // Add elements to the grid content pane jPanelSolvedBoard.add(solvedCells[i]); String ch = Integer.toString(this.gameBoard.cells[i]); solvedCells[i].setText(ch); solvedCells[i].setEditable(false); } // gameBoard.out(); jPanelSolvedBoard.setVisible(true); this.jPanelSolvedBoard.repaint(); this.jFrameSolvedBoard.setVisible(true); this.jFrameSolvedBoard.pack(); // }