Implement more types of heuristics

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
2019-10-26 20:50:41 -03:00
parent 8f901d8f0a
commit bbf10d7c95
4 changed files with 42 additions and 13 deletions

View File

@@ -34,6 +34,10 @@ public interface Constants {
* The name of the game
*/
String TITLE = "La Aventura de Azaraka";
/**
* The heuristic to use in the AI distance calculations
*/
AIHeuristic aIHeuristic = AIHeuristic.MANHATTAN;
/**
* Which type of AI to use
*/
@@ -186,6 +190,24 @@ public interface Constants {
}
}
/**
* The different heuristics that the AI uses to calculate distance
*/
enum AIHeuristic {
/**
* Used for 4 direction movements
*/
MANHATTAN,
/**
* Used for 8 direction movements
*/
DIAGONAL,
/**
* Used for distance between 2 points
*/
EUCLIDEAN
}
/**
* The different AI that can be used by the player
*/

View File

@@ -22,6 +22,7 @@ import cl.cromer.azaraka.object.Portal;
import cl.cromer.azaraka.sprite.Animation;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
@@ -52,28 +53,36 @@ public interface PlayerAI extends Runnable, Constants {
/**
* The heuristic to get the distance between the start state and the end state
*
* <p>
* Manhattan Distance
* Used for 4 direction movements
* h = abs (current_cell.x goal.x) +
* abs (current_cell.y goal.y)
*
* h = abs (current_cell.x goal.x) +
* abs (current_cell.y goal.y)
* <p>
* Diagonal Distance
* Used for 8 direction movements
* h = max { abs(current_cell.x goal.x),
* abs(current_cell.y goal.y) }
*
* h = max { abs(current_cell.x goal.x),
* abs(current_cell.y goal.y) }
* <p>
* Euclidean Distance
* Used for distance between 2 points
* h = sqrt ( (current_cell.x goal.x)2 +
* (current_cell.y goal.y)2 )
* h = sqrt ( (current_cell.x goal.x)2 +
* (current_cell.y goal.y)2 )
*
* @param start The start state
* @param goal The goal state
* @return Returns the distance between the states
*/
default double heuristic(State start, State goal) {
return Math.abs(start.getX() - goal.getX()) + Math.abs(start.getY() - goal.getY());
switch (aIHeuristic) {
case DIAGONAL:
return Math.max(Math.abs(start.getX() - goal.getX()), Math.abs(start.getY() - goal.getY()));
case EUCLIDEAN:
return Point2D.distance(start.getX(), start.getY(), goal.getX(), goal.getY());
case MANHATTAN:
default:
return Math.abs(start.getX() - goal.getX()) + Math.abs(start.getY() - goal.getY());
}
}
/**

View File

@@ -504,7 +504,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
* Initialize the enemy cost and level
*
* @param level The level
* @param cost The cost
* @param cost The cost
*/
EnemyCost(int level, int cost) {
this.level = level;