Guest User

Untitled

a guest
Jan 17th, 2018
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package dmi.vi1.search.examples.fourinarow;
  2.  
  3. /**
  4. * @author Nemanja M. <nmilosev@dmi.uns.ac.rs>
  5. *
  6. */
  7.  
  8. public class FourInARowBoard {
  9.  
  10. public static int EMPTY = 0;
  11. public static int RED_DOT = 1;
  12. public static int YELLOW_DOT = 2;
  13.  
  14. public static final int X_DIMENSION = 7;
  15. public static final int Y_DIMENSION = 4;
  16.  
  17. private int[][] matrix;
  18.  
  19. public FourInARowBoard() {
  20. matrix = new int[Y_DIMENSION][X_DIMENSION];
  21. }
  22.  
  23. public FourInARowBoard(int[][] matrix) {
  24. this.matrix = matrix;
  25. }
  26.  
  27. public boolean isTerminalState() {
  28. return getEmptyPositionsNumber() == 0 || someOneWon();
  29. }
  30.  
  31. public int getValueAt(int x, int y) {
  32. return matrix[x][y];
  33. }
  34.  
  35. public void setValueAt(int x, int y, int value) {
  36. matrix[x][y] = value;
  37. }
  38.  
  39. public FourInARowBoard clone() {
  40. FourInARowBoard newBoard = new FourInARowBoard();
  41.  
  42. for (int i = 0; i < Y_DIMENSION; i++)
  43. for (int j = 0; j < X_DIMENSION; j++) {
  44. newBoard.setValueAt(i, j, this.getValueAt(i, j));
  45. }
  46.  
  47. return newBoard;
  48. }
  49.  
  50. private int getEmptyPositionsNumber() {
  51.  
  52. int emptyFields = 0;
  53.  
  54. for (int i = 0; i < Y_DIMENSION; i++) {
  55. for (int j = 0; j < X_DIMENSION; j++) {
  56. if (matrix[i][j] == EMPTY)
  57. emptyFields++;
  58. }
  59. }
  60.  
  61. return emptyFields;
  62. }
  63.  
  64. public boolean someOneWon() {
  65. return checkHorizontal() || checkVertical() || checkDiagonalFromLeftToRight() || checkDiagonalFromRightToLeft();
  66. }
  67.  
  68. private boolean checkVertical() {
  69. for (int j = 0; j < X_DIMENSION; j++) {
  70.  
  71. boolean completeVertical = true;
  72.  
  73. for (int i = 0; i < Y_DIMENSION - 1; i++) {
  74.  
  75. if (matrix[i][j] == EMPTY) {
  76. completeVertical = false;
  77. break;
  78. }
  79.  
  80. if (matrix[i][j] != matrix[i + 1][j])
  81. completeVertical = false;
  82.  
  83. }
  84.  
  85. if (completeVertical) {
  86. //System.out.println("Found complete vertical at: " + j + ", 0");
  87. return true;
  88. }
  89.  
  90. }
  91. return false;
  92. }
  93.  
  94. private boolean checkHorizontal() {
  95. for (int j = 0; j < Y_DIMENSION; j++) {
  96. for (int i = 0; i < X_DIMENSION - 4; i++) {
  97. if (matrix[i][j] != EMPTY && matrix[i][j] == matrix[i][j+1] && matrix[i][j+1] == matrix[i][j+2] && matrix[i][j+2] == matrix[i][j+3]) {
  98. //System.out.println("Found complete horizontal at: " + i + ", " + j);
  99. return true;
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105.  
  106. private boolean checkDiagonalFromLeftToRight() {
  107.  
  108. for (int i = 0; i < 4; i++) {
  109. int[] cache = new int[4];
  110.  
  111. for (int j = 0; j < 4; j++) {
  112. //System.out.println(j + "," + (j + i));
  113. cache[j] = matrix[j][j + i];
  114. }
  115.  
  116. if (cache[0] != EMPTY && cache[0] == cache[1] && cache[1] == cache[2] && cache[2] == cache[3]) {
  117. //System.out.println("Found complete LtR diagonal at: " + i + ", 0");
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123.  
  124. private boolean checkDiagonalFromRightToLeft() {
  125.  
  126. for (int i = 0; i < 4; i++) {
  127. int[] cache = new int[4];
  128.  
  129. for (int j = 3; j >= 0; j--) {
  130. //System.out.println(j + "," + (i + 3 - j));
  131. cache[j] = matrix[j][i + 3 - j];
  132. }
  133.  
  134. if (cache[0] != EMPTY && cache[0] == cache[1] && cache[1] == cache[2] && cache[2] == cache[3]) {
  135. //System.out.println("Found complete RtL diagonal at: " + i + ", 3");
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141.  
  142. @Override
  143. public String toString() {
  144. StringBuilder sb = new StringBuilder();
  145.  
  146. for (int i = 0; i < Y_DIMENSION; i++) {
  147. for (int j = 0; j < X_DIMENSION; j++) {
  148. sb.append("|").append(this.getValueAt(i, j)).append("|");
  149. }
  150. sb.append("\n");
  151. }
  152.  
  153. return sb.toString()
  154. .replace("||", "|")
  155. .replace(String.valueOf(RED_DOT), "R")
  156. .replace(String.valueOf(YELLOW_DOT), "Y")
  157. .replace("0", " ");
  158. }
  159. }
Add Comment
Please, Sign In to add comment