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

Implementation of the tile difference heuristic for the 8-puzzle. More...

#include <Heuristic.hpp>

Inheritance diagram for TileDifference:
Collaboration diagram for TileDifference:

Public Member Functions

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

Detailed Description

Implementation of the tile difference heuristic for the 8-puzzle.

Definition at line 43 of file Heuristic.hpp.

Member Function Documentation

◆ calc()

int TileDifference::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 46 of file Heuristic.hpp.

46  {
47  int distance = 0, dimension = currentState.getDimension();
48 
49  // adds 1 for every tile that is not in its goal position
50  for (int i = 0; i < pow(dimension, 2); i ++) {
51  int *pos = currentState.find(i);
52  int x = pos[0], y = pos[1];
53  delete[] pos;
54  int x_opt = i != 0 ? (i - 1) / dimension : 2,
55  y_opt = i != 0 ? (i - 1) % dimension : 2;
56 
57  distance += x != x_opt || y != y_opt;
58  }
59  return distance;
60  }
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:

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