骑士遍历问题

package com.borland.sample.OneHorse;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

class AccessibleSquares {
  private static int horizontal[] = {
      2, 1, -1, -2, -2, -1, 1, 2};
  private static int vertical[] = {
      -1, -2, -2, -1, 1, 2, 2, 1};
  private int xpos[];
  private int ypos[];
  private int accessibility[];
  private int ownxpos, ownypos;
  private int ownAccessibility;
  private int arrayPos;
  private int countAccessibility;

  public AccessibleSquares(int x, int y) {

    int testXPos;
    int testYPos;
    xpos = new int[8];
    ypos = new int[8];
    accessibility = new int[8];
    arrayPos = 0;
    ownxpos = x;
    ownypos = y;
    ownAccessibility = Horse.access[x][y];

    for (int i = 0; i < horizontal.length; i++) {
      testXPos = x + horizontal[i];
      testYPos = y + vertical[i];

      if ( (testXPos >= 0) & (testXPos < 8) &
          (testYPos >= 0) & (testYPos < 8)) {

        xpos[arrayPos] = testXPos;
        ypos[arrayPos] = testYPos;
        accessibility[arrayPos] = Horse.access[testXPos][testYPos];

        //because  accessibility [ arrayPos ] = 0 indicating the square has been occupied
        if (accessibility[arrayPos] > 0) {
          arrayPos++;
        }
      } //end of if
    } // end of for

    countAccessibility = arrayPos;
    if (countAccessibility > 0) {
      sortAll();
    }
    arrayPos = -1;
  } // end of constructor

  public boolean hasMoreAccessible() {

    // arrayPos + 1 point to the next accessible
    if ( (arrayPos + 1) < countAccessibility) {
      return true;
    }
    else {
      return false;
    }
  } //end of the hasMoreAccessible()

  public AccessibleSquares nextAccessible() {
    arrayPos++;
    return this;
  }


  public AccessibleSquares accessibleAt(int pos) {
    if ( (pos >= 0) & (pos < countAccessibility)) {
      arrayPos = pos;
    }
    return this;
  }

  public int getXpos() {
    return xpos[arrayPos];
  }

  public int getYpos() {
    return ypos[arrayPos];
  }

  public int getAccessibility() {
    return accessibility[arrayPos];
  }

  public int getTotalAccessible() {
    return countAccessibility;
  }

  //bubble sorting
  private void sortAll() {
    for (int begin = 0; begin < countAccessibility - 1; begin++) {
      for (int i = begin + 1; i < countAccessibility; i++) {
        if (accessibility[begin] > accessibility[i]) {
          swapAll(begin, i);
        } //end of if
      } // end of inner for
    } // end of outer for
  } // end of sortAll

  private void swapAll(int i, int j) {
    int temp;
    temp = xpos[i];
    xpos[i] = xpos[j];
    xpos[j] = temp;

    temp = ypos[i];
    ypos[i] = ypos[j];
    ypos[j] = temp;

    temp = accessibility[i];
    accessibility[i] = accessibility[j];
    accessibility[j] = temp;
  }

  public void domoving() {
    for (int i = 0; i < countAccessibility; i++) {
      Horse.access[xpos[i]][ypos[i]]--;
    }
    Horse.access[ownxpos][ownypos] = 0;
  }

