Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package terrain;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Observable;
  10.  
  11. /**
  12. *
  13. * @author mikeg
  14. */
  15. public class NodeAlgorithmMove extends Observable implements Runnable {
  16.  
  17. public int runningTotal;
  18. private Terrain terrain;
  19. private boolean gameFinished;
  20. private int intelligence;
  21. private ArrayList<TerrainNode> terrainNodeArrayList;
  22. private TerrainNode[][] nodeMatrix;
  23.  
  24. public NodeAlgorithmMove(Terrain terrain, int intelligence) {
  25. this.runningTotal = 0;
  26. this.terrain = terrain;
  27. this.gameFinished = false;
  28. this.intelligence = intelligence;
  29. this.terrainNodeArrayList = new ArrayList<TerrainNode>();
  30. this.createGraph();
  31.  
  32. }
  33.  
  34. public void createGraph() {
  35. nodeMatrix = new TerrainNode[terrain.getNumRows()][terrain.getNumCols()];
  36. for (int i = 0; i < this.terrain.getNumRows(); i++) {
  37. for (int j = 0; j < this.terrain.getNumCols(); j++) {
  38. TerrainNode newNode = new TerrainNode(j, i, terrain.getDifficulty(i, j));
  39. nodeMatrix[i][j] = newNode;
  40. }
  41. }
  42. for (int i = 0; i < this.terrain.getNumRows(); i++) {
  43. for (int j = 0; j < this.terrain.getNumCols(); j++) {
  44. if (i == 0) {
  45. nodeMatrix[i][j].setLastRow(true);
  46. }
  47. if (i == this.terrain.getNumRows() - 1) {
  48. nodeMatrix[i][j].setFirstRow(true);
  49. }
  50. if (!nodeMatrix[i][j].isLastRow()) {
  51. nodeMatrix[i][j].setUpNode(nodeMatrix[i - 1][j]);
  52. if (j - 1 < 0) {
  53. nodeMatrix[i][j].setDiagonalRightNode(nodeMatrix[i - 1][j + 1]);
  54. nodeMatrix[i][j].setDiagonalLeftNode(null);
  55. } else if (j + 1 == this.terrain.getNumCols()) {
  56. nodeMatrix[i][j].setDiagonalLeftNode(nodeMatrix[i - 1][j - 1]);
  57. nodeMatrix[i][j].setDiagonalRightNode(null);
  58. } else {
  59. nodeMatrix[i][j].setDiagonalLeftNode(nodeMatrix[i - 1][j - 1]);
  60. nodeMatrix[i][j].setDiagonalRightNode(nodeMatrix[i - 1][j + 1]);
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. public void setRunningTotal(int value) {
  68. this.runningTotal += value;
  69. setChanged();
  70. notifyObservers();
  71. }
  72.  
  73. public int getRunningTotal() {
  74. return this.runningTotal;
  75. }
  76.  
  77. public boolean isGameFinished() {
  78. return gameFinished;
  79. }
  80.  
  81. public void setGameFinished(boolean gameFinished) {
  82. this.gameFinished = gameFinished;
  83. setChanged();
  84. notifyObservers();
  85. }
  86.  
  87. @Override
  88. public void run() {
  89. while (!gameFinished) {
  90. if (this.intelligence == 0) {
  91. this.greedyAlgorithm(null);
  92. } else {
  93. this.solveWithIntelligence();
  94. }
  95. }
  96. }
  97.  
  98. public int[][] buildSolutionMatrix() {
  99. int[][] sm = new int[terrain.getNumRows()][terrain.getNumCols()];
  100. for (int i = 0; i < terrain.getNumCols(); i++) {
  101. sm[terrain.getNumRows() - 1][i] = terrain.getDifficulty(terrain.getNumRows() - 1, i);
  102. }
  103. for (int i = terrain.getNumRows() - 2; i >= 0; i--) {
  104. for (int j = 0; j < terrain.getNumCols(); j++) {
  105. System.out.println("current node: " + nodeMatrix[i][j].getDifficulty());
  106. int lowest = 0;
  107. if (j != 0 && j != terrain.getNumCols() - 1) {
  108. lowest = Math.min(Math.min(sm[i + 1][j - 1], sm[i + 1][j]), sm[i + 1][j + 1]);
  109. } else if (j == 0) {
  110. lowest = Math.min(Math.min(999, sm[i + 1][j]), sm[i + 1][j + 1]);
  111. } else if (j == terrain.getNumCols() - 1) {
  112. lowest = Math.min(Math.min(sm[i + 1][j - 1], sm[i + 1][j]), 999);
  113. }
  114. System.out.println("The lowest below " + nodeMatrix[i][j].getDifficulty() + " is " + lowest);
  115. sm[i][j] = lowest + nodeMatrix[i][j].getDifficulty();
  116. System.out.println("sm " + i + " : " + j + " is set to " + (lowest + nodeMatrix[i][j].getDifficulty()));
  117. }
  118. }
  119. return sm;
  120. }
  121.  
  122. public void solveWithIntelligence() {
  123.  
  124. int[][] sm = this.buildSolutionMatrix();
  125. int[] lowestIndex = new int[2];
  126. TerrainNode previousSelectedNode = null;
  127.  
  128. for (int i = 0; i < terrain.getNumRows(); i++) {
  129. int lowest = 999;
  130. lowestIndex[0] = i;
  131. lowestIndex[1] = 0;
  132. for (int j = 0; j < terrain.getNumCols(); j++) {
  133. if (sm[i][j] <= lowest) {
  134. if ((i > 0 && j > previousSelectedNode.getColumn() + 1) || (i > 0 && j < previousSelectedNode.getColumn() - 1)) {
  135. System.out.println("Disregarding " + i + " " + j);
  136. } else if (sm[i][j] == lowest) {
  137. System.out.println("Checking for the best node between " + terrain.getDifficulty(i, j) + " and " + terrain.getDifficulty(lowestIndex[0], lowestIndex[1]));
  138. if (terrain.getDifficulty(i, j) < terrain.getDifficulty(lowestIndex[0], lowestIndex[1])) {
  139. System.out.println(terrain.getDifficulty(i, j) + " is lower and was selected");
  140. lowest = sm[i][j];
  141. lowestIndex[0] = i;
  142. lowestIndex[1] = j;
  143. } else {
  144. System.out.println(terrain.getDifficulty(lowestIndex[0], lowestIndex[1]) + " is lower and was selected");
  145. }
  146. } else {
  147. lowest = sm[i][j];
  148. lowestIndex[0] = i;
  149. lowestIndex[1] = j;
  150. }
  151. }
  152. }
  153. System.out.println("Setting visited " + lowestIndex[0] + " " + lowestIndex[1]);
  154. terrain.setVisited(lowestIndex[0], lowestIndex[1], true);
  155. previousSelectedNode = nodeMatrix[lowestIndex[0]][lowestIndex[1]];
  156. this.setRunningTotal(terrain.getDifficulty(lowestIndex[0], lowestIndex[1]));
  157. }
  158.  
  159. System.out.println("yeet");
  160. this.setGameFinished(true);
  161. }
  162.  
  163. public void greedyAlgorithm(TerrainNode currentNode) {
  164. if (intelligence == 0) {
  165. currentNode = this.findLowestFirstRow();
  166. }
  167.  
  168. while (currentNode.getUpNode() != null) {
  169. int lowest;
  170. if (currentNode.getDiagonalLeftNode() == null) {
  171. lowest = Math.min(Math.min(999, currentNode.getUpNode().getDifficulty()), currentNode.getDiagonalRightNode().getDifficulty());
  172. } else if (currentNode.getDiagonalRightNode() == null) {
  173. lowest = Math.min(Math.min(currentNode.getDiagonalLeftNode().getDifficulty(), currentNode.getUpNode().getDifficulty()), 999);
  174. } else {
  175. lowest = Math.min(Math.min(currentNode.getDiagonalLeftNode().getDifficulty(), currentNode.getUpNode().getDifficulty()), currentNode.getDiagonalRightNode().getDifficulty());
  176. }
  177.  
  178. if (currentNode.getDiagonalLeftNode() != null && lowest == currentNode.getDiagonalLeftNode().getDifficulty()) {
  179. currentNode = currentNode.getDiagonalLeftNode();
  180. } else if (currentNode.getUpNode() != null && lowest == currentNode.getUpNode().getDifficulty()) {
  181. currentNode = currentNode.getUpNode();
  182. } else if (currentNode.getDiagonalRightNode() != null && lowest == currentNode.getDiagonalRightNode().getDifficulty()) {
  183. currentNode = currentNode.getDiagonalRightNode();
  184. }
  185. terrain.setVisited(currentNode.getRow(), currentNode.getColumn(), true);
  186. this.setRunningTotal(currentNode.getDifficulty());
  187. }
  188. this.setGameFinished(true);
  189. }
  190.  
  191. public TerrainNode findLowestFirstRow() {
  192. TerrainNode currentNode = null;
  193.  
  194. int i = terrain.getNumRows() - 1;
  195. TerrainNode lowestNode = nodeMatrix[i][0];
  196. for (int j = 0; j < terrain.getNumCols(); j++) {
  197. currentNode = nodeMatrix[i][j];
  198. if (currentNode.getDifficulty() < lowestNode.getDifficulty()) {
  199. lowestNode = currentNode;
  200. }
  201. }
  202. terrain.setVisited(lowestNode.getRow(), lowestNode.getColumn(), true);
  203. this.setRunningTotal(lowestNode.getDifficulty());
  204. return lowestNode;
  205.  
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement