In Designer, try to reproduce a layout of buttons resembling that in Figure 9.14.
solution/layout/buttonrows/buttonrows.ui
The 15 puzzle (or n2–1 puzzle) involves an 4x4 (nxn) grid that contains 15 tiles numbered 1 to 15 (1 to n2–1), and one empty space. The only tiles that can move are those next to the empty space.
Create a 15 puzzle with QPushButtons in a QGridLayout.
At the start of the game, the tiles are presented to the player in "random" order. The object of the game is to rearrange them so that they are in ascending order, with the lowest numbered tile in the upper-left corner.
If the player solves the puzzle, pop up a QMessageBox saying "YOU WIN!" (or something more clever).
Add some buttons:
Shuffle – Randomize the tiles, by performing a large number (at least 50) of legal tile-slides.
Quit – Terminate the game.
[ fromfile: layout.xml id: x-layouts ]
QButtonGroup | |
---|---|
To avoid connecting a signal to a slot for each button, you can group the buttons together in a QButtonGroup, which has a 1-argument signal for QAbstractButton. |
Design Suggestions | |
---|---|
Start with classes shown in Figure 9.16 and try to partition your code properly into them. The "view" classes should deal with GUI while the "model" classes should have no QWidget code or dependency at all, and deal only with the logic of the game. |
solution/15-puzzle
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |