Problem statement:
You might have come across a puzzle which contains 15 numbered square pieces, which can be moved horizontally or vertically. A possible arrangement of these pieces is shown below:
11 43 15 76
87 120 12 1
66 22 43 4
21 89 93
As you can see, there is a blank at the bottom right corner. Implement the following program:
Numbers should be displayed in the above fashion. Allow the user to hit any of the arrow keys (up, down, left or right). If user hits up key, then the pieces with number 4 and blank piece will be swapped, and this table will look like
11 43 15 76
87 120 12 1
66 22 43
21 89 93 4
After that if user hits left key, then the piece with number 93 and blank piece will be swapped and table will look like:
11 43 15 76
87 120 12 1
66 22 43
21 89 93 4
If user hits left key and there is no column on left, then it should give appropriate message, which shows that it can’t move this side. With every hit, it will check whether numbers are sorted in ascending order or not. When all 15 integers are sorted and blank is at last position, then it should stop by giving success message, and this table will look like:
1 4 11 12
15 21 22 43
43 66 76 87
89 93 120
To keep track of keys, use getch ( ) and identify each key by its codes as follows:
• Up arrow key: 72
• Down arrow key: 80
• Right arrow key: 75
• Left arrow key: 77
It should also show number of moves at each step. It may also be stopped by press ‘q’ somewhere to quit the program.