Sei sulla pagina 1di 1

CPSC2150:Instructor Nalin Wijesinghe while(!

solution_found())
Assignment 2: design Due September 23, 08 {
Naoya Makino: 100106040 make a new node in each possible
Purpose: Design a puzzle ADT that will handle 8 case
digits square puzzle. in the new node, find the all new
Assumptions movable case
 In this puzzle, the digits 1 to 8 are in a 3 by 3 if find a solution of the case
grid leaving one "hole". return the solution path
 Starting from an initial configuration where the }
digits are not in sequence (in row major order) follow the solution path
 the objective is to arrange all the digits in }
sequence leaving the "hole" at the bottom right
corner. Movable digits places for each space’s cases
 A digit square may only be moved to a
neighbouring square if that neighbour is _ * * _ * * _
currently the "hole". * * *
 A neighbour is a vertical or horizontal
neighbour. * * *
 Any solution derived by your program need _ * * _ * * _
not be optimal. * * *
Required functions
• Constructors
* * *
o Puzzle() // Construct a new puzzle
_ * * _ * * _
• Accessors
o Void Print()//Print the board _ : space *: movable digits
o bool is_done()//Check if a game is
done
o bool is_movable(int index)//Check

Sample output
if the number of the index is a valid
movementint

-------------

initial board
o bool solution_found(TreeNode *)//
-------------
| 7 | 8 | 6 |
|---+---+---+
| 4 |
|---+---+---+
| 1 | 2 | 3 |
1234_5786
find a solution as a result of
estimations
| 5 | --> | 4 | 5 |
o TreeNode *createNode(TreeNode
*)
• Mutators
-->

o Void swap(int index)// Swap with -------------


-------------
| 7 | 8 | 6 |
|---+---+---+

|---+---+---+
| 1 | 2 | 3 |

index value with the space


12345_786

o Make_children(TreeNode *)//make move '5'


a child node under the parent
• Private
| --> | 4 | 5 | 6 |

o int solution[int]//the solution of the


-->

-------------

puzzle
-------------
| 7 | 8 |
|---+---+---+

|---+---+---+
| 1 | 2 | 3 |

move '6' = final

o int n_nodes//the number of nodes


12345678_

created
o int moves[int][int]//possible moves
for each position
|

Algorithm to solve the puzzle


while(!is_done)
{
for the all movable digits given the space

Potrebbero piacerti anche