java - Issues with Knights Tour
问题描述
I've made this piece of code to complete the knight's tour, where you have to make it to every square on the chessboard with a knight with no repeats, but I'm having some issues. It only ever prints an empty chessboard with no moves made, and I'm not sure how to fix it.
import javax.lang.model.util.ElementScanner6;
public class KnightsTour
{
private int[][] ChessBoard = new int[9][9];
private boolean[] Taken = new boolean[9];
private int[] Horizontal = {0,1,2,2,1,-1,-2,-2,-1};
private int[] Vertical = {0,-2,-1,1,2,2,1,-1,-2};
private int myRow, myCol;
private int moveCount;
public KnightsTour()
{
doKnightsTour();
}
public void doKnightsTour()
{
while(moveKnight()==true)
{
}
printChessBoard();
}
private boolean moveKnight()
{
int randomPos;
boolean moveMade = false;
if(moveCount < 64)
{
if(allTakenNearby() == false)
{
while(moveMade = false)
{
randomPos = (int)(Math.random()*8) + 1;
if(Taken[randomPos] == false)
{
myRow += Vertical[randomPos];
myCol += Horizontal[randomPos];
moveCount++;
moveMade = true;
}
}
}
}
return moveMade;
}
private boolean allTakenNearby()
{
int tempRow;
int tempCol;
boolean allFilled = true;
boolean notTaken;
for(int i = 1; i<= 8; i++)
{
Taken[i] = false;
}
for(int i = 1; i <= 8; i++)
{
tempRow = myRow + Vertical[i];
tempCol = myCol + Horizontal[i];
if(tempRow >= 1 && tempRow <= 8 && tempCol >= 1 && tempCol <=8 )
{
if(ChessBoard[tempRow][tempCol] > 0 )
{
Taken[i] = true;
}
else
{
allFilled = false;
}
}
else
{
Taken[i] = true;
}
}
return allFilled;
}
private void printChessBoard()
{
int x;
int y;
System.out.println();
System.out.println("ChessBoard is: ");
System.out.println();
for(x = 1; x <= 8; x++)
{
for(y = 1; y <= 8; y++)
{
if(y == 8)
{
if(ChessBoard[x][y] == 0)
{
System.out.print("xx");
System.out.print(" ");
}
else if(ChessBoard[x][y] > 0 && ChessBoard[x][y] <= 9)
{
System.out.print("0" + ChessBoard[x][y]);
System.out.print(" ");
}
else
{
System.out.print(ChessBoard[x][y]);
System.out.print(" ");
}
}
else
{
if(ChessBoard[x][y] == 0)
{
System.out.print("xx");
System.out.print(" ");
}
else if(ChessBoard[x][y] > 0 && ChessBoard[x][y] <= 9)
{
System.out.print("0" + ChessBoard[x][y]);
System.out.print(" ");
}
else
{
System.out.print(ChessBoard[x][y]);
System.out.print(" ");
}
}
}
System.out.println();
}
System.out.println();
System.out.println("Amount of Moves Made = " + moveCount);
}
}
Tester:
import javax.lang.model.util.ElementScanner6;
public class KnightsTourTester
{
public static void main(String args[])
{
KnightsTour tour = new KnightsTour();
}
}
Any suggestions would be appreciated, I'm doing this for a class so I need to get it done soon.
解决方案
while(moveMade = false)
- assignment instead of comparison (and don't use comparisons on booleans, please - while (!moveMade)
)
推荐阅读
- bash - 如何使用 BASH 返回目录中多个文件的 MD5 和 SHA1 值
- java - 如果所有周期对象都是连续周期,如何使用 java 8 (LAMBDA) 检查集合
- azure - 如何在 Azure 服务总线上转发过期消息
- linux - 如何使我的 bash 脚本在下载时自动变成终端命令?
- chapel - 是否有使用 Chapel 编译器进行多处理器编译的选项?
- javascript - 在 React Native 中单击下一个按钮时如何聚焦第二个字段
- javascript - 在 Fabric 对象移动事件中获取 x/y 偏移
- react-native - 带有反应导航 5 的 switchNavigator
- cognos - 如何在 IBM Cognos 中基于多个条件创建计算
- create-react-app - 在 create-react-app 中找不到弹出命令 - 如何添加到 package.json?