帮忙下载一下源码,非常感谢别人帮忙投票的话!

各位Java大神,帮我解决一下这段代码出现的问题吧,谢谢了_java吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:496,643贴子:
各位Java大神,帮我解决一下这段代码出现的问题吧,谢谢了收藏
import java.awt.*;import java.awt.event.*; import javax.swing.*; import java.awt.image.*; public class KnightsTour extends JApplet {
// 初始位置位于某个位置的可达到数采用二维数组表示
//即以棋盘任意一点为初试位置,骑士游历完整个棋盘的路径数
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);int countMoving = -1;int tourXpos[] = new int[64];int tourYpos[] = new int[64];private int recordXpos[][];private int recordYpos[][];private int recordCount = -1;private boolean success =//声明MyPanel的一个对象MyPanel myPpublic void tour(int xpos,int ypos){int x,y;countMoving++;//如果64个格子都被走过,则返回if(countMoving ==63){tourXpos[countMoving] =tourYpos[countMoving] =success =countMoving--;}AccessibleSquares nextSquare = new AccessibleSquares(xpos,ypos);while(nextSquare.hasMoreAccessible()){//开始移动nextSquare.domoving();//把这一步记录下来tourXpos[countMoving] =tourYpos[countMoving] =//尝试下一步的移动nextSquare.nextAccessible();tour(nextSquare.getXpos(),nextSquare.getYpos());//如果64格全走过就返回if(success){countMoving--;}//如果失败,则从起始位置从新开始nextSquare.undomoving();}countMoving--;}public static int[] arrayCopy(int array1[]) {int[] array2 = new int[array1.length];for(int row = 0;row & array1.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.row++){for(int column = 0;row & array1[0].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 static void main(String[] args){KnightsTour knightsTour = new KnightsTour(); knightsTour.tour(0,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 =countMoving = -1;startx =starty =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&);
//建立Next Moving按钮JButton nextTour = new JButton(&Next Tour&);
//建立Next Tour按钮buttonPanel.add(nextTour);buttonPanel.add(nextMoving);getContentPane().add(buttonPanel,BorderLayout.SOUTH);getContentPane().add(myPanel);nextMoving.addActionListener(//匿名内部类,定义了actionPerformed函数,调用showNext函数响应Next Moving Button事件new ActionListener(){public void actionPerformed(ActionEvent e){myPanel.showNext();}});nextTour.addActionListener(//内部类,定义了actionPerformed函数,响应Next Tour Button 事件//在方法中定义的内部类可以访问定义它的外部类对象中的实例变量和方法,以及该方法中的所有final局部变量new ActionListener(){public void actionPerformed(ActionEvent e){if(recordCount & recordXpos.length - 1){recordCount++;}else{recordCount = 0;}myPanel.initboard(recordXpos[recordCount],recordYpos[recordCount]);}});}public void paint(Graphics g){super.paint(g);}}class AccessibleSquares{//骑士8个方向走L形状所需的x坐标和y坐标的变化量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[];
//骑士所处x轴坐标private int ypos[];
//骑士所处y轴坐标private int accessibility[];private int ownxpos,private int ownAprivate int arrayPprivate int countA//构造函数public AccessibleSquares(int x,int y){int testXPint testYPxpos = new int[8];ypos = new int[8];accessibility = new int [8];arrayPos = 0;ownxpos =ownypos =ownAccessibility = KnightsTour.access[x][y];//8个可能方向for(int i = 0;i&horizontal.i++){testXPos = x + horizontal[i];testYPos = y + vertical[i];//如果测试位置在棋盘以内if((testXPos &= 0)&(testXPos & 8)&(testYPos &= 0)&(testYPos & 8)){xpos[arrayPos] = testXPypos[arrayPos] = testYPaccessibility[arrayPos] = KnightsTour.access[testXPos][testYPos];//若accessibility[arrayPos] = 0,表明格子已经被占用if(accessibility[arrayPos] & 0){arrayPos++;}}}countAccessibility = arrayPif(countAccessibility & 0){sortAll();}arrayPos = -1;}public boolean hasMoreAccessible(){//arrayPos+1指向下一个可行的if((arrayPos + 1) & countAccessibility){}else{}}public AccessibleSquares nextAccessible(){arrayPos ++;}public AccessibleSquares accessibleAt(int pos){if((pos &= 0)&(pos & countAccessibility)){arrayPos =}}public int getXpos(){return xpos[arrayPos];}public int getYpos(){return ypos[arrayPos];}public int getAccessibility(){return accessibility[arrayPos];}public int getTotalAccessible(){return countA}//冒泡排序法。冒泡排序虽然容易编程,但是算法开销较大,对比较大的数组排序更是如此//如果遇到大型数组的排序,注意开发更有效的冒泡排序方法,以及其他高效率的排序方法private void sortAll(){for(int begin = 0;begin & countAccessibility - 1;begin++){for(int i = begin+1;i & countAi++){if(accessibility[begin] & accessibility[i]){swapAll(begin,i);}}}}//交换两个数private void swapAll(int i,int j){temp = xpos[i];xpos[i] = xpos[j];xpos[j] =temp = ypos[i];ypos[i] = ypos[j];ypos[j] =temp = accessibility[i];accessibility[i] = accessibility[j];accessibility[j] =}//进行移动操作public void
domoving(){for(int i = 0;i & countAi++){KnightsTour.access[xpos[i]][ypos[i]]--;}KnightsTour.access[ownxpos][ownypos] = 0;}//撤销移动操作public void undomoving(){for(int i = 0;i & countAi++){KnightsTour.access[xpos[i]][ypos[i]]++;}KnightsTour.access[ownxpos][ownypos] = ownA}}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 displayCprivate int lastxpos,lastypos,nextxpos,ImageIcon images[];p//MyPanel的构造函数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 =displayCount = -1;for(int row = 0;row & 8;row++){for(int column = 0;column & 8;column++){//白的用0表示,黑的用1表示chessboard[row][column] = (row + column)%2;}}for(int row = 0;row & newxrecord.row++){xrecord[row] = newxrecord[row];yrecord[row] = newyrecord[row];}displayCount = 0;chessboard [xrecord [displayCount]] [yrecord [displayCount]]+=2;}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);}}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);}//起始位置显示为红色g.setColor(Color.red);g.drawRect(40*xrecord[0]+2,40*yrecord[0]+2,36,36);}}出现异常错误:
数组越界异常
你这样排还不如一页页发eclipse的代码,看着头晕
复制数组方法中
length应该减1.
你创建一个5位数组 赋值只能是0-4
public static int[][] arrayCopy(int[][] array1) {int[][] array2 = new int[array1.length][array1[0].length];for(int row = 0;row & array1.length-1;row++){for(int column = 0;row
这错了不是ROW 是COLUMN& array1[0].length-1;column++){array2[row][column] = array1[row][column];};};return array2;}
还有就是要减1
我也是菜鸟 刚在公司实习 没事干才帮你看看
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或帮忙下载一个csdn的源码 谢谢了_百度知道从源代码找到下载地址大神们帮帮忙_百度知道求一份手势识别的源码(带教程就更好了),非常感谢!_百度知道

我要回帖

更多关于 感谢别人帮忙的短信 的文章

 

随机推荐