Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. public boolean recursiveCheck(Board board, Piece[] pieces, PrintableBoard pb) {
  2. // While solution incomplete
  3. for (int y = 0; y < board.getDimension() && currentPiece < pieces.length; y++) {
  4. for (int x = 0; x < board.getDimension() && currentPiece < pieces.length; x++) {
  5. // If operator is unapplied (x and y coordinates)
  6. if (!board.hasPiece(x, y)) {
  7. // Apply operator
  8. board.setPieceCoordinates(currentPiece, x, y);
  9. // If state is valid
  10. if (!board.hasThreatenedPieces()) {
  11. pb.paintImmediately(0, 0, pb.getWidth(), pb.getHeight());
  12. try
  13. {
  14. Thread.sleep(200); // sleep for 20 milliseconds
  15. }
  16. catch (Exception e)
  17. {
  18. e.printStackTrace();
  19. }
  20. // Store state
  21. currentPiece++;
  22. // If solution incomplete
  23. if (currentPiece < pieces.length) {
  24. // Recursive call to itself
  25. if (recursiveCheck(board, pieces, pb)) {
  26. return true;
  27. }
  28. // If state is invalid
  29. if (board.hasThreatenedPieces()) {
  30. // Delete state
  31. board.setPieceCoordinates(currentPiece, -1, -1);
  32. currentPiece--;
  33. }
  34. } else {
  35. return true;
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return false;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement