001 /* 002 * Created on Sep 12, 2004 003 * 004 */ 005 package aima.search.eightpuzzle; 006 007 import aima.basic.XYLocation; 008 import aima.search.framework.HeuristicFunction; 009 010 /** 011 * @author Ravi Mohan 012 * 013 */ 014 015 public class MisplacedTilleHeuristicFunction implements HeuristicFunction { 016 017 public double getHeuristicValue(Object state) { 018 EightPuzzleBoard board = (EightPuzzleBoard) state; 019 return getNumberOfMisplacedTiles(board); 020 021 } 022 023 private int getNumberOfMisplacedTiles(EightPuzzleBoard board) { 024 int numberOfMisplacedTiles = 0; 025 if (!(board.getLocationOf(0).equals(new XYLocation(0, 0)))) { 026 numberOfMisplacedTiles++; 027 } 028 if (!(board.getLocationOf(1).equals(new XYLocation(0, 1)))) { 029 numberOfMisplacedTiles++; 030 } 031 if (!(board.getLocationOf(2).equals(new XYLocation(0, 2)))) { 032 numberOfMisplacedTiles++; 033 } 034 if (!(board.getLocationOf(3).equals(new XYLocation(1, 0)))) { 035 numberOfMisplacedTiles++; 036 } 037 if (!(board.getLocationOf(4).equals(new XYLocation(1, 1)))) { 038 numberOfMisplacedTiles++; 039 } 040 if (!(board.getLocationOf(5).equals(new XYLocation(1, 2)))) { 041 numberOfMisplacedTiles++; 042 } 043 if (!(board.getLocationOf(6).equals(new XYLocation(2, 0)))) { 044 numberOfMisplacedTiles++; 045 } 046 if (!(board.getLocationOf(7).equals(new XYLocation(2, 1)))) { 047 numberOfMisplacedTiles++; 048 } 049 if (!(board.getLocationOf(8).equals(new XYLocation(2, 2)))) { 050 numberOfMisplacedTiles++; 051 } 052 return numberOfMisplacedTiles; 053 } 054 055 }