-1

私はこの宿題にいくつか問題があります。ナイト チェスの駒を使用し、ユーザーがボード上の開始位置を取り、ボード上のすべての位置に触れることができるかどうかを確認することになっています。ボードは私の教授によって既にプログラムされているので、問題ないはずです。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;
   }
}
4

1 に答える 1

0

をコーディングするにはtakeStep():

  • 「乱数を生成します...」`Randomを参照してください
  • "If the.." 標準の if else 句を使用する
  • 「……別のランダムなステップを試してください。」ループが必要です
  • 「騎士が200を試みると...」ループの上限
于 2013-11-25T18:34:09.057 に答える