Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package terrain;
- import java.util.ArrayList;
- import java.util.Observable;
- /**
- *
- * @author mikeg
- */
- public class NodeAlgorithmMove extends Observable implements Runnable {
- public int runningTotal;
- private Terrain terrain;
- private boolean gameFinished;
- private int intelligence;
- private ArrayList<TerrainNode> terrainNodeArrayList;
- private TerrainNode[][] nodeMatrix;
- public NodeAlgorithmMove(Terrain terrain, int intelligence) {
- this.runningTotal = 0;
- this.terrain = terrain;
- this.gameFinished = false;
- this.intelligence = intelligence;
- this.terrainNodeArrayList = new ArrayList<TerrainNode>();
- this.createGraph();
- }
- public void createGraph() {
- nodeMatrix = new TerrainNode[terrain.getNumRows()][terrain.getNumCols()];
- for (int i = 0; i < this.terrain.getNumRows(); i++) {
- for (int j = 0; j < this.terrain.getNumCols(); j++) {
- TerrainNode newNode = new TerrainNode(j, i, terrain.getDifficulty(i, j));
- nodeMatrix[i][j] = newNode;
- }
- }
- for (int i = 0; i < this.terrain.getNumRows(); i++) {
- for (int j = 0; j < this.terrain.getNumCols(); j++) {
- if (i == 0) {
- nodeMatrix[i][j].setLastRow(true);
- }
- if (i == this.terrain.getNumRows() - 1) {
- nodeMatrix[i][j].setFirstRow(true);
- }
- if (!nodeMatrix[i][j].isLastRow()) {
- nodeMatrix[i][j].setUpNode(nodeMatrix[i - 1][j]);
- if (j - 1 < 0) {
- nodeMatrix[i][j].setDiagonalRightNode(nodeMatrix[i - 1][j + 1]);
- nodeMatrix[i][j].setDiagonalLeftNode(null);
- } else if (j + 1 == this.terrain.getNumCols()) {
- nodeMatrix[i][j].setDiagonalLeftNode(nodeMatrix[i - 1][j - 1]);
- nodeMatrix[i][j].setDiagonalRightNode(null);
- } else {
- nodeMatrix[i][j].setDiagonalLeftNode(nodeMatrix[i - 1][j - 1]);
- nodeMatrix[i][j].setDiagonalRightNode(nodeMatrix[i - 1][j + 1]);
- }
- }
- }
- }
- }
- public void setRunningTotal(int value) {
- this.runningTotal += value;
- setChanged();
- notifyObservers();
- }
- public int getRunningTotal() {
- return this.runningTotal;
- }
- public boolean isGameFinished() {
- return gameFinished;
- }
- public void setGameFinished(boolean gameFinished) {
- this.gameFinished = gameFinished;
- setChanged();
- notifyObservers();
- }
- @Override
- public void run() {
- while (!gameFinished) {
- if (this.intelligence == 0) {
- this.greedyAlgorithm(null);
- } else {
- this.solveWithIntelligence();
- }
- }
- }
- public int[][] buildSolutionMatrix() {
- int[][] sm = new int[terrain.getNumRows()][terrain.getNumCols()];
- for (int i = 0; i < terrain.getNumCols(); i++) {
- sm[terrain.getNumRows() - 1][i] = terrain.getDifficulty(terrain.getNumRows() - 1, i);
- }
- for (int i = terrain.getNumRows() - 2; i >= 0; i--) {
- for (int j = 0; j < terrain.getNumCols(); j++) {
- System.out.println("current node: " + nodeMatrix[i][j].getDifficulty());
- int lowest = 0;
- if (j != 0 && j != terrain.getNumCols() - 1) {
- lowest = Math.min(Math.min(sm[i + 1][j - 1], sm[i + 1][j]), sm[i + 1][j + 1]);
- } else if (j == 0) {
- lowest = Math.min(Math.min(999, sm[i + 1][j]), sm[i + 1][j + 1]);
- } else if (j == terrain.getNumCols() - 1) {
- lowest = Math.min(Math.min(sm[i + 1][j - 1], sm[i + 1][j]), 999);
- }
- System.out.println("The lowest below " + nodeMatrix[i][j].getDifficulty() + " is " + lowest);
- sm[i][j] = lowest + nodeMatrix[i][j].getDifficulty();
- System.out.println("sm " + i + " : " + j + " is set to " + (lowest + nodeMatrix[i][j].getDifficulty()));
- }
- }
- return sm;
- }
- public void solveWithIntelligence() {
- int[][] sm = this.buildSolutionMatrix();
- int[] lowestIndex = new int[2];
- TerrainNode previousSelectedNode = null;
- for (int i = 0; i < terrain.getNumRows(); i++) {
- int lowest = 999;
- lowestIndex[0] = i;
- lowestIndex[1] = 0;
- for (int j = 0; j < terrain.getNumCols(); j++) {
- if (sm[i][j] <= lowest) {
- if ((i > 0 && j > previousSelectedNode.getColumn() + 1) || (i > 0 && j < previousSelectedNode.getColumn() - 1)) {
- System.out.println("Disregarding " + i + " " + j);
- } else if (sm[i][j] == lowest) {
- System.out.println("Checking for the best node between " + terrain.getDifficulty(i, j) + " and " + terrain.getDifficulty(lowestIndex[0], lowestIndex[1]));
- if (terrain.getDifficulty(i, j) < terrain.getDifficulty(lowestIndex[0], lowestIndex[1])) {
- System.out.println(terrain.getDifficulty(i, j) + " is lower and was selected");
- lowest = sm[i][j];
- lowestIndex[0] = i;
- lowestIndex[1] = j;
- } else {
- System.out.println(terrain.getDifficulty(lowestIndex[0], lowestIndex[1]) + " is lower and was selected");
- }
- } else {
- lowest = sm[i][j];
- lowestIndex[0] = i;
- lowestIndex[1] = j;
- }
- }
- }
- System.out.println("Setting visited " + lowestIndex[0] + " " + lowestIndex[1]);
- terrain.setVisited(lowestIndex[0], lowestIndex[1], true);
- previousSelectedNode = nodeMatrix[lowestIndex[0]][lowestIndex[1]];
- this.setRunningTotal(terrain.getDifficulty(lowestIndex[0], lowestIndex[1]));
- }
- System.out.println("yeet");
- this.setGameFinished(true);
- }
- public void greedyAlgorithm(TerrainNode currentNode) {
- if (intelligence == 0) {
- currentNode = this.findLowestFirstRow();
- }
- while (currentNode.getUpNode() != null) {
- int lowest;
- if (currentNode.getDiagonalLeftNode() == null) {
- lowest = Math.min(Math.min(999, currentNode.getUpNode().getDifficulty()), currentNode.getDiagonalRightNode().getDifficulty());
- } else if (currentNode.getDiagonalRightNode() == null) {
- lowest = Math.min(Math.min(currentNode.getDiagonalLeftNode().getDifficulty(), currentNode.getUpNode().getDifficulty()), 999);
- } else {
- lowest = Math.min(Math.min(currentNode.getDiagonalLeftNode().getDifficulty(), currentNode.getUpNode().getDifficulty()), currentNode.getDiagonalRightNode().getDifficulty());
- }
- if (currentNode.getDiagonalLeftNode() != null && lowest == currentNode.getDiagonalLeftNode().getDifficulty()) {
- currentNode = currentNode.getDiagonalLeftNode();
- } else if (currentNode.getUpNode() != null && lowest == currentNode.getUpNode().getDifficulty()) {
- currentNode = currentNode.getUpNode();
- } else if (currentNode.getDiagonalRightNode() != null && lowest == currentNode.getDiagonalRightNode().getDifficulty()) {
- currentNode = currentNode.getDiagonalRightNode();
- }
- terrain.setVisited(currentNode.getRow(), currentNode.getColumn(), true);
- this.setRunningTotal(currentNode.getDifficulty());
- }
- this.setGameFinished(true);
- }
- public TerrainNode findLowestFirstRow() {
- TerrainNode currentNode = null;
- int i = terrain.getNumRows() - 1;
- TerrainNode lowestNode = nodeMatrix[i][0];
- for (int j = 0; j < terrain.getNumCols(); j++) {
- currentNode = nodeMatrix[i][j];
- if (currentNode.getDifficulty() < lowestNode.getDifficulty()) {
- lowestNode = currentNode;
- }
- }
- terrain.setVisited(lowestNode.getRow(), lowestNode.getColumn(), true);
- this.setRunningTotal(lowestNode.getDifficulty());
- return lowestNode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement