Implement more types of heuristics

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

View File

@ -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. # 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 distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists

View File

@ -34,6 +34,10 @@ public interface Constants {
* The name of the game * The name of the game
*/ */
String TITLE = "La Aventura de Azaraka"; 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 * 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 * 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 cl.cromer.azaraka.sprite.Animation;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -52,17 +53,17 @@ public interface PlayerAI extends Runnable, Constants {
/** /**
* The heuristic to get the distance between the start state and the end state * The heuristic to get the distance between the start state and the end state
* * <p>
* Manhattan Distance * Manhattan Distance
* Used for 4 direction movements * Used for 4 direction movements
* h = abs (current_cell.x goal.x) + * h = abs (current_cell.x goal.x) +
* abs (current_cell.y goal.y) * abs (current_cell.y goal.y)
* * <p>
* Diagonal Distance * Diagonal Distance
* Used for 8 direction movements * Used for 8 direction movements
* h = max { abs(current_cell.x goal.x), * h = max { abs(current_cell.x goal.x),
* abs(current_cell.y goal.y) } * abs(current_cell.y goal.y) }
* * <p>
* Euclidean Distance * Euclidean Distance
* Used for distance between 2 points * Used for distance between 2 points
* h = sqrt ( (current_cell.x goal.x)2 + * h = sqrt ( (current_cell.x goal.x)2 +
@ -73,8 +74,16 @@ public interface PlayerAI extends Runnable, Constants {
* @return Returns the distance between the states * @return Returns the distance between the states
*/ */
default double heuristic(State start, State goal) { default double heuristic(State start, State goal) {
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()); return Math.abs(start.getX() - goal.getX()) + Math.abs(start.getY() - goal.getY());
} }
}
/** /**
* Sort the destinations based on importance and distance * Sort the destinations based on importance and distance