Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package ch.rfin.ai.search.problems;
  2.  
  3. import ch.rfin.ai.search.AbstractSearchProblem;
  4.  
  5. import java.util.*;
  6.  
  7. public class EightQueens extends AbstractSearchProblem<EightQueens.State, EightQueens.Action> {
  8.  
  9. private static final int SIZE = 8;
  10.  
  11. public EightQueens() {
  12. super(new State(0), new State(SIZE));
  13. }
  14.  
  15. private State result(State state, Action move){
  16.  
  17. int[] board = state.board;
  18.  
  19. for(int x = 0; x < SIZE; x++){
  20. if(board[x + move.y * SIZE] == 1){
  21. return null;
  22. }
  23. }
  24.  
  25. for(int y = 0; y < SIZE; y++){
  26. if(board[move.x + y * SIZE] == 1){
  27. return null;
  28. }
  29. }
  30.  
  31. for(int y = 0; y < SIZE; y++){
  32. for(int x = 0; x < SIZE; x++){
  33. if(board[x + y * SIZE] == 1){
  34.  
  35. int deltaRow = Math.abs(x - move.x);
  36. int deltaCol = Math.abs(y - move.y);
  37. if(deltaCol == deltaRow){
  38. return null;
  39. }
  40. }
  41. }
  42. }
  43.  
  44. return new State(state, move);
  45. }
  46.  
  47. @Override
  48. public Collection<Action> possibleActionsIn(State state) {
  49.  
  50. List<Action> actions = new ArrayList<>();
  51.  
  52. for(int y = 0; y < SIZE; y++){
  53. for(int x = 0; x < SIZE; x++){
  54. Action action = new Action(x, y);
  55. State after = result(state, action);
  56. if(after != null){
  57. actions.add(action);
  58. }
  59. }
  60. }
  61.  
  62. return actions;
  63. }
  64.  
  65. @Override
  66. public State transition(State state, Action action) {
  67.  
  68. State result = result(state, action);
  69.  
  70. if(result == null){
  71. throw new IllegalArgumentException("No transition for " + action + " in " + state + ".");
  72. }
  73. return result;
  74. }
  75.  
  76. public static class State{
  77. public int queens = 0;
  78.  
  79. public int board[] = {0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0,
  87. };
  88.  
  89.  
  90. public State(int queens){
  91. this.queens = queens;
  92. };
  93.  
  94. public State(State oldState, Action action){
  95. board = oldState.board.clone();
  96.  
  97. this.board[action.x + action.y * SIZE] = 1;
  98. this.queens = oldState.queens + 1;
  99.  
  100. }
  101.  
  102. @Override
  103. public boolean equals(Object obj) {
  104. return queens == ((State) obj).queens;
  105. }
  106.  
  107. @Override
  108. public String toString() {
  109. String string = "State: \n queens: " + queens + "\n board: \n";
  110. for(int y = 0; y < SIZE; y++){
  111. for(int x = 0; x < SIZE; x++){
  112. string += board[x + y * SIZE] + " ";
  113. }
  114. string += "\n";
  115. }
  116. return string;
  117. }
  118.  
  119.  
  120. }
  121.  
  122. public static class Action{
  123. public int x;
  124. public int y;
  125.  
  126. public Action(int x, int y){
  127. this.x = x;
  128. this.y = y;
  129. }
  130.  
  131. @Override
  132. public String toString() {
  133. return "Action{" +
  134. "x=" + x +
  135. ", y=" + y +
  136. '}';
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement