我第一次使用ANT并开发了KnightsTour程序的淡化版本。我最初在Eclipse中开发没有问题,我试图在ANT中编译,但我没有成功。我没有对这两个类进行任何更改,但JAVAC编译器声明我的一个类AVAILABLELOCATIONS已被复制。
这是编译器错误。
[javac] C:\Users\Admiral Sudoku\Documents\Eduboard\Ken Ton\build.xml:19: war
ning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; se
t to false for repeatable builds
[javac] Compiling 2 source files to C:\Users\Admiral Sudoku\Documents\Eduboa
rd\Ken Ton\build\classes
[javac] C:\Users\Admiral Sudoku\Documents\Eduboard\Ken Ton\src\Knightstour\A
vailableLocations.java:1: error: duplicate class: AvailableLocations
[javac] public class AvailableLocations {
[javac] ^
[javac] C:\Users\Admiral Sudoku\Documents\Eduboard\Ken Ton\src\Knightstour\m
ainclass.java:6: error: cannot access AvailableLocations
[javac] public static Vector checkmovesavailable (int[][] board, Availabl
eLocations knightlocation, int x, int y)
[javac] ^
[javac] bad source file: C:\Users\Admiral Sudoku\Documents\Eduboard\Ken To
n\src\Knightstour\AvailableLocations.java
[javac] file does not contain class Knightstour.AvailableLocations
[javac] Please remove or make sure it appears in the correct subdirector
y of the sourcepath.
这是AvailableLocations类。
public class AvailableLocations {
int x;
int y;
public AvailableLocations(int x_coord, int y_coord)
{
x = x_coord;
y = y_coord;
}
}
这是我的主要课程:
package Knightstour;
import java.util.*;
public class mainclass{
public static Vector checkmovesavailable (int[][] board, AvailableLocations knightlocation, int x, int y)
{
Vector availablemoves = new Vector();
if (knightlocation.x - 2 >= 0)
{
if (knightlocation.y + 1 < y)
{
if (board[knightlocation.x - 2][knightlocation.y + 1] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x- 2, knightlocation.y+ 1 );
availablemoves.addElement(location);
}
}
if (knightlocation.y - 1 >= 0)
{
if (board[knightlocation.x - 2][knightlocation.y - 1] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x - 2, knightlocation.y - 1 );
availablemoves.addElement(location);
}
}
}
if (knightlocation.x + 2 < x)
{
if (knightlocation.y + 1 < y)
{
if (board[knightlocation.x + 2][knightlocation.y + 1] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x+ 2, knightlocation.y+ 1 );
availablemoves.addElement(location);
}
}
if (knightlocation.y - 1 >= 0)
{
if (board[knightlocation.x + 2][knightlocation.y - 1] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x + 2, knightlocation.y- 1 );
availablemoves.addElement(location);
}
}
}
if (knightlocation.y - 2 >= 0)
{
if (knightlocation.x+ 1 < x)
{
if (board[knightlocation.x+ 1][knightlocation.y - 2] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x + 1, knightlocation.y- 2 );
availablemoves.addElement(location);
}
}
if (knightlocation.x - 1 >= 0)
{
if (board[knightlocation.x- 1][knightlocation.y - 2] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x- 1, knightlocation.y - 2 );
availablemoves.addElement(location);
}
}
}
if (knightlocation.y + 2 < y)
{
if (knightlocation.x + 1 < x)
{
if (board[knightlocation.x+ 1][knightlocation.y + 2] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x + 1, knightlocation.y + 2 );
availablemoves.addElement(location);
}
}
if (knightlocation.x - 1 >= 0)
{
if (board[knightlocation.x - 1][knightlocation.y + 2] == 0)
{
AvailableLocations location = new AvailableLocations(knightlocation.x- 1, knightlocation.y + 2 );
availablemoves.addElement(location);
}
}
}
return availablemoves;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int maxattempts = Integer.parseInt(args[2]);
int [][] board = new int[x][y];
AvailableLocations knightlocation = new AvailableLocations(0, 0);
board [0][0] = 1;
Vector locationsvisited = new Vector();
locationsvisited.addElement(knightlocation);
int i = 0;
while (i <= maxattempts)
{
i++;
Vector movesavailable = checkmovesavailable(board, knightlocation,x, y);
int numofmovesavailable = movesavailable.size();
if (numofmovesavailable == 0)
{
break;
}
Random rand = new Random();
int selectmove = rand.nextInt(numofmovesavailable - 1 + 1) + 1;
knightlocation = (AvailableLocations) movesavailable.elementAt(selectmove -1);
board[knightlocation.x][knightlocation.y] = i + 1;
if (i == x *y)
{
break;
}
}
for(int k = 0; k<x; k++)
{
for(int j = 0; j<y; j++)
{
if (board[k][j] == 0)
{
System.out.print("x ");
}else{
System.out.print(board[k][j]);
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
根据要求:ANT构建的详细信息:
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="Knightstour.mainclass"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
答案 0 :(得分:0)
您已将AvailableLocations类放在与mainclass相同的目录中,但AvailableLocations.java似乎缺少package
语句。尝试将package Knightstour;
添加到AvailableLocations。