bem lah vai, eu coloquei cinco asteriscos para facilitar achar a parte do codigo que eu tava falando
perceba que se voce comentar o showMessageDialog que tem logo abaixo dos cinco asteriscos, ele faz toda a rotina e mostra só o final
// Tour do Cavalo \
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class D722 extends JApplet implements ActionListener {
// board[row][col]
int[][] board = new int[8][8], useAccessibility= new int[8][8];
int accessibility[][] = {
{2,3,4,4,4,4,3,2},
{3,4,6,6,6,6,4,3},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{3,4,6,6,6,6,4,3},
{2,3,4,4,4,4,3,2},
};
int[] horizontal = new int[8], vertical = new int [8];
int currentCol = 0, currentRow = 0;
JTextArea txtBoard;
JTextField txtX, txtY;
JButton btnAction;
public void init() {
// Array de movimentos \
horizontal[0] = 2;
horizontal[1] = 1;
horizontal[2] = -1;
horizontal[3] = -2;
horizontal[4] = -2;
horizontal[5] = -1;
horizontal[6] = 1;
horizontal[7] = 2;
vertical[0] = -1;
vertical[1] = -2;
vertical[2] = -2;
vertical[3] = -1;
vertical[4] = 1;
vertical[5] = 2;
vertical[6] = 2;
vertical[7] = 1;
Container c = getContentPane();
c.setLayout (new FlowLayout());
txtBoard = new JTextArea();
c.add( txtBoard );
txtX = new JTextField(5);
c.add(txtX);
txtY = new JTextField(5);
c.add(txtY);
btnAction = new JButton("Ação!");
btnAction.addActionListener(this);
c.add( btnAction );
}
public void actionPerformed (ActionEvent e) {
currentRow = Integer.parseInt( txtX.getText() );
currentCol = Integer.parseInt( txtY.getText() );
heuristicMove();
}
// Zera a matriz
public void resetBoard() {
for (int x = 0; x < board.length; x++)
for (int y = 0; y < board[x].length; y++){
board[x][y] = 0;
useAccessibility[x][y] = accessibility[x][y];
}
}
// Mostra resultado da matriz board[][] no txtBoard
public void showBoard() {
String output="";
DecimalFormat df = new DecimalFormat("00");
for (int x = 0; x < board.length; x++){
for (int y = 0; y < board[x].length; y++) {
if (board[x][y] > 0)
output += " " + String.valueOf(df.format(board[x][y])) + " ";
else
output += " [] ";
}
output += "\n";
}
txtBoard.setFont( new Font("Lucida Console",Font.PLAIN,20) );
txtBoard.setText(output) ;
//*****
JOptionPane.showMessageDialog(null,"Passing by");
}
// Testa se vai sair do tabuleiro ou se posição já foi visitada
public int testMove() {
int lowAcc = 10, testAcc = -1, useMove = -1;
int minusAcc[] = new int[8];
for (int i=0; i <8; i++) minusAcc[i] = 0;
for (int x=0; x < 8; x++){
if (testMove(x)) {
minusAcc[x] = 1;
testAcc = getAccessiblityByMove(x);
if (testAcc < lowAcc) {
lowAcc = testAcc;
useMove = x;
}
}
}
for (int z=0; z <8; z++){
if (minusAcc[z] == 1) {
reduceAccessibility(currentRow + horizontal[z], currentCol + vertical[z]);
}
}
return useMove;
}
public boolean testMove(int n) {
if ( currentRow + horizontal[n] > 7 || currentRow + horizontal[n] < 0 )
return false;
if ( currentCol + vertical[n] > 7 || currentCol + vertical[n] < 0)
return false;
int testRow = 0, testCol = 0;
testRow = currentRow + horizontal[n];
testCol = currentCol + vertical[n];
if ( board[testRow][testCol] > 0 )
return false;
else
return true;
}
public void reduceAccessibility(int row, int col) {
useAccessibility[row][col] -= 1;
}
public int getAccessiblityByMove(int n) {
int testRow = 0, testCol = 0;
testRow = currentRow + horizontal[n];
testCol = currentCol + vertical[n];
return useAccessibility[testRow][testCol];
}
//Efetua o moviemento pelo codigo
public void doMove(int n, int c) {
currentRow += horizontal[n];
currentCol += vertical[n];
board[currentRow][currentCol] = c;
}
public void heuristicMove() {
int moveCount = 0, moveN = 0;
resetBoard();
for (moveCount = 1; moveCount < 65; moveCount++) {
moveN = testMove();
if (moveN >= 0){
showBoard();
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {
}
doMove(moveN, moveCount);
} else {
System.out.println("Breaking " + moveN);
break;
}
}
showBoard();
JOptionPane.showMessageDialog(null,"Movimentos:" + (moveCount-1),"",JOptionPane.INFORMATION_MESSAGE);
}
}