I am working on randomizing which move my knight takes in my knight’s tour code. For some reason though, my switch statement always goes to default, instead of executing a possible move. I know my possible moves methods work because I have separately ran firstMoveChoice()
on it’s own and it worked. So I know the problem is with my random move picker, but I can’t seem to figure it out.
Thanks for your help and time in advance! It is really much appreciated as I have looked this problem over and cannot find the answer.
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; } }
Here is the result of the above code:
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)
Here is my Main method:
public static void main(String[] args) { MoveKnight myKnight = new MoveKnight(); myKnight.moveTheKnight(); }
Here is my ChessBoard class:
public class ChessBoard { int[][] board; public ChessBoard() { this.board = new int[8][8]; } }
Here is 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 axisn"); 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 axisn"); 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 squaren"); 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 movedn"); for(int[] row : board) { printRow(row); } if (knight == 64) { System.out.println("Knight has completed the tour"); } return board; } }
Here is thirdMoveChoice()
: It is practically the same as all the others, except for the change in numbers for the different moves.
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 axisn"); 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 axisn"); 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 squaren"); 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 movedn"); } for(int[] row : board) { printRow(row); } if (knight == 64) { System.out.println("Knight has completed the tour"); return board; } return board; }
Advertisement
Answer
It’s because random.nextInt()
return a value between -2,147,483,648 and 2,147,483,647
you should use the random.nextInt(int n)
that return a value between 0 inclusive and n exclusive
so replace
int randomMove = new Random().nextInt(); b
by
int randomMove = 1 + new Random().nextInt(3);
Also, you should assign new Random()
to a field that would be reused.