1 /***********************************************************************
2  *
3  * Copyright (C) 2007, 2008, 2010 Graeme Gott <graeme@gottcode.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  ***********************************************************************/
19 
20 #ifndef BOARD_H
21 #define BOARD_H
22 
23 #include <QWidget>
24 class QPixmap;
25 class QTimer;
26 class Piece;
27 
28 #include <random>
29 
30 class Board : public QWidget
31 {
32 	Q_OBJECT
33 public:
34 	Board(QWidget* parent = 0);
35 
36 	bool cell(int x, int y) const;
37 	bool endGame();
38 	void findFullLines();
39 
40 public slots:
41 	void newGame();
42 	void pauseGame();
43 	void resumeGame();
44 
45 signals:
46 	void pauseAvailable(bool available);
47 	void nextPieceAvailable(QPixmap piece);
48 	void levelUpdated(int level);
49 	void linesRemovedUpdated(int lines);
50 	void scoreUpdated(int score);
51 	void gameOver(int level, int lines, int score);
52 	void gameStarted();
53 	void hideMessage();
54 	void showMessage(const QString& message);
55 
56 protected:
57 	virtual void keyPressEvent(QKeyEvent* event);
58 	virtual void mousePressEvent(QMouseEvent* event);
59 	virtual void paintEvent(QPaintEvent*);
60 	virtual void focusOutEvent(QFocusEvent* event);
61 	virtual void resizeEvent(QResizeEvent* event);
62 
63 private slots:
64 	void shiftPiece();
65 	void flashLines();
66 	void removeLines();
67 
68 private:
69 	void gameOver();
70 	void addCell(int x, int y, int type);
71 	void removeCell(int x, int y);
72 	void createPiece();
73 	void landPiece();
74 	int nextPiece();
75 	QPixmap renderPiece(int type) const;
76 
77 private:
78 	QPixmap m_images[7];
79 	int m_cells[10][20];
80 	int m_full_lines[4];
81 	int m_removed_lines;
82 	int m_level;
83 	int m_score;
84 	Piece* m_piece;
85 	int m_next_piece;
86 	QTimer* m_shift_timer;
87 	QTimer* m_flash_timer;
88 	int m_flash_frame;
89 	int m_piece_size;
90 	QRect m_background;
91 	bool m_started;
92 	bool m_done;
93 	bool m_paused;
94 
95 	std::mt19937 m_random_generator;
96 	std::uniform_int_distribution<int> m_random_distribution;
97 };
98 
cell(int x,int y)99 inline bool Board::cell(int x, int y) const
100 {
101 	Q_ASSERT(x >= 0 && x < 10);
102 	Q_ASSERT(y >= 0 && y < 20);
103 	return m_cells[x][y] != 0;
104 }
105 
nextPiece()106 inline int Board::nextPiece()
107 {
108 	return m_random_distribution(m_random_generator);
109 }
110 
111 #endif // BOARD_H
112