  public void undomoving() {
    for (int i = 0; i < countAccessibility; i++) {
      Horse.access[xpos[i]][ypos[i]]++;
    }
    Horse.access[ownxpos][ownypos] = ownAccessibility;
  }
}
package com.borland.sample.OneHorse;
import java.awt.Graphics;
import java.awt.*;
import javax.swing.*;
//import java.awt.event.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class MyPanel
    extends JPanel {
  public static final int WHITE = 0;
  public static final int BLACK = 1;
  public static final int WKNIGHT = 2;
  public static final int BKNIGHT = 3;

  private int chessboard[][];
  private int xrecord[];
  private int yrecord[];
  private int displayCount;
  private int lastxpos, lastypos, nextxpos, nextypos;
  ImageIcon images[];
  private boolean start;

  public MyPanel() {
    initvariance();
  }

  public MyPanel(int[] newxrecord, int[] newyrecord) {
    initvariance();
    initboard(newxrecord, newyrecord);
  }

  public void initvariance() {
    chessboard = new int[8][8];
    xrecord = new int[64];
    yrecord = new int[64];
    images = new ImageIcon[4];
    images[0] = new ImageIcon("white.jpg");
    images[1] = new ImageIcon("black.jpg");
    images[2] = new ImageIcon("wknight.jpg");
    images[3] = new ImageIcon("bknight.jpg");
  }

  public void initboard(int[] newxrecord, int[] newyrecord) {
    start = true;
    displayCount = -1;
    for (int row = 0; row < 8; row++) {
      for (int column = 0; column < 8; column++) {
        // white use 0 ,black use 1
        chessboard[row][column] = (row + column) % 2;
      }
    } //end of outer for

    for (int row = 0; row < newxrecord.length; row++) {
      xrecord[row] = newxrecord[row];
      yrecord[row] = newyrecord[row];
    }
    displayCount = 0;
    chessboard[xrecord[displayCount]][yrecord[displayCount]] += 2;
  } //end of initboard

  public void showNext() {
    if (displayCount < xrecord.length - 1) {
      displayCount++;
      chessboard[xrecord[displayCount]][yrecord[displayCount]] += 2;
      repaint();
    }
  }

  public void paintComponent(Graphics g) {

    for (int row = 0; row < 8; row++) {
      for (int column = 0; column < 8; column++) {
        images[chessboard[row][column]].paintIcon(this, g, 40 * row,
                                                  40 * column);
      } //end of inner for
    } //end of outer for

    if (displayCount > 0) {
      lastxpos = xrecord[displayCount - 1];
      lastypos = yrecord[displayCount - 1];
      nextxpos = xrecord[displayCount];
      nextypos = yrecord[displayCount];
      g.setColor(Color.green);
      g.drawRect(40 * xrecord[displayCount - 1] + 2,
                 40 * yrecord[displayCount - 1] + 2, 36, 36);
      g.setColor(Color.green);
      g.drawRect(40 * xrecord[displayCount] + 2, 40 * yrecord[displayCount] + 2,
                 36, 36);
      g.setColor(Color.blue);
      g.drawRect(40 * Math.min(nextxpos, lastxpos),
                 40 * Math.min(nextypos, lastypos),
                 (Math.abs(nextxpos - lastxpos) + 1) * 40,
                 (Math.abs(nextypos - lastypos) + 1) * 40);
    } //end of if
    g.setColor(Color.red);
    g.drawRect(40 * xrecord[0] + 2, 40 * yrecord[0] + 2, 36, 36);
  } // end of the method paintComponent
}
package com.borland.sample.OneHorse;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Horse extends JApplet implements Serializable {
  public static int access[][] = {
      {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}
  };
  public static int accessbak[][] = arrayCopy(access);
  // the value indicate the No.value moving
  int countMoving = -1;
  int tourXpos[] = new int[64];
  int tourYpos[] = new int[64];
  private int recordXpos[][];
  private int recordYpos[][];
  private int recordCount = -1;
  private int startx;
  private int starty;
  private boolean success = false;
  MyPanel myPanel;

  public void  tour(int xpos, int ypos) {
    countMoving++;
    //all the  64 squares has been touch , return
    if (countMoving == 63) {
      tourXpos[countMoving] = xpos;
      tourYpos[countMoving] = ypos;
     //         if ( ( ( Math.abs( xpos -startx ) == 1)  & ( Math.abs ( ypos - starty ) ==2 ) ) |
      //            ( ( Math.abs( xpos -startx ) == 2)  & ( Math.abs ( ypos - starty ) ==1 ) ) )
      success = true;
      countMoving--;
      return ;
    }
    AccessibleSquares nextSquare = new AccessibleSquares(xpos, ypos);
    while (nextSquare.hasMoreAccessible()) {
      // do moving
      nextSquare.domoving();
      //record this moving
      tourXpos[countMoving] = xpos;
      tourYpos[countMoving] = ypos;

      // try the next moving
      nextSquare.nextAccessible();
      tour(nextSquare.getXpos(), nextSquare.getYpos());

      //all the  64 squares has been touch , return
      if (success) {
        countMoving--;
        return ;
      }

      //this moving try is a faillure, pick it up from the chess board
      nextSquare.undomoving();
    } // end of while

    countMoving--;
  } //end of tour method

 


  public static int [] arrayCopy(int array1[]) {
    int[] array2 = new int[array1.length];
    for (int row = 0; row < array1.length; row++) {
      array2[row] = array1[row];
    }
    ;
    return array2;
  }

  public static int[][] arrayCopy(int array1[][]) {
    int[][] array2 = new int[array1.length][array1[0].length];
    for (int row = 0; row < array1.length; row++) {
      for (int column = 0; column < array1[0].length; column++) {
        array2[row][column] = array1[row][column];
      }
      ;
    }
    ;
    return array2;
  }

  public void initialArray(int chessBoard[][]) {
    for (int row = 0; row < 8; row++) {
      for (int column = 0; column < 8; column++) {
        chessBoard[row][column] = 0;
      }
      ;
    }
    ;
  }
  public void init() {
    recordCount = -1;
    recordXpos = new int[64][64];
    recordYpos = new int[64][64];
    for (int row = 0; row < 8; row++) {
      for (int column = 0; column < 8; column++) {
        success = false;
        countMoving = -1;
        startx = row;
        starty = column;
        access = arrayCopy(accessbak);
        tour(row, column);
        recordCount++;
        recordXpos[recordCount] = arrayCopy(tourXpos);
        recordYpos[recordCount] = arrayCopy(tourYpos);
      }
    }
    recordCount = 0;
    myPanel = new MyPanel(recordXpos[0], recordYpos[0]);
    JPanel buttonPanel = new JPanel();
    JButton nextMoving = new JButton("Next Moving");
    JButton nextTour = new JButton("Next Tour");
    buttonPanel.add(nextTour);
    buttonPanel.add(nextMoving);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    getContentPane().add(myPanel);

    nextMoving.addActionListener(

        //anonymous inner class
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        myPanel.showNext();
      }
    }
    ); //end call to addActionListener

    nextTour.addActionListener(

        //anonymous inner class
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (recordCount < recordXpos.length - 1) {
          recordCount++;
        }
        else {
          recordCount = 0;
        }
        myPanel.initboard(recordXpos[recordCount], recordYpos[recordCount]);
        myPanel.repaint();
      }
    }
    ); //end call to addActionListener

  }

 

  public void paint(Graphics g) {
    super.paint(g);
  }
  private void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();
  }
  private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();
  }
}

 

在JBuilder中运行,有错误java.lang.NoSuchMethodError: main,请高手帮忙调试一下吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值