2013-11-25 49 views
-1

我在这个家庭作业中遇到了一些问题。它应该使用骑士棋子,并由用户在棋盘上占据起始位置,看它是否可触及棋盘上的每个位置。董事会已经由我的教授编程,所以应该没问题。我们应该编码takeStep()和runTour()。我无法弄清楚如何让它运行。任何见解都会有所帮助,所以谢谢。Java骑士之旅作业

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; 
    } 
} 
+1

#1的目标是不给见解。询问的人需要确定是否无效,调试,解释,然后询问为什么它不起作用,同时提供所有的细节。然后,当我们确切知道你在做什么时,我们可以提供帮助。 –

+1

它编译?你有运行时错误吗?如果你有任何错误信息,它会有所帮助。 '无法让它运行'是没有意义的。 – 2013-11-25 18:29:51

+2

当你抛出一些代码并要求人们为你调试时,你不可能得到很多帮助。运行它,调试它,尝试一些东西,并确保包含关于你发现的所有细节。那么你会得到很多帮助。如果看起来你自己做了一些工作,人们更倾向于帮助你。 – MadConan

回答

0

要编写你的takeStep()

  • “产生一个随机数...” 看到`Random
  • “如果...” 使用一个标准的if else子句
  • ” .. 。另一个随机步骤。“你需要有一个循环
  • “如果骑士试图200 ......”上限为你的循环