0

私は、ナイトのツアーコードでナイトが取る移動をランダム化することに取り組んでいます。ただし、何らかの理由で、私の switch ステートメントは、可能な移動を実行する代わりに、常にデフォルトになります。私は自分で個別に実行firstMoveChoice()して機能したため、可能な移動方法が機能することを知っています。だから私は問題が私のランダムな動きのピッカーにあることを知っていますが、私はそれを理解できないようです.

ご協力ありがとうございます。この問題を調べても答えが見つからないので、本当に感謝しています。

public class MoveKnight extends Moves {

public int[][] moveTheKnight() {

    Moves switchBetweenMoves = new Moves();
    switchBetweenMoves.startingLocation();

    while (knight != 64) {
        int randomMove = new Random().nextInt();
        switch (randomMove) {
            case 1:
                switchBetweenMoves.firstMoveChoice();
                break;

            case 2:
                switchBetweenMoves.secondMoveChoice();
                break;

            case 3:
                switchBetweenMoves.thirdMoveChoice();
                break;

            // other possible moves

            default:
                System.out.println("No more possible moves.");
                break;
        }
        break;
    }
    return board;
}
}

上記のコードの結果は次のとおりです。

1  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
No more possible moves.
BUILD SUCCESSFUL (total time: 0 seconds)

これが私の主な方法です:

public static void main(String[] args) {

    MoveKnight myKnight = new MoveKnight();
    myKnight.moveTheKnight();
}

これが私の ChessBoard クラスです。

public class ChessBoard {

int[][] board;

public ChessBoard() {
    this.board = new int[8][8];
}
}

ここはfirstMoveChoice()

public int[][] firstMoveChoice() {

System.out.println();

x += 1;
y += 2;

if (x >= board.length) { // this tests to make sure the knight does not move off the row
    System.out.println("Cannot move off board on x axis\n");
    x -= 1;
    y -= 2;
    return board;
}
else if (y >= board.length) { // this tests to make sure the knight does not move off the column
    System.out.println("Cannot move off board on y axis\n");
    x -= 1;
    y -= 2;
    return board;
}
else if (board[x][y] != 0) { // this prevents the knight from landing on a previously landed on square
    x -= 1;
    y -= 2;
    System.out.println("Cannot move onto a used square\n");
    return board;
}
else { // this moves the knight when the above statements are false
    board[x][y] = ++knight;
    System.out.println("This executed and the knight moved\n");

    for(int[] row : board) {
        printRow(row);
    }

    if (knight == 64) {
        System.out.println("Knight has completed the tour");
        }

        return board;
    }
}

これはthirdMoveChoice(): 異なる移動の数の変更を除いて、他のすべてのものと実質的に同じです。

public int[][] thirdMoveChoice() {

System.out.println();

x += 2;
y -= 1;

if (x >= board.length) { // this tests to make sure the knight does not move off the row
    System.out.println("Cannot move off board on x axis\n");
    x -= 2;
    y += 1;
    return board;
}
else if (y >= board.length) { // this tests to make sure the knight does not move off the column
    System.out.println("Cannot move off board on y axis\n");
    x -= 2;
    y += 1;
    return board;
}
else if (board[x][y] != 0) { // this prevents the knight from landing on a previously landed on square
    x -= 2;
    y += 1;
    System.out.println("Cannot move onto a used square\n");
    return board;
}
else { // this moves the knight when the above statements are false
    board[x][y] = ++knight;
    System.out.println("This executed and the knight moved\n");
}

for(int[] row : board) {
    printRow(row);
}

if (knight == 64) {
    System.out.println("Knight has completed the tour");
    return board;
}

return board;
}
4

1 に答える 1