/* * * Title: The Knight'Tour (Hamiltonian Cycle Method) * written by: Arthur Y. Gapusan (ajgapusan@hotmail.com) * Date Modified: March 15, 2004 =) 12:43 AM * * (Not a Great Way in solving a knight's tour problem) * (coz the solution is just like going around in a circle) * (pero sagdi lng, mo work man sad.. hehehe!) * * http://www.planet-java.biz.ly/ * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class Tour2 extends Frame implements ActionListener { int dis[][]; Button bs[][] = new Button[8][8]; int px[] = {0, 1, 2, 3, 4, 5, 6, 7}; int py[] = {0, 1, 2, 3, 4, 5, 6, 7}; int k = 1; Button tryAgain; int iX, iY; public Tour2() { play(); addWindowListener(new Window()); } public void play() { setLayout(null); setFont(new Font("Monospaced", Font.BOLD, 23)); for (int i=0;i<8;i++) { for (int j=0;j<8;j++) bs[i][j] = (Button) new Button(""); } for (int i=0, k=0;i<8;i++) { for (int j=0;j<8;j++) bs[i][j].setBounds((px[j] * 60)+5, (py[i]*60)+23, 60, 60); } for (int i=0, k=0;i<8;i++) { for (int j=0;j<8;j++) add(bs[i][j]); } for (int i=0;i<8;i++) { for (int j=0;j<8;j++) bs[i][j].addActionListener(this); } Color c = new Color(55, 100, 100); tryAgain = new Button("Clear"); tryAgain.setBounds(5,513, 100, 30); tryAgain.setFont(new Font("Monospaced", Font.BOLD, 15)); tryAgain.setForeground(c); add(tryAgain); tryAgain.addActionListener(this); } // |----------------------------| // | Action Performed | // |----------------------------| public void actionPerformed(ActionEvent ae) { if (ae.getSource() == tryAgain) { removeAll(); play(); } for (int i=0;i<8;i++) { for (int j=0;j<8;j++) { if (ae.getSource() == bs[i][j]) { iX = ((bs[i][j].getX() - 5) / 60); iY = ((bs[i][j].getY() - 23) / 60); game(); } } } } public void game() { int f; int count; int a[] = new int[8]; int sqr[][] = {{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 rc[][] = new int[8][8]; int x = 0, y = 0; for (y=0;y<8;y++) for (x=0;x<8;x++) rc[y][x] = 0; int c = 3, r = 5; sqr[c][r] = 1; rc[c][r] = 1; int moveX[] = {2, 1, -1, -2, -2, -1, 1, 2}; int moveY[] = {-1, -2, -2, -1, 1, 2, 2, 1}; for (count = 2;count<65;count++) { for (int w=0;w<8;w++) a[w] = 9; for (int l=0;l<1;l++) { for (int m=0;m<8;m++) try { if (((c+moveX[m])>=0) && ((r+moveY[m])>=0)) if ((rc[c+moveX[m]][r+moveY[m]])==0) a[m] = (sqr[c+moveX[m]][r+moveY[m]]); } catch (ArrayIndexOutOfBoundsException e) { } int temp; for (int t=0;t<7;t++) { for (int i=t+1;i<8;i++) { if (a[t]>a[i]) { temp = a[t]; a[t] = a[i]; a[i] = temp; } } } for (int m=0;m<8;m++) { try { if (((c+moveX[m])<=7) && ((r+moveY[m])>=0)) { if ((rc[c+moveX[m]][r+moveY[m]])==0) { if ((sqr[c+moveX[m]][r+moveY[m]])==a[0]) { sqr[c+moveX[m]][r+moveY[m]] = count; rc[c+moveX[m]][r+moveY[m]]=count; c+=moveX[m]; r+=moveY[m]; break; } } } } catch (ArrayIndexOutOfBoundsException e) { } } } } int dis[][] = new int[8][8]; for (int i=0;i<8;i++) for (int j=0;j<8;j++) dis[i][j] = 0; c = iY; r = iX; int find = rc[c][r]; int nx = rc[c][r]; int k; dis[c][r] = 1; for (k=1;k<65; ) { inner: for (int i=0;i<8;i++) { for (int j=0;j<8;j++) { if (rc[i][j] == find) { rc[i][j] = 0; dis[i][j] = k; find++; k++; break inner; } } } if (find>64) break; } for (int b=1;b