vendredi 9 octobre 2020

How to make a knight randomly move in a chessboard?

I have this program called knight tour where the knight moves around a chess board. I have been trying to figure out how to make the knight move randomly, instead of following a pattern.

I would like to know how to randomly move the knight.

Here's my code:

package assignment3;

import java.util.Random;

/*
 * knows its current position (row and column) 
 * knows the eight types of moves it can make
 * can tell you it’s current row and column
 * can determine whether a move of a given type is legal or not
 * can move
 */
public class Knight {
    private int boardSize = 8;
    private int[] rowMoves = {-1, -2, -2, -1, 1, 2, 2, 1};
    private int[] colMoves = {2, 1, -1, -2, -2, -1, 1, 2};

    public Knight() {
        //ignore this constructor
    }

    public void InitializeBoard() {
        //initialize board
        for (int i = 0; i < boardSize; i++)
            Arrays.fill(chessboard2[i], Integer.MIN_VALUE); //setting array to negative value
    }

    /**
     * calls method canMove to check if knight can move
     * moves knight
     */
    public boolean move(int moveNum, int x, int y, int[][] chessboard2) {
        Random rand = new Random(); 
        //if moveNum == 64 all squares have been visited 
        if (moveNum == 64) {
            System.out.println("\ntrue board is 64\n");
            return true;
        }
        
        //int nextRow = rand.nextInt(boardSize);
        //int nextCol = rand.nextInt(boardSize);
        
        //for loop to try 8 possibe moves
        for (int i = 0; i < rowMoves.length; i++) {
            int nextRow =  x + rowMoves[i];
            int nextCol =  y + colMoves[i];
       
            //check if postion is valid and not visited yet
            if (canMove(nextRow, nextCol) && chessboard2[nextRow][nextCol] == Integer.MIN_VALUE) {
                //if move is valid knight moves
                chessboard2[nextRow][nextCol] = moveNum + 1;
                
                //make next move
                if(move(moveNum + 1, nextRow, nextCol, chessboard2))
                   return true;

                //move(moveNum + 1, nextRow, nextCol);
           
                //if cant find next move: backtrack
                chessboard2[nextRow][nextCol] = Integer.MIN_VALUE;
            }
        }
        
        return false;
    }     

    /**
     * calls method moveLegal from class Chessboard to see if move is legal
     * @return true if move is legal, else return false
     */
    public boolean canMove(int x, int y) {
        //if statement to check if currentRow and currentCol is whithin 
        //boundaries
        return(x >= 0 && x < boardSize && y >= 0 && y < boardSize);
    }

    public void print() {
        for (int i = 0; i < boardSize; i++)
            System.out.println(String.join(" ", chessboard2[i]));
    }
 
    public void solve() {
        //setting array location [0][0] to 0
        chessboard2[0][0] = 1;
    
        //check move
        if (move(1, 0, 0)) // if true, it will print chess board
            print();
        else //if false, there is no solution
            System.out.print("no solution");
    }
}

public class TesterMain {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Knight test = new Knight();
        test.solve();
    }
}

Sorry if my code is a bit messy, I am still working on the program.




Aucun commentaire:

Enregistrer un commentaire