Share with others

Assignment No 04


AIM:


Write an Applet that crease a working version of “fifteen puzzle”. The puzzle contains a grid with 4 rows and 4 columns. The cells are filled with 1 to 15 leaving one cell empty. On clicking the mouse on a cell adjacent to the empty cell, the number in the cell moves to the empty cell. The puzzle is completed when the user arranges the numbers in the cells in ascending order. (AWT and Applet)


 THEORY:

Steps:

1)    Draw 4 rows and columns in the puzzle. Also stores the width and height in pixels of each cell. Design an array of integers which represents the puzzle internally.

2)    This two dimensional array is filled with numbers between 1 to 15 randomly selected. One cell is left blank.

3)    On MousePressed() event, check whether the mouse was pressed inside the puzzle (grid), then the mouse co-ordinates are translated into rows and columns of the grid. Use swap() method which contains swapping code which is get called only and only when the cells adjacent to the cell in which mouse was clicked is blank.  


CONCLUSION


Applet containing puzzle is successfully implemented.



PROGRAM CODE


Share with others