我在完成这项家庭作业时遇到了一些问题。它应该使用骑士棋子并由用户接收棋盘上的起始位置,并查看它是否可以触及棋盘上的每个位置。董事会已经由我的教授编程,所以应该没问题。我们应该编写takeStep()和runTour()代码。我只是无法弄清楚如何让它运行。任何见解都会有所帮助,谢谢你。
package knightstoursetup;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author
*/
public class KnightMoves
{
public final int COMPLETE_TOUR = 64;
private ChessBoard kBoard = new ChessBoard();
private int[][] moves = new int[8][2];
private int stepNumber;
private int currentRow;
private int currentCol;
private int startRow;
private int startCol;
int nextRow;
int nextCol;
Random randNum = new Random();
/**
* CONSTRUCTOR initialize class instance variables and objects
*/
public KnightMoves()
{
moves[0][0] = -2; // move 0 row offset
moves[0][1] = -1; // move 0 col offset
moves[1][0] = -2; // move 1 row offset
moves[1][1] = 1; // move 1 col offset
moves[2][0] = -1; // move 2 row offset
moves[2][1] = 2; // move 2 col offset
moves[3][0] = 1; // move 3 row offset
moves[3][1] = 2; // move 3 col offset
moves[4][0] = 2; // move 4 row offset
moves[4][1] = 1; // move 4 col offset
moves[5][0] = 2; // move 5 row offset
moves[5][1] = -1; // move 5 col offset
moves[6][0] = 1; // move 6 row offset
moves[6][1] = -2; // move 6 col offset
moves[7][0] = -1; // move 7 row offset
moves[7][1] = -2; // move 7 col offset
stepNumber = 0;
startRow = startCol = 1;
currentRow = currentCol = 1;
}
/**
* Description: Get the starting board Position (row, col) for the
* KNIGHT from program user
*/
public void getStartPosition()
{
Scanner input = new Scanner(System.in);
do{
System.out.print("\n enter a starting location for the knight's row ");
startRow = input.nextInt();
} while (1 > startRow || startRow > 8);
do {
System.out.print("\n enter a starting location for the knight's col ");
startCol = input.nextInt();
} while (1 > startCol || startCol > 8);
currentRow = startRow + 1;
currentCol = startCol + 1;
kBoard.setSquare(currentRow, currentCol, ++stepNumber);
System.out.println(kBoard);
}
/**
* Mutator: runTour() Description: runs the application by retrieving the
* starting position from the user, and then attempting to complete a tour by
* taking steps as long as the knight can find a valid move (unvisited, on
* the board square). Once the knight gets stuck OR he completes the tour, it
* prints the results of the tour
*
* preconditions: none postconditions: runs and eports the results of a
* Knight's Tour calls: instructions() getStartPosition for User input for
* step 1 takeStep() (repeatedly, until either completes the tour or gets
* stuck toString(this) to report the outcome of the tour
*/
public void runTour() {
// instructions();
getStartPosition();
takeStep();
System.out.println(this);
}
/**
* Accessor: takeStep() Description: generates a random number between 0 .. 7
* that corresponds to the [possible knight] moves array. Since 0 denotes a
* valid unvisited square, we can use that to check for making a step. If the
* currentRow + the moves[offset], currentCol + the moves[offset] has a 0,
* then we can "step" into this square, otherwise we need to try another
* random step. If the knight attempts 200 steps and still can't find an
* available square (from the 8 possible moves) then we assume he is "stuck"
* and takeStep will return a value of false for success.
*
* @return success of true if the knight is able to take another step on the
* board; false if there are no valid moves left
*/
private boolean takeStep() {
boolean success = false;
int attempts = stepNumber;
int num;
do {
} while (success == false && attempts < 200);
return success;
}
/**
* accessor: toString() reports the results of the knight's tour
*
* @return results of the knight's tour attempt for a complete tour
*/
public String toString() {
String whatHappened = "";
whatHappened += "Starting location: [" + startRow + ", "
+ startCol + "]\n";
whatHappened += " Tour ended after " + stepNumber + " steps\n";
whatHappened += "The knight got stuck in location ["
+ (currentRow - 1) + ", " + (currentCol - 1) + "]\n";
return whatHappened;
}
}
答案 0 :(得分:0)
为您的takeStep()
编码: