Implement more types of heuristics

Signed-off-by: Chris Cromer <chris@cromer.cl>
master
Chris Cromer 3 years ago
parent 8f901d8f0a
commit bbf10d7c95
  1. 2
      gradle/wrapper/gradle-wrapper.properties
  2. 22
      src/main/java/cl/cromer/azaraka/Constants.java
  3. 29
      src/main/java/cl/cromer/azaraka/ai/PlayerAI.java
  4. 2
      src/main/java/cl/cromer/azaraka/ai/PlayerAStarAI.java

@ -12,8 +12,6 @@
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
#Sat Oct 26 13:11:28 CLST 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists

@ -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
*/

@ -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());
}
}
/**

@ -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;

Loading…
Cancel
Save