Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean recursiveCheck(Board board, Piece[] pieces, PrintableBoard pb) {
- // While solution incomplete
- for (int y = 0; y < board.getDimension() && currentPiece < pieces.length; y++) {
- for (int x = 0; x < board.getDimension() && currentPiece < pieces.length; x++) {
- // If operator is unapplied (x and y coordinates)
- if (!board.hasPiece(x, y)) {
- // Apply operator
- board.setPieceCoordinates(currentPiece, x, y);
- // If state is valid
- if (!board.hasThreatenedPieces()) {
- pb.paintImmediately(0, 0, pb.getWidth(), pb.getHeight());
- try
- {
- Thread.sleep(200); // sleep for 20 milliseconds
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- // Store state
- currentPiece++;
- // If solution incomplete
- if (currentPiece < pieces.length) {
- // Recursive call to itself
- if (recursiveCheck(board, pieces, pb)) {
- return true;
- }
- // If state is invalid
- if (board.hasThreatenedPieces()) {
- // Delete state
- board.setPieceCoordinates(currentPiece, -1, -1);
- currentPiece--;
- }
- } else {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement