Search algorithms for the 8-puzzle solution
Public Member Functions
Manhattan Class Reference

Implementation of the Manhattan distance for the 8-puzzle. More...

#include <Heuristic.hpp>

Inheritance diagram for Manhattan:
Collaboration diagram for Manhattan:

Public Member Functions

int calc (GameState &currentState)
 Calculates the heuristic value for a state. More...
 

Detailed Description

Implementation of the Manhattan distance for the 8-puzzle.

Definition at line 23 of file Heuristic.hpp.

Member Function Documentation

◆ calc()

int Manhattan::calc ( GameState currentState)
inlinevirtual

Calculates the heuristic value for a state.

Parameters
currentStatethe state to be evaluated
Returns
heuristic value for currentState

Implements Heuristic.

Definition at line 26 of file Heuristic.hpp.

26  {
27  int distance = 0, dimension = currentState.getDimension();
28 
29  for (int i = 0; i < pow(dimension, 2); i ++) {
30  int *pos = currentState.find(i);
31  int x = pos[0], y = pos[1];
32  delete[] pos;
33  int x_opt = i != 0 ? (i - 1) / dimension : 2,
34  y_opt = i != 0 ? (i - 1) % dimension : 2;
35 
36  distance += abs(x - x_opt) + abs(y - y_opt);
37  }
38  return distance;
39  }
int getDimension() const
Definition: GameState.hpp:284
int * find(int value)
Finds the position of a tile in the board.
Definition: GameState.hpp:318
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: