Kalotan Puzzle
Previous    Next    Home    Source Code    Package    Up.java    Out.java    Choose.java    Variable.java    Puzzle.java   

  1. A better example of the use of exceptions for controling execution is shown here where we write code as though Java were a declarative language like prolog. The problem is a logic puzzle. To solve it we need to find values for variables that cause all logic expressions to be true. At any time in the execution some subset of variables has values. If extending to set the value of another variable is found to be impossible, one of the set variables is unset, then set to another value and execution continues. We develop the exeception Up to backtrack one level. We develop class Choose to try all possible values for a variable. The class Puzzle implements the assert_ method that ensures some logic expression is satisfied. The user sees the friendly looking Prog17 which only requires stating the logic expressions.
  2. The KalotanPuzzle class extends the Puzzle class (inheriting assert_) and implements the Variable interface (forcing implementation of checkIt which actually drives the computation and makes the puzzle itself look like another variable whose value must be set - although it is not set it behaves consistently with Choose objects